This commit is contained in:
@ -227,5 +227,74 @@ main() {
|
||||
print_clean_repositories "$TEMP_FILE"
|
||||
}
|
||||
|
||||
# === Autocomplete Function ===
|
||||
|
||||
handle_autocomplete() {
|
||||
local cur_word="${COMP_WORDS[COMP_CWORD]}"
|
||||
local prev_word="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
|
||||
# If we're completing the first argument after 'whatsdirty'
|
||||
if [ ${COMP_CWORD} -eq 1 ]; then
|
||||
# Suggest common directories and saved directory
|
||||
local saved_dir=$(load_directory)
|
||||
local suggestions=""
|
||||
|
||||
# Add saved directory if it exists
|
||||
if [ -n "$saved_dir" ] && [ -d "$saved_dir" ]; then
|
||||
suggestions="$saved_dir"
|
||||
fi
|
||||
|
||||
# Add common development directories
|
||||
for dir in ~/code ~/projects ~/dev ~/src ~/workspace /home/*/code /home/*/projects; do
|
||||
if [ -d "$dir" ]; then
|
||||
# Only add if it contains git repositories
|
||||
if find "$dir" -maxdepth 2 -name ".git" -type d 2>/dev/null | head -1 | grep -q .; then
|
||||
suggestions="$suggestions $dir"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Directory completion for current word
|
||||
if [ -n "$cur_word" ]; then
|
||||
# Use compgen for directory completion
|
||||
local dir_completions=($(compgen -d -- "$cur_word"))
|
||||
# Add our suggestions that match the current word
|
||||
local matching_suggestions=($(echo "$suggestions" | tr ' ' '\n' | grep "^$cur_word" | sort -u))
|
||||
# Combine and deduplicate
|
||||
COMPREPLY=($(printf '%s\n' "${dir_completions[@]}" "${matching_suggestions[@]}" | sort -u))
|
||||
else
|
||||
# No current word, suggest all our directories plus general directory completion
|
||||
local dir_completions=($(compgen -d))
|
||||
local all_suggestions=($(echo "$suggestions" | tr ' ' '\n' | sort -u))
|
||||
# Combine and deduplicate, but limit to reasonable number
|
||||
COMPREPLY=($(printf '%s\n' "${all_suggestions[@]}" "${dir_completions[@]}" | sort -u | head -20))
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Check if we're being called for autocomplete
|
||||
if [ "$1" = "autocomplete" ]; then
|
||||
# This is called when the tool is being used for bash completion
|
||||
# The arguments after 'autocomplete' are the completion context
|
||||
shift
|
||||
|
||||
# Set up COMP_WORDS and COMP_CWORD from arguments
|
||||
COMP_WORDS=("$@")
|
||||
COMP_CWORD=$(($# - 1))
|
||||
|
||||
# Generate completions
|
||||
handle_autocomplete
|
||||
|
||||
# Print completions, one per line
|
||||
printf '%s\n' "${COMPREPLY[@]}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check if we're being called to show version
|
||||
if [ "$1" = "version" ]; then
|
||||
echo "whatsdirty 1.0.0"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Execute main function with all arguments
|
||||
main "$@"
|
Reference in New Issue
Block a user