diff options
Diffstat (limited to 'pkgs/claude-remote.nix')
| -rw-r--r-- | pkgs/claude-remote.nix | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix index 327a871..e8e0dc7 100644 --- a/pkgs/claude-remote.nix +++ b/pkgs/claude-remote.nix @@ -801,16 +801,80 @@ let case $used in *" $o "*) ;; *) left="$left $o" ;; esac done [ "$cmd" != rename ] || max=$((max - $(printf '%s\n' $used | grep -c -e '^--from=$' -e '^--to=$'))) - COMPREPLY=($(compgen -W "$cmds $left" -- "$cur")) - if [ ''${#words[@]} -lt "$max" ]; then - COMPREPLY+=($(compgen -W "$(claude-remote --names 2>/dev/null)" -- "$cur")) - fi + # in groups: commands, options, names (their kinds, for the colors) + local kinds=() k=0 g + COMPREPLY=() + for g in "$cmds" "$left" "$([ ''${#words[@]} -ge "$max" ] || claude-remote --names 2>/dev/null)"; do + for c in $(compgen -W "$g" -- "$cur"); do + COMPREPLY+=("$c") kinds+=("$k") + done + k=$((k + 1)) + done # an option that takes a value (--type=), the only one: no space after if [ ''${#COMPREPLY[@]} = 1 ] && [ "''${COMPREPLY[0]%=}" != "''${COMPREPLY[0]}" ]; then compopt -o nospace fi + # Listing only (a second TAB), in color, by kind: readline would + # print escapes as ^[, so the list is printed here, and readline gets + # one blank candidate, to list instead, and redraw the prompt after. + if [ "$COMP_TYPE" = 63 ] && [ ''${#COMPREPLY[@]} -gt 1 ]; then + local colors= + colors=$(_claude_remote_colors) || return 0 + _claude_remote_list "$colors" "''${kinds[*]}" "''${COMPREPLY[@]}" >/dev/tty + COMPREPLY=(" ") + fi + } + # the colors of commands, options, and names, as SGR parameters (as + # claude-inbox has them): truecolor, 256 colors, or 8, as the terminal + # has them; none (and so readline's own listing) with NO_COLOR, or on a + # dumb terminal + _claude_remote_colors() { + [ -z "''${NO_COLOR-}" ] && [ -w /dev/tty ] || return 1 + case ''${TERM-dumb} in dumb | "") return 1 ;; esac + local n + case ''${COLORTERM-} in + truecolor | 24bit) + echo "38;2;230;180;80 38;2;150;150;175 38;2;100;160;240" + return ;; + esac + case $TERM in + *256color*) n=256 ;; + *) n=$(tput colors 2>/dev/null) || n=8 ;; + esac + if [ "''${n:-0}" -ge 256 ]; then + echo "38;5;179 38;5;103 38;5;75" + elif [ "''${n:-0}" -ge 8 ]; then + echo "33 35 34" + else + return 1 + fi + } + # COLORS KINDS WORD...: the words in columns, down then across, like + # readline's, each in the color of its kind + _claude_remote_list() { + local colors=($1) kinds=($2) words=("''${@:3}") w width=0 n=$(($# - 2)) + for w in "''${words[@]}"; do [ ''${#w} -le "$width" ] || width=''${#w}; done + width=$((width + 2)) + local cols=$(( ''${COLUMNS:-80} / width )) rows r c i + [ "$cols" -ge 1 ] || cols=1 + rows=$(( (n + cols - 1) / cols )) + printf '\n' + for ((r = 0; r < rows; r++)); do + for ((c = 0; c < cols; c++)); do + i=$((c * rows + r)) + [ "$i" -lt "$n" ] || break + if [ $((c + 1)) -lt "$cols" ] && [ $((i + rows)) -lt "$n" ]; then + printf '\e[%sm%s\e[0m%*s' "''${colors[kinds[i]]}" "''${words[i]}" $((width - ''${#words[i]})) "" + else + printf '\e[%sm%s\e[0m' "''${colors[kinds[i]]}" "''${words[i]}" + fi + done + [ $((r + 1)) -ge "$rows" ] || printf '\n' + done } - complete -F _claude_remote claude-remote + # in that order (bash 4.4 and later) + complete -o nosort -F _claude_remote claude-remote 2>/dev/null || + complete -F _claude_remote claude-remote ''; in runCommand "claude-remote" { meta.mainProgram = "claude-remote"; } '' |
