style: restore the default group switching behavior (#567)

* style: restore the default group switching behavior

* fix: multiple header in one line

* fix: trailing spaces
This commit is contained in:
Aloxaf 2026-03-14 23:32:35 +08:00 committed by GitHub
parent 4a2cc70704
commit 9fa836466a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 45 deletions

View file

@ -88,8 +88,6 @@ if [[ "$use_fzf_default_opts" == "yes" ]]; then
fzf_default_opts=$FZF_DEFAULT_OPTS
fi
local -a initial_command=(${(z)reload_command} 0)
FZF_DEFAULT_OPTS=$fzf_default_opts SHELL=$ZSH_NAME ${(z)fzf_command} \
--ansi \
--bind=$binds \
@ -106,7 +104,7 @@ FZF_DEFAULT_OPTS=$fzf_default_opts SHELL=$ZSH_NAME ${(z)fzf_command} \
--query=$_ftb_query \
--tiebreak=begin \
${fzf_preview:+--preview=${ftb_preview_init}$fzf_preview} \
$fzf_flags < <($initial_command) || ret=$?
$fzf_flags < $tmp_dir/completions.$$ || ret=$?
if (( ! use_tmux_popup )); then
echoti civis >/dev/tty 2>/dev/null

View file

@ -9,34 +9,13 @@ local pid=$1 header_lines=$2 active_group_style=$3 tmp_dir=$4 offset=$5
# read completion list
local -a list=(${(f)mapfile[$tmp_dir/completions.$pid]})
local grep_cmd=''
if (( $#list > 10000 )); then
local v
# Prefer GNU grep: grep first (Linux), then ggrep (macOS/Homebrew)
# Use $commands[...] so we get the resolved executable path (not an alias/function).
if (( ${+commands[grep]} )); then
v=$("$commands[grep]" --version 2>/dev/null)
[[ $v == *'(GNU grep)'* ]] && grep_cmd=$commands[grep]
fi
if [[ -z $grep_cmd && ${+commands[ggrep]} -eq 1 ]]; then
v=$("$commands[ggrep]" --version 2>/dev/null)
[[ $v == *'(GNU grep)'* ]] && grep_cmd=$commands[ggrep]
fi
fi
# --- group marker detection (fzf-tab groups) ---------------------------------
# When groups exist, fzf-tab prefixes each entry with a group SGR like $'\e[94m'.
# When no groups exist (often file lists), entries may start with $'\e[0m'
# (reset for LS_COLORS). Treat that as "no groups".
local -Ua group_sgr_prefixes
if [[ -n $grep_cmd ]]; then
group_sgr_prefixes=(
${(f)"$(
print -l -- ${list:$header_lines} | "$grep_cmd" -a -oP $'^\x1b\\[[0-9;]*m'
)"}
)
if (( $#list > 10000 && $+commands[grep] )); then
group_sgr_prefixes=(${(f)"$(print -l -- ${list:$header_lines} | command grep -a -o $'^\x1b\\[[0-9;]*m')"})
else
group_sgr_prefixes=(${(M)${list:$header_lines}#$'\x1b['[0-9;]#*m})
fi
@ -46,14 +25,15 @@ if (( $#group_sgr_prefixes == 1 )) && [[ $group_sgr_prefixes[1] == $'\e[0m' ]];
group_sgr_prefixes=()
fi
# --- current group index (persist across reloads) ----------------------------
local current=1
# calculate and persist current group index
# the index starts from 2, because switching from "all" to "group 1" looks weird
local current=2
if (( $#group_sgr_prefixes > 0 )) && [[ -f $tmp_dir/current-group.$pid ]]; then
current=$(( $(<$tmp_dir/current-group.$pid) + offset ))
(( current > $#group_sgr_prefixes )) && current=1
(( current <= 0 )) && current=$#group_sgr_prefixes
fi
print -r -- $current > $tmp_dir/current-group.$pid
(( current > $#group_sgr_prefixes )) && current=1
(( current == 0 )) && current=$#group_sgr_prefixes
echo $current > $tmp_dir/current-group.$pid
# configure style of active header
local sgr_on='' sgr_off=''
@ -66,24 +46,16 @@ case $active_group_style in
esac
# The ANSI SGR code that marks every line in the currently selected group.
local current_prefix=${group_sgr_prefixes[current]}
local current_prefix=$group_sgr_prefixes[current]
# print headers
if (( header_lines != 0 )); then
local -i i
local line
for i in {1..$header_lines}; do
line=$list[i]
if [[ $line == *$current_prefix* ]]; then
# Apply style only to the group label (up to the first ']'), then disable it
line=${line/$current_prefix(#b)([^]]##)\]/$current_prefix$sgr_on${match[1]}]$sgr_off}
fi
print -r $line
done
# Apply style only to the group label (excluding trailing spaces), then disable it
print -l ${${(S)list[1,header_lines]/(#b)($current_prefix*)($'\x1b[00m')/${match[1]/%(#b)( #)/$sgr_off$match}$match[2]}/$current_prefix/$current_prefix$sgr_on}
fi
if [[ -n $grep_cmd ]]; then
print -rl -- ${list:$header_lines} | "$grep_cmd" -a -F -- "${group_sgr_prefixes[current]}"
if (( $#list > 10000 && $+commands[grep] )); then
print -l -- ${list:$header_lines} | command grep -a -F -- "${group_sgr_prefixes[current]}"
else
print -rl -- ${(M)${list:$header_lines}:#${group_sgr_prefixes[current]}*}
print -l -- ${(M)${list:$header_lines}:#${group_sgr_prefixes[current]}*}
fi