← back to Terminal Status

verification/install/before/.agents/skills/color/scripts/semantic-dot.sh

71 lines

#!/usr/bin/env bash
set -u

STATE="${1:?state required}"
R="${2:?red required}"
G="${3:?green required}"
B="${4:?blue required}"
DOT="${5:?dot required}"
DEFAULT_LABEL="${6:?default label required}"
shift 6

paint() {
  local tty_path="$1" label="${2:-$DEFAULT_LABEL}" off="${3:-0}"
  [[ -w "$tty_path" ]] || return 1
  local state_file="${HOME}/.codex/tab-dots/$(basename "$tty_path").dot"
  if [[ "$off" == "1" ]]; then
    { printf '\033]6;1;bg;*;default\007'; printf '\033]1337;SetBadgeFormat=\a'; } > "$tty_path" 2>/dev/null
    rm -f "$state_file" 2>/dev/null
    return 0
  fi
  local title="$DOT $label"
  {
    printf '\033]6;1;bg;red;brightness;%d\007' "$R"
    printf '\033]6;1;bg;green;brightness;%d\007' "$G"
    printf '\033]6;1;bg;blue;brightness;%d\007' "$B"
    printf '\033]1;%s\007' "$title"
    printf '\033]2;%s\007' "$title"
  } > "$tty_path" 2>/dev/null
  mkdir -p "${HOME}/.codex/tab-dots"
  printf '%s' "$title" > "$state_file"
  printf '\033]1337;SetBadgeFormat=%s\a' "$(printf '%s' "$title" | base64 | tr -d '\n')" > "$tty_path" 2>/dev/null
}

if [[ "${1:-}" == "--all" ]]; then
  ttys="$(ps -axo tty=,comm=,command= 2>/dev/null | awk '$1 ~ /^ttys/ && $3 ~ /(^|\/)codex$/ {print $1}' | sort -u)"
  [[ -z "$ttys" ]] && { echo "/${STATE}dot --all → no live Codex ttys"; exit 0; }
  count=0
  for tty_name in $ttys; do
    paint "/dev/$tty_name" "$DEFAULT_LABEL" 0 && count=$((count + 1))
  done
  echo "/${STATE}dot --all → marked ${count} Codex tab(s) $DOT $DEFAULT_LABEL"
  exit 0
fi

OFF=0
LABEL="${1:-$DEFAULT_LABEL}"
[[ "${1:-}" == "--off" ]] && OFF=1

tty_path=""
process_id=$$
for _ in 1 2 3 4 5 6 7 8 9 10; do
  line="$(ps -o ppid=,tty= -p "$process_id" 2>/dev/null)" || break
  set -- $line
  parent_id="${1:-}"
  tty_name="${2:-}"
  [[ -z "$parent_id" ]] && break
  if [[ "$tty_name" == ttys* ]]; then tty_path="/dev/$tty_name"; break; fi
  process_id="$parent_id"
done

if [[ -z "$tty_path" ]]; then
  echo "/${STATE}dot → could not resolve this Codex session tty"
  exit 1
fi

if [[ "$OFF" == "1" ]]; then
  paint "$tty_path" "" 1 && echo "/${STATE}dot --off → cleared $tty_path"
else
  paint "$tty_path" "$LABEL" 0 && echo "/${STATE}dot → $tty_path $DOT $LABEL rgb(${R},${G},${B})"
fi