Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions scripts/bash_pinyin_completion
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ if ! declare -F _comp_compgen_filedir &>/dev/null; then
fi

eval "function __bak_comp_compgen_filedir() { $(declare -f _comp_compgen_filedir | tail -n +2) }"
eval "function __bak_comp_compgen_filedir_xspec() { $(declare -f _comp_compgen_filedir_xspec | tail -n +2) }"

# replace _comp_compgen_filedir
_comp_compgen_filedir() {
__bak_comp_compgen_filedir "$@"
_pinyin_completion "$@"
}

_comp_compgen_filedir_xspec() {
__bak_comp_compgen_filedir_xspec "$@"
_pinyin_completion "$@"
}

_pinyin_completion() {
local cur="${COMP_WORDS[COMP_CWORD]}"

# ignore empty
Expand Down Expand Up @@ -46,15 +55,19 @@ _comp_compgen_filedir() {
if [[ "${compgen_opts[0]}" == -d ]]; then
mapfile -t pinyin_matched < <(
compgen -d -- |
grep -v '^\.\{1,2\}$' | # Exclude . and ..
bash-pinyin-completion-rs "$basepart" 2>/dev/null
)
else
mapfile -t pinyin_matched < <(
compgen -f -- | while IFS= read -r line; do
if [ -d "$line" ]; then
printf "%s/\n" "${line%%/}"
else
printf "%s\n" "$line"
compgen -f -- |
while IFS= read -r line; do
if [[ "$line" != "." && "$line" != ".." ]]; then # Exclude . and ..
if [ -d "$line" ]; then
printf "%s/\n" "${line%%/}"
else
printf "%s\n" "$line"
fi
fi
done | bash-pinyin-completion-rs "$basepart" 2>/dev/null
)
Expand All @@ -71,11 +84,7 @@ _comp_compgen_filedir() {
# merge result
local -a old_candidates=("${COMPREPLY[@]}")
COMPREPLY=("${old_candidates[@]}" "${pinyin_matched[@]}")

# remove dup
IFS=$'\n' read -r -d '' -a COMPREPLY < <(
printf '%s\n' "${COMPREPLY[@]}" | awk '!seen[$0]++' | sort
)
COMPREPLY=($(printf "%s\n" "${COMPREPLY[@]}" | awk '!seen[$0]++'))

# fix space postfix
if ((${#COMPREPLY[@]} == 1)) && [[ ${COMPREPLY[0]} != */ ]]; then
Expand Down