From 69ad680b689ac59331245819c631510d8f559bc9 Mon Sep 17 00:00:00 2001 From: BarbUk Date: Tue, 5 Feb 2019 19:01:52 +0400 Subject: [PATCH 1/5] BASH: tab complete using fzf --- z.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/z.lua b/z.lua index 3965ec5..3ab4a62 100755 --- a/z.lua +++ b/z.lua @@ -1866,6 +1866,30 @@ if [ -n "$BASH_VERSION" ]; then fi ]] +local script_fzf_complete_bash = [[ +if command -v fzf >/dev/null 2>&1; then + # To redraw line after fzf closes (printf '\e[5n') + bind '"\e[0n": redraw-current-line' + + _zlua_fzf_complete() { + local query="${COMP_WORDS[COMP_CWORD]}" + + fzf="fzf --reverse --inline-info +s --height 35%" + + selected=$(_zlua --complete | $fzf --query "$query") + + printf '\e[5n' + + if [ -n "$selected" ]; then + COMPREPLY=("$selected") + return 0 + fi + } + + complete -o bashdefault -F _zlua_fzf_complete ${_ZL_CMD:-z} +fi +]] + local script_complete_zsh = [[ _zlua_zsh_tab_completion() { # tab completion @@ -1901,6 +1925,7 @@ function z_shell_init(opts) end end print(script_complete_bash) + print(script_fzf_complete_bash) elseif opts.zsh ~= nil then if prompt_hook then print(once and script_init_zsh_once or script_init_zsh) From bc0403e5c72772f0cb8e72e621243e0a3fa5d924 Mon Sep 17 00:00:00 2001 From: BarbUk Date: Tue, 5 Feb 2019 20:21:45 +0400 Subject: [PATCH 2/5] Define fzf as an option --- z.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/z.lua b/z.lua index 3ab4a62..b6cd6d9 100755 --- a/z.lua +++ b/z.lua @@ -32,6 +32,10 @@ -- * put something like this in your .bashrc: -- eval "$(lua /path/to/z.lua --init bash enhanced)" -- +-- Bash fzf tab completion Mode: +-- * put something like this in your .bashrc: +-- eval "$(lua /path/to/z.lua --init bash fzf)" +-- -- Zsh Install: -- * put something like this in your .zshrc: -- eval "$(lua /path/to/z.lua --init zsh)" @@ -1925,7 +1929,9 @@ function z_shell_init(opts) end end print(script_complete_bash) - print(script_fzf_complete_bash) + if opts.fzf ~= nil then + print(script_fzf_complete_bash) + end elseif opts.zsh ~= nil then if prompt_hook then print(once and script_init_zsh_once or script_init_zsh) From 7f1dd0a1ddb8d01633bb3680dc1d498784ed56a4 Mon Sep 17 00:00:00 2001 From: BarbUk Date: Tue, 5 Feb 2019 20:22:17 +0400 Subject: [PATCH 3/5] Add some documentation for fzf option --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b74294f..3ffaf63 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ z -i foo # cd with interactive selection if you want `z.lua` print the new directory after cd. + If you want `fzf` tab completion use: + + eval "$(lua /path/to/z.lua --init bash fzf)" + - zsh: put something like this in your `.zshrc`: From 5cd9136e28aa0a81d3db74b1ca2a391ad57ca702 Mon Sep 17 00:00:00 2001 From: BarbUk Date: Tue, 5 Feb 2019 20:36:00 +0400 Subject: [PATCH 4/5] Handle fzf height --- z.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/z.lua b/z.lua index b6cd6d9..51d8969 100755 --- a/z.lua +++ b/z.lua @@ -1877,10 +1877,7 @@ if command -v fzf >/dev/null 2>&1; then _zlua_fzf_complete() { local query="${COMP_WORDS[COMP_CWORD]}" - - fzf="fzf --reverse --inline-info +s --height 35%" - - selected=$(_zlua --complete | $fzf --query "$query") + selected=$(_zlua --complete | $zlua_fzf --query "$query") printf '\e[5n' @@ -1930,6 +1927,11 @@ function z_shell_init(opts) end print(script_complete_bash) if opts.fzf ~= nil then + fzf_cmd = "fzf --reverse --inline-info +s" + if not os.getenv('_ZL_FZF_FULLSCR') then + fzf_cmd = fzf_cmd .. ' --height 35%' + end + print('zlua_fzf="' .. fzf_cmd .. '"') print(script_fzf_complete_bash) end elseif opts.zsh ~= nil then From a842fb97c6c2003d444d94b4691a67bdd7e6aa6e Mon Sep 17 00:00:00 2001 From: BarbUk Date: Tue, 5 Feb 2019 20:36:21 +0400 Subject: [PATCH 5/5] Define selected var as local --- z.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/z.lua b/z.lua index 51d8969..7878ff4 100755 --- a/z.lua +++ b/z.lua @@ -1877,7 +1877,7 @@ if command -v fzf >/dev/null 2>&1; then _zlua_fzf_complete() { local query="${COMP_WORDS[COMP_CWORD]}" - selected=$(_zlua --complete | $zlua_fzf --query "$query") + local selected=$(_zlua --complete | $zlua_fzf --query "$query") printf '\e[5n'