Skip to content

Commit 7ef47ad

Browse files
committed
add: debug file
Signed-off-by: wxiwnd <[email protected]>
1 parent 4159ae2 commit 7ef47ad

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env bash
2+
3+
DEBUG_LOG="/tmp/pinyin_completion_debug.log"
4+
rm -f "$DEBUG_LOG"
5+
touch "$DEBUG_LOG"
6+
7+
debug() {
8+
echo "[$(date +%T.%N)] $*" >> "$DEBUG_LOG"
9+
}
10+
debug "Debug Session:"
11+
12+
# Load bash-completion if not loaded
13+
# [ -f /usr/share/bash-completion/bash_completion ] && \
14+
# . /usr/share/bash-completion/bash_completion
15+
16+
# Detect bash-completion
17+
if ! declare -F _comp_compgen_filedir &>/dev/null; then
18+
echo "No function _comp_compgen_filedir found. Please install bash-completion first."
19+
exit 1
20+
fi
21+
22+
eval "function __bak_comp_compgen_filedir() { $(declare -f _comp_compgen_filedir | tail -n +2) }"
23+
eval "function __bak_comp_compgen_filedir_xspec() { $(declare -f _comp_compgen_filedir_xspec | tail -n +2) }"
24+
25+
# replace _comp_compgen_filedir
26+
_comp_compgen_filedir() {
27+
debug "Pre _comp_compgen_filedir: $*"
28+
__bak_comp_compgen_filedir "$@"
29+
debug "Original COMPREPLY: ${COMPREPLY[*]}"
30+
_pinyin_completion "$@"
31+
debug "Post COMPREPLY: ${COMPREPLY[*]}"
32+
}
33+
34+
_comp_compgen_filedir_xspec() {
35+
debug "Pre _comp_compgen_filedir: $*"
36+
__bak_comp_compgen_filedir_xspec "$@"
37+
debug "Original COMPREPLY: ${COMPREPLY[*]}"
38+
_pinyin_completion "$@"
39+
debug "Post COMPREPLY: ${COMPREPLY[*]}"
40+
}
41+
42+
_pinyin_completion() {
43+
local cur="${COMP_WORDS[COMP_CWORD]}"
44+
45+
# ignore empty
46+
[ -z "$cur" ] && return
47+
48+
49+
local dirpart="$(dirname -- "$cur")"
50+
local basepart="$(basename -- "$cur")"
51+
52+
[[ "$dirpart" == "." && "$cur" != */* ]] && dirpart=""
53+
54+
local savedPWD="$PWD"
55+
local resolved_dir
56+
local compgen_opts=(-f)
57+
[[ "${1-}" == -d ]] && compgen_opts=(-d)
58+
59+
if [[ -n "$dirpart" ]]; then
60+
resolved_dir="$(realpath -- "$dirpart" 2>/dev/null)"
61+
if [[ -d "$resolved_dir" ]]; then
62+
cd -- "$resolved_dir" 2>/dev/null || return
63+
else
64+
cd "$savedPWD" || return
65+
return
66+
fi
67+
fi
68+
69+
local -a pinyin_matched
70+
if [[ "${compgen_opts[0]}" == -d ]]; then
71+
mapfile -t pinyin_matched < <(
72+
compgen -d -- |
73+
bash-pinyin-completion-rs "$basepart" 2>/dev/null
74+
)
75+
else
76+
mapfile -t pinyin_matched < <(
77+
compgen -f -- |
78+
while IFS= read -r line; do
79+
if [ -d "$line" ]; then
80+
printf "%s/\n" "${line%%/}"
81+
else
82+
printf "%s\n" "$line"
83+
fi
84+
done | bash-pinyin-completion-rs "$basepart" 2>/dev/null
85+
)
86+
fi
87+
88+
if [[ -n "$dirpart" ]]; then
89+
local sep="/"
90+
# dirpart is root
91+
if [[ "$dirpart" == "/" ]]; then
92+
sep=""
93+
fi
94+
95+
for i in "${!pinyin_matched[@]}"; do
96+
pinyin_matched[$i]="${dirpart}${sep}${pinyin_matched[$i]}"
97+
done
98+
fi
99+
100+
cd "$savedPWD" || return
101+
102+
# merge result
103+
local -a old_candidates=("${COMPREPLY[@]}")
104+
COMPREPLY=("${old_candidates[@]}" "${pinyin_matched[@]}")
105+
106+
# mapfile -t COMPREPLY < <(printf "%s\n" "${COMPREPLY[@]}" | awk '!seen[$0]++')
107+
declare -A seen
108+
local -a unique_compreply=()
109+
for item in "${COMPREPLY[@]}"; do
110+
if [[ -z "${seen[$item]}" ]]; then
111+
seen["$item"]=1
112+
unique_compreply+=( "$item" )
113+
fi
114+
done
115+
COMPREPLY=( "${unique_compreply[@]}" )
116+
117+
# fix space postfix
118+
if ((${#COMPREPLY[@]} == 1)) && [[ ${COMPREPLY[0]} != */ ]]; then
119+
compopt -o nospace 2>/dev/null
120+
fi
121+
}

0 commit comments

Comments
 (0)