Skip to content

Commit

Permalink
refactor: _fzf_tab_get_headers => -ftb-generate-header
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf committed Oct 24, 2020
1 parent 252fd4e commit f3afb03
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 42 deletions.
41 changes: 2 additions & 39 deletions fzf-tab.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -173,43 +173,6 @@ _fzf_tab_find_query_str() {
done
}

# pupulates array `headers` with group descriptions
_fzf_tab_get_headers() {
typeset -ga headers=()
local i tmp group_colors
local -i mlen=0 len=0

if (( $#_ftb_groups == 1 )) && { ! -ftb-zstyle -m single-group "header" }; then
return
fi

# calculate the max column width
for i in $_ftb_groups; do
(( $#i > mlen )) && mlen=$#i
done
mlen+=1

-ftb-zstyle -a group-colors group_colors || group_colors=($_ftb_group_colors)

for (( i=1; i<=$#_ftb_groups; i++ )); do
[[ $_ftb_groups[i] == "__hide__"* ]] && continue

if (( len + $#_ftb_groups[i] > COLUMNS - 5 )); then
headers+=$tmp
tmp='' && len=0
fi
if (( len + mlen > COLUMNS - 5 )); then
# the last column doesn't need padding
headers+=$tmp$group_colors[i]$_ftb_groups[i]$'\033[00m'
tmp='' && len=0
else
tmp+=$group_colors[i]${(r:$mlen:)_ftb_groups[i]}$'\033[00m'
len+=$mlen
fi
done
(( $#tmp )) && headers+=$tmp
}

_fzf_tab_colorize() {
emulate -L zsh -o cbases -o octalzeroes

Expand Down Expand Up @@ -264,11 +227,11 @@ _fzf_tab_complete() {
1) choices=("EXPECT_KEY" "${_ftb_compcap[1]%$bs*}");;
*)
_fzf_tab_find_query_str # sets `query`
_fzf_tab_get_headers # sets `headers`
-ftb-generate-header # sets `_ftb_headers`
-ftb-zstyle -s continuous-trigger continuous_trigger || continuous_trigger=/
-ftb-zstyle -s print-query print_query || print_query=alt-enter

print -rl -- $headers $_ftb_complist | -ftb-fzf
print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf

# insert query string directly
if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then
Expand Down
6 changes: 3 additions & 3 deletions lib/-ftb-fzf
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ local fzf_command fzf_flags tmp
print -rl -- $_ftb_compcap > /tmp/fzf-tab/compcap.$$
print -rl -- $_ftb_groups > /tmp/fzf-tab/groups.$$

local -i lines=$#_ftb_compcap
local -i lines=$#_ftb_compcap header_lines=$#_ftb_headers

typeset -g choices
choices="$($fzf_command \
--ansi \
--bind=$binds \
--color=hl:$(( $#headers == 0 ? 188 : 255 )) \
--color=hl:$(( $#_ftb_headers == 0 ? 188 : 255 )) \
--cycle \
--delimiter='\x00' \
--expect=$continuous_trigger,$print_query \
--header-lines=$#headers \
--header-lines=$#_ftb_headers \
--height=${FZF_TMUX_HEIGHT:=$(( lines > LINES / 2 ? LINES / 2 : lines ))} \
--layout=reverse \
--multi \
Expand Down
35 changes: 35 additions & 0 deletions lib/-ftb-generate-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/hint/zsh

typeset -ga _ftb_headers=()
local i tmp group_colors
local -i mlen=0 len=0

if (( $#_ftb_groups == 1 )) && { ! -ftb-zstyle -m single-group "header" }; then
return
fi

# calculate the max column width
for i in $_ftb_groups; do
(( $#i > mlen )) && mlen=$#i
done
mlen+=1

-ftb-zstyle -a group-colors group_colors || group_colors=($_ftb_group_colors)

for (( i=1; i<=$#_ftb_groups; i++ )); do
[[ $_ftb_groups[i] == "__hide__"* ]] && continue

if (( len + $#_ftb_groups[i] > COLUMNS - 5 )); then
_ftb_headers+=$tmp
tmp='' && len=0
fi
if (( len + mlen > COLUMNS - 5 )); then
# the last column doesn't need padding
_ftb_headers+=$tmp$group_colors[i]$_ftb_groups[i]$'\033[00m'
tmp='' && len=0
else
tmp+=$group_colors[i]${(r:$mlen:)_ftb_groups[i]}$'\033[00m'
len+=$mlen
fi
done
(( $#tmp )) && _ftb_headers+=$tmp

0 comments on commit f3afb03

Please sign in to comment.