diff options
Diffstat (limited to 'pkgs/claude-inbox.nix')
| -rw-r--r-- | pkgs/claude-inbox.nix | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/pkgs/claude-inbox.nix b/pkgs/claude-inbox.nix index f75faa4..698f2c6 100644 --- a/pkgs/claude-inbox.nix +++ b/pkgs/claude-inbox.nix @@ -52,8 +52,53 @@ let --vm HOST ($CLAUDE_INBOX_VM, or claude), --no-fetch. EOF } + # colors, if FD (1 or 2) is a terminal, as completion has them: + # commands, options, REPO, BRANCH; none with NO_COLOR, or a dumb one + colors() { + C_CMD= C_OPT= C_REPO= C_BRANCH= C_OFF= + test -t "$1" && test -z "''${NO_COLOR-}" || return 0 + case ''${TERM-dumb} in dumb | "") return 0 ;; esac + local n p + case ''${COLORTERM-} in + truecolor | 24bit) p="38;2;230;180;80 38;2;150;150;175 38;2;100;160;240 38;2;120;200;120" ;; + *) + case $TERM in + *256color*) n=256 ;; + *) n=$(tput colors 2>/dev/null) || n=8 ;; + esac + if test "''${n:-0}" -ge 256; then p="38;5;179 38;5;103 38;5;75 38;5;114" + elif test "''${n:-0}" -ge 8; then p="33 35 34 32" + else return 0 + fi ;; + esac + set -- $p + C_CMD=$'\e['$1m C_OPT=$'\e['$2m C_REPO=$'\e['$3m C_BRANCH=$'\e['$4m C_OFF=$'\e[0m' + } + # the commands, options, REPO and BRANCH in a text (the help, an + # error), in their colors, if any (a colored word follows an m, so + # each loop ends) + paint() { + if test -z "$C_OFF"; then cat; return; fi + local x='[^-A-Za-z0-9_]' + sed -E \ + -e ":a; s/(^|$x)(--(list|show|take|push)|COMMAND)($x|$)/\1$C_CMD\2$C_OFF\4/; ta" \ + -e ":b; s/(^|$x)(-d|--vm|--no-fetch|--by-remote|-f|--force|--pick|--diff|-h|--help|OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \ + -e ":c; s/(^|$x)(REPO)($x|$)/\1$C_REPO\2$C_OFF\3/; tc" \ + -e ":d; s/(^|$x)(BRANCH)($x|$)/\1$C_BRANCH\2$C_OFF\3/; td" + } + # an error, and the REPO and BRANCH it's about, if known, in color die() { - echo "claude-inbox: $*" >&2 + local m="$*" + colors 2 + if test -n "$C_OFF"; then + if test -n "''${name-}" && test "''${m#"$name: "}" != "$m"; then + m="$C_REPO$name$C_OFF: ''${m#"$name: "}" + fi + if test -n "''${b-}"; then + m=''${m//"$b"/"$C_BRANCH$b$C_OFF"} + fi + fi + printf 'claude-inbox: %s\n' "$m" | paint >&2 exit 1 } @@ -85,8 +130,8 @@ let --pick) pick=1 ;; --diff) diff=1 what= ;; --diff=*) diff=1 what=''${1#--diff=} ;; - -h | --help) usage; exit ;; - -*) usage >&2; exit 1 ;; + -h | --help) colors 1; usage | paint; exit ;; + -*) colors 2; usage | paint >&2; exit 1 ;; *) words+=("$1") esac shift |
