diff --git a/omz-custom/completion.sh b/omz-custom/completion.sh new file mode 100644 index 0000000..2d8a7df --- /dev/null +++ b/omz-custom/completion.sh @@ -0,0 +1,104 @@ +# autocomplete + +: ${CMR_USER_CACHE:="$HOME/.cache/cmr_users"} + +cmr_update_users() { + local cache="$CMR_USER_CACHE" + local dir="${cache:h}" # directory part + + mkdir -p -- "$dir" + + echo "Updating cmr user cache in $cache..." >&2 + + # Fetch ALL active users (using --paginate for multiple pages) + glab api "users?state=active&per_page=100" --paginate \ + | jq -r '.[].username' \ + | sort -u > "$cache" + + local count + count=$(wc -l < "$cache" | tr -d ' ') + echo "Stored $count usernames in $cache" >&2 +} + + +_cmr() { + emulate -L zsh + setopt extended_glob + + local -a opts + opts=('--draft') + + # Option completion + if [[ $words[CURRENT] == -* ]]; then + _describe 'options' opts + return + fi + + # Cache path + : ${CMR_USER_CACHE:="$HOME/.cache/cmr_users"} + + # If cache missing → auto-update once + if [[ ! -r "$CMR_USER_CACHE" ]]; then + # Show a short message only once per shell session + print -r -- "cmr: user cache missing, updating..." >&2 + cmr_update_users + fi + + # Load cached users + local -a users + users=("${(@f)$(< "$CMR_USER_CACHE")}") + + # Full current "word" at cursor + local cur=$words[CURRENT] + + # Split into prefix (before last comma) + fragment after last comma + local prefix last + prefix="${cur%,*}" + last="${cur##*,}" + + # Tell zsh what is fixed vs typed fragment + if [[ "$prefix" != "$cur" ]]; then + IPREFIX="${prefix}," + PREFIX="$last" + else + IPREFIX="" + PREFIX="$cur" + fi + + # Substring filter locally + if [[ -n "$last" ]]; then + local -a filtered + local u + for u in "${users[@]}"; do + [[ $u == *${last}* ]] && filtered+="$u" + done + users=("${filtered[@]}") + fi + + (( ${#users} == 0 )) && return 0 + + compadd -Q -- "${users[@]}" +} + +function _tat_autocomplete() { + local -a projects files dirs + local expl + + # Get tmuxinator projects + projects=(${(f)"$(tls)"}) + + # Get files and directories with proper completion + _alternative \ + 'projects:tmuxinator projects:compadd -a projects' \ + 'files:files:_files' +} + +# Register the completion function for zsh +if [[ -n ${ZSH_VERSION-} ]]; then + compdef _tat_autocomplete tat + compdef _cmr cmr +else + # Fallback for bash + complete -f -F _tat_autocomplete -o default tat + complete -f -F _cmr -o default tat +fi diff --git a/omz-custom/custom_commands.sh b/omz-custom/custom_commands.sh index 633eed9..4972b46 100644 --- a/omz-custom/custom_commands.sh +++ b/omz-custom/custom_commands.sh @@ -404,6 +404,7 @@ function gpla() { # source the tmux-sessions.sh in the same folder source $(dirname "$0")/tmux-sessions.sh +<<<<<<< HEAD function aoc() { if [ -z "$1" ]; then @@ -448,3 +449,5 @@ function aoc() { echo " - $dir/input.in" echo " - $dir/example.in" } + +source $(dirname "$0")/completion.sh diff --git a/omz-custom/tmux-sessions.sh b/omz-custom/tmux-sessions.sh index f598769..43ca731 100644 --- a/omz-custom/tmux-sessions.sh +++ b/omz-custom/tmux-sessions.sh @@ -33,24 +33,3 @@ function tat() { function tls() { tmuxinator list | grep -v tmuxinator | tr ' ' '\n' | grep -v '^$' } - -function _tat_autocomplete() { - local -a projects files dirs - local expl - - # Get tmuxinator projects - projects=(${(f)"$(tls)"}) - - # Get files and directories with proper completion - _alternative \ - 'projects:tmuxinator projects:compadd -a projects' \ - 'files:files:_files' -} - -# Register the completion function for zsh -if [[ -n ${ZSH_VERSION-} ]]; then - compdef _tat_autocomplete tat -else - # Fallback for bash - complete -f -F _tat_autocomplete -o default tat -fi