jimhester / per-directory-history

Per directory history for zsh, as well as global history, and the ability to toggle between them with ^G.
zlib License
259 stars 36 forks source link

[Request] Make it work with fzf-history-widget #41

Open jezus-nunez opened 3 years ago

jezus-nunez commented 3 years ago

Tried a variant from which includes the binding for ^G https://github.com/junegunn/fzf/blob/master/shell/key-bindings.zsh

# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
  local selected num
  setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
  selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
    FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-g:per-directory-history-toggle-history,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
  local ret=$?
  if [ -n "$selected" ]; then
    num=$selected[1]
    if [ -n "$num" ]; then
      zle vi-fetch-history -n $num
    fi
  fi
  zle reset-prompt
  return $ret
}

But it is failing silently, when calling it straight from the terminal I get

❯ per-directory-history-toggle-history

using global historyper-directory-history-toggle-history:zle:8: widgets can only be called when ZLE is active
per-directory-history-toggle-history:zle:9: widgets can only be called when ZLE is active

Any hints on how to make this work, bypassing the ZLE requirement?

lmburns commented 3 years ago

I found a workaround, you can do this and create a widget that will call both per-directory-history-toggle-history and fzf-history-widget. It also helps that powerlevel10k has a feature that shows whether or not per-directory-history-toggle-history is active. Here is what I put in my .zshrc.

per-dir-fzf() { 
  # if  history file in use is not the directories (i.e., is global)
  if [[ $_per_directory_history_is_global == true ]]; then
    per-directory-history-toggle-history; fzf-history-widget
  else
    fzf-history-widget
  fi
}
zle -N per-dir-fzf
bindkey '®' per-dir-fzf  # alt+t instead of ctrl+t (normal fzf widget)

Hope this helps somewhat.

Edit: I just realized that I added the functionality to p10k and it doesn't come by default. However, it is easy to add.

Create a function titled prompt_my_per_dir_status

if $_per_directory_history_is_global; then
    return
else
    p10k segment -i ''
fi

Then in your .p10k.zsh file add the following to this array:

typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
     my_per_dir_status
)