From 2a97c358f642bad32bbdd6e025a8b5700bd2eb91 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Tue, 26 Mar 2019 10:22:47 +0300 Subject: [PATCH] more args, restructurize --- rc/fzf.kak | 65 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index e2a3680..14d6bff 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -43,7 +43,7 @@ These are default arguments for the tools above: highlight: "highlight --failsafe -O ansi {}" rouge: "rougify {}" ' \ -str fzf_highlighter "highlight" +str fzf_highligh_cmd "highlight" declare-option -docstring "height of fzf tmux split in screen lines or percents. Default value: 25%%" \ @@ -143,7 +143,7 @@ fzf -params 2..4 %{ evaluate-commands %sh{ fi if [ -z "${command##edit*}" ] && [ $kak_opt_fzf_preview = "true" ]; then - case $kak_opt_fzf_highlighter in + case $kak_opt_fzf_highligh_cmd in bat) highlighter="bat --color=always --style=plain {}" ;; coderay) @@ -153,11 +153,11 @@ fzf -params 2..4 %{ evaluate-commands %sh{ rouge) highlighter="rougify {}" ;; bat*|coderay*|highlight*|rougify*) - highlighter=$kak_opt_fzf_highlighter ;; + highlighter=$kak_opt_fzf_highligh_cmd ;; *) - executable=$(printf "%s\n" "$kak_opt_fzf_highlighter" | grep -o -E '[[:alpha:]]+' | head -1) + executable=$(printf "%s\n" "$kak_opt_fzf_highligh_cmd" | grep -o -E '[[:alpha:]]+' | head -1) printf "%s\n" "echo -markup %{{Information}'$executable' highlighter is not supported by the script. fzf.kak may not work as you expect.}" - highlighter=$kak_opt_fzf_highlighter ;; + highlighter=$kak_opt_fzf_highligh_cmd ;; esac tmux_height=$kak_opt_fzf_tmux_height_file_preview @@ -217,25 +217,54 @@ fzf -params 2..4 %{ evaluate-commands %sh{ }} define-command -docstring \ -"fzf fzf-command: generic fzf command. -This command can be used to create new fzf wrappers for various Kakoune or external -features. More about arguments: +"fzf : generic fzf command. This command can be used to create new fzf wrappers for various Kakoune or external features. Switches: --kak-cmd : A Kakoune cmd that is applied to fzf resulting value. - --fzf-cmd : A command that is used to provide list of values to fzf. --fzf-args : Additional flags for fzf program --post-action : Extra commands that are preformed after `-kak-cmd' command." \ + -kak-cmd : A Kakoune cmd that is applied to fzf resulting value. + -fzf-cmd : A command that is used to provide list of values to fzf. + -fzf-args : Additional flags for fzf program + -post-action : Extra commands that are preformed after `-kak-cmd' command. + -preview: should fzf window include preview" \ new-fzf -shell-script-candidates %{echo "-kak-cmd\n-fzf-cmd\n-fzf-args\n-post-action\n"} -params .. %{ evaluate-commands %sh{ while [ $# -gt 0 ]; do case $1 in - -kak-cmd) shift; kak_cmd="$kak_cmd $1" ;; - -fzf-cmd) shift; fzf_cmd="$fzf_cmd $1" ;; - -fzf-args) shift; fzf_args="$fzf_args $1" ;; - -post-action) shift; post_action="$post_action $1" ;; - *) ignore="$ignore $1" ;; + -kak-cmd) shift; kak_cmd="${kak_cmd} $1" ;; + -fzf-cmd) shift; fzf_cmd="${fzf_cmd} $1" ;; + -fzf-args) shift; fzf_args="${fzf_args} $1" ;; + -post-action) shift; post_action="${post_action} $1" ;; + -preview) preview=true + *) ignored="${ignored} $1" ;; esac shift done + + if [ "$preview" = "true" ] && [ ${kak_opt_fzf_preview} = "true" ]; then + case ${kak_opt_fzf_highligh_cmd} in + bat) highligh_cmd="bat --color=always --style=plain {}" ;; + coderay) highligh_cmd="coderay {}" ;; + highlight) highligh_cmd="highlight --failsafe -O ansi {}" ;; + rouge) highligh_cmd="rougify {}" ;; + *) highligh_cmd="${kak_opt_fzf_highligh_cmd}" ;; + esac + if [ -n "${kak_client_env_TMUX}" ]; then + [ -z "${kak_cmd##edit*}" ] && tmux_height=$kak_opt_fzf_tmux_height_file_preview + preview_pos="pos=right:${kak_opt_fzf_preview_width};" + else + preview_pos="sleep 0.1; [ \$(tput cols) -gt \$(expr \$(tput lines) \* 2) ] && pos=right:${kak_opt_fzf_preview_width} || pos=top:${kak_opt_fzf_preview_height};" + fi + fzf_args="${fzf_args} --preview '(${highligh_cmd} || cat {}) 2>/dev/null | head -n ${kak_opt_fzf_preview_lines}' --preview-window=\${pos}" + fi + + fzf_tmp=$(mktemp -d ${TMPDIR:-/tmp}/fzf.kak.XXXXXX) + fzfcmd="${fzf_tmp}/fzfcmd" + result="${fzf_tmp}/result" + printf "%s\n" "cd \"${PWD}\" && ${preview_pos} ${fzf_cmd} | SHELL=$(command -v sh) ${kak_opt_fzf_implementation} ${fzf_args} > ${result}; rm ${fzfcmd}" > ${fzfcmd} + chmod 755 ${fzfcmd} + + if [ -n "${kak_client_env_TMUX}" ]; then + [ -n "${tmux_height%%*%}" ] && measure="-l" || measure="-p" + cmd="nop %sh{ command tmux split-window ${measure} ${tmux_height%%%*} 'sh -c ${fzfcmd}' }" + else + cmd="terminal %{${fzfcmd}}" + fi }}