Skip to content

ENH: User Experience and Bug fix #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
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
31 changes: 22 additions & 9 deletions scripts/bash_pinyin_completion
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,30 @@ _pinyin_completion() {
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 [[ "$line" != "." && "$line" != ".." ]]; then # Exclude . and ..
if [ -d "$line" ]; then
printf "%s/\n" "${line%%/}"
else
printf "%s\n" "$line"
fi
if [ -d "$line" ]; then
printf "%s/\n" "${line%%/}"
else
printf "%s\n" "$line"
fi
done | bash-pinyin-completion-rs "$basepart" 2>/dev/null
)
fi

if [[ -n "$dirpart" ]]; then
local sep="/"
# dirpart is root
if [[ "$dirpart" == "/" ]]; then
sep=""
fi

for i in "${!pinyin_matched[@]}"; do
pinyin_matched[$i]="${dirpart}/${pinyin_matched[$i]}"
pinyin_matched[$i]="${dirpart}${sep}${pinyin_matched[$i]}"
done
fi

Expand All @@ -84,7 +87,17 @@ _pinyin_completion() {
# merge result
local -a old_candidates=("${COMPREPLY[@]}")
COMPREPLY=("${old_candidates[@]}" "${pinyin_matched[@]}")
mapfile -t COMPREPLY < <(printf "%s\n" "${COMPREPLY[@]}" | awk '!seen[$0]++')

# mapfile -t COMPREPLY < <(printf "%s\n" "${COMPREPLY[@]}" | awk '!seen[$0]++')
declare -A seen
local -a unique_compreply=()
for item in "${COMPREPLY[@]}"; do
if [[ -z "${seen[$item]}" ]]; then
seen["$item"]=1
unique_compreply+=( "$item" )
fi
done
COMPREPLY=( "${unique_compreply[@]}" )

# fix space postfix
if ((${#COMPREPLY[@]} == 1)) && [[ ${COMPREPLY[0]} != */ ]]; then
Expand Down
7 changes: 7 additions & 0 deletions scripts/install_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ if [ ! -d /etc/bash_completion.d ]; then
exit 1
fi

# Install debug version if --debug is specified
if [ "$1" = "--debug" ]; then
echo "Installing debug version..."
sudo cp scripts/bash_pinyin_completion_debug /etc/bash_completion.d/bash_pinyin_completion
exit 0
fi

# Install or upgrade the binary
if [ -f /usr/bin/bash-pinyin-completion-rs ]; then
echo "The binary /usr/bin/bash-pinyin-completion-rs already exists. Upgrading..."
Expand Down