Merge pull request #18 from JaviMerino/no_duplicates

Add options to remove dates and duplicates from search
This commit is contained in:
Josh Skidmore
2022-05-16 11:46:57 -04:00
committed by GitHub
2 changed files with 37 additions and 11 deletions

View File

@@ -36,13 +36,15 @@ plugins=(… zsh-fzf-history-search)
## Configuration Variables
| Variable | Description |
| ----------------------------------------- | ------------------------------------------------------------------------------------------------- |
| `ZSH_FZF_HISTORY_SEARCH_BIND` | Keybind to trigger fzf reverse search (default: `'^r'`) |
| `ZSH_FZF_HISTORY_SEARCH_FZF_ARGS` | Arguments for `fzf` (might be updated, not recommended to override) (default: `'+s +m -x -e'`) |
| `ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS` | Extra arguments for `fzf` (default: `''`) |
| `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | Put the cursor on at the end of the line after completion, `empty=false` (default: `''`) |
| `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | Include event numbers in search. Set to 0 to remove event numbers from the search. (default: `1`)|
| Variable | Default | Description |
| ----------------------------------------- | --------------- | ---------------------------------------------------------------------------------------------------------- |
| `ZSH_FZF_HISTORY_SEARCH_BIND` | `'^r'` | Keybind to trigger fzf reverse search |
| `ZSH_FZF_HISTORY_SEARCH_FZF_ARGS` | `'+s +m -x -e'` | Arguments for `fzf` (might be updated, not recommended to override) |
| `ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS` | `''` | Extra arguments for `fzf` |
| `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | `''` | Put the cursor on at the end of the line after completion, `empty=false` |
| `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | `1` | Include event numbers in search. Set to 0 to remove event numbers from the search. |
| `ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH` | `1` | Include ISO8601 timestamps in search. Set to 0 to remove them from the search. |
| `ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES`| `''` | Remove duplicate entries from search. Only makes sense with `EVENT_NUMBERS` and `DATE_INSEARCH` 0 (false).|
## TODO

View File

@@ -21,18 +21,42 @@ typeset -g ZSH_FZF_HISTORY_SEARCH_END_OF_LINE=''
(( ! ${+ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS=1
# Include full date timestamps in ISO8601 `yyyy-mm-dd hh:mm' format
(( ! ${+ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH=1
# Remove duplicate entries in history
(( ! ${+ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES} )) &&
typeset -g ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES=''
fzf_history_search() {
setopt extendedglob
FC_ARGS="-li"
CANDIDATE_LEADING_FIELDS=4
FC_ARGS="-l"
CANDIDATE_LEADING_FIELDS=2
if (( ! $ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS )); then
FC_ARGS+=" -n"
CANDIDATE_LEADING_FIELDS=3
((CANDIDATE_LEADING_FIELDS--))
fi
candidates=(${(f)"$(fc ${=FC_ARGS} -1 0 | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"})
if (( $ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH )); then
FC_ARGS+=" -i"
((CANDIDATE_LEADING_FIELDS+=2))
fi
history_cmd="fc ${=FC_ARGS} -1 0"
if [ -n "${ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES}" ];then
if (( $+commands[awk] )); then
history_cmd="$history_cmd | awk '!seen[\$0]++'"
else
# In case awk is not installed fallback to uniq. It will only remove commands that are repeated consecutively.
history_cmd="$history_cmd | uniq"
fi
fi
candidates=(${(f)"$(eval $history_cmd | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"})
local ret=$?
if [ -n "$candidates" ]; then
BUFFER="${candidates[@]/(#m)*/${${(As: :)MATCH}[${CANDIDATE_LEADING_FIELDS},-1]}}"