#!/usr/bin/env bash
# Subway Seat — generated from palette.py by build.py. Edit the palette, not this file.
#
# Subway Seat rows for Claude Code's subagent panel: a route bullet colored by
# state, the name, what it's doing, and how full its context is.
# Reads {columns, tasks:[…]} on stdin and prints one {"id","content"} line per task.
# Needs jq; without it Claude Code keeps its own rows.

input=$(cat)
command -v jq >/dev/null 2>&1 || exit 0

pick_flavor() {
  case ${SUBWAY_SEAT_FLAVOR:-} in walnut | tunnel | enamel) flavor=$SUBWAY_SEAT_FLAVOR; return ;; esac
  flavor=walnut
  case $1 in
    *subway-seat-enamel*) flavor=enamel ;;
    *subway-seat-tunnel*) flavor=tunnel ;;
    *subway-seat*) ;;
    light*) flavor=enamel ;;
    auto)
      case ${COLORFGBG:-} in
        *\;7 | *\;15) flavor=enamel ;;
        *\;*) ;;
        *) case $OSTYPE in darwin*) defaults read -g AppleInterfaceStyle >/dev/null 2>&1 || flavor=enamel ;; esac ;;
      esac ;;
  esac
}
pick_flavor "$(jq -r '.theme // empty' "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/settings.json" 2>/dev/null)"

case $flavor in
  tunnel) INK='20;13;7' TEXT='233;216;182' DIM='170;145;113' FAINT='145;119;89' RAIL='79;57;39' YELLOW='243;191;69' GREEN='173;185;86' SAGE='134;173;149' RED='255;131;115' ;;
  enamel) INK='248;239;223' TEXT='62;44;30' DIM='115;92;68' FAINT='140;114;84' RAIL='186;163;128' YELLOW='151;102;8' GREEN='102;116;15' SAGE='62;113;87' RED='191;66;51' ;;
  moquette) INK='18;24;38' TEXT='216;222;234' DIM='143;154;176' FAINT='115;129;156' RAIL='61;79;114' YELLOW='242;192;63' GREEN='119;197;129' SAGE='84;180;181' RED='254;132;116' ;;
  deep) INK='10;14;24' TEXT='212;218;231' DIM='139;150;172' FAINT='111;124;151' RAIL='48;62;91' YELLOW='242;192;63' GREEN='119;197;129' SAGE='84;180;181' RED='254;132;116' ;;
  portland) INK='232;240;255' TEXT='41;48;64' DIM='85;97;121' FAINT='105;119;148' RAIL='147;167;207' YELLOW='141;108;8' GREEN='13;129;49' SAGE='0;115;118' RED='204;46;37' ;;
  guimard) INK='19;26;23' TEXT='217;225;219' DIM='144;158;150' FAINT='116;133;124' RAIL='62;85;74' YELLOW='241;191;75' GREEN='112;202;169' SAGE='84;155;159' RED='231;135;123' ;;
  catacombes) INK='10;16;13' TEXT='212;221;215' DIM='140;154;146' FAINT='112;129;120' RAIL='49;67;58' YELLOW='241;191;75' GREEN='112;202;169' SAGE='84;155;159' RED='231;135;123' ;;
  carrelage) INK='238;242;241' TEXT='39;52;47' DIM='86;100;95' FAINT='108;124;118' RAIL='153;171;166' YELLOW='145;109;7' GREEN='33;131;102' SAGE='0;98;103' RED='172;59;50' ;;
  *) INK='32;22;14' TEXT='237;220;188' DIM='174;149;117' FAINT='150;123;92' RAIL='99;73;50' YELLOW='243;191;69' GREEN='173;185;86' SAGE='134;173;149' RED='255;131;115' ;;
esac

# Route bullets: round with a Nerd Font, square with SUBWAY_SEAT_GLYPHS=plain.
caps=$'\xee\x82\xb6\037\xee\x82\xb4'  # U+E0B6, U+E0B4
[ "${SUBWAY_SEAT_GLYPHS:-}" = plain ] && caps=$'\037'

jq -c --arg ink "$INK" --arg text "$TEXT" --arg dim "$DIM" --arg faint "$FAINT" --arg rail "$RAIL" \
  --arg run "$YELLOW" --arg ok "$GREEN" --arg bad "$RED" --arg idle "$SAGE" --arg caps "$caps" '
  def fg($c): "\u001b[38;2;\($c)m";
  def bg($c): "\u001b[48;2;\($c)m";
  def reset: "\u001b[0m";
  def clean: tostring | gsub("\\p{Cc}"; "");
  def cut($n): if length > $n then .[0:$n - 1] + "…" else . end;
  def k($n): if $n >= 1000000 then "\(($n / 100000 | floor) / 10)M"
             elif $n >= 1000 then "\(($n / 100 | floor) / 10)k" else "\($n | floor)" end;
  def kind: {local_agent: "agent", local_bash: "shell", remote_agent: "remote agent",
             local_workflow: "workflow", in_process_teammate: "teammate"}[.] // .;
  (.columns // 80) as $cols
  | (.tasks // [])[]
  | select(.id != null)
  | ((.status // "") | ascii_downcase) as $s
  | (if ($s | test("fail|error")) then $bad
     elif ($s | test("kill|cancel|stop")) then $faint
     elif ($s | test("complete|done|success|finish")) then $ok
     elif ($s | test("run|progress|active|work")) then $run
     else $idle end) as $line
  | ((.name // .description // (.type // "agent" | kind)) | clean) as $full
  | (.tokenCount // 0) as $tok
  | (if (.contextWindowSize // 0) > 0
     then ($tok * 5 / .contextWindowSize + 0.5 | floor | if . > 5 then 5 elif . < 0 then 0 else . end)
     else null end) as $cells
  | k($tok) as $toks
  | ($cols - 4 - (if $cells != null then 7 else 0 end) - 2 - ($toks | length)) as $avail
  | ((.label // .description // "") | clean | if . == $full then "" else . end) as $doing
  # the name gets the whole row when there is nothing else to say, else up to 28 columns
  | (if $doing == "" or ($full | length) + 2 + ($doing | length) <= $avail then $avail else [28, $avail] | min end
     | if . < 4 then 4 else . end) as $fit
  | ($full | cut($fit)) as $name
  | ($name | .[0:1] | ascii_upcase) as $letter
  | ($avail - ($name | length) - 2) as $room
  | ($doing | if $room < 8 then "" else cut($room) end) as $what
  | {
      id,
      content: (
        (($caps | split("\u001f")) as [$l, $r]
         | if $l == "" then bg($line) + fg($ink) + " \($letter) " + reset
           else fg($line) + $l + bg($line) + fg($ink) + "\u001b[1m" + $letter + reset + fg($line) + $r + reset end)
        + " "
        + fg($text) + $name + reset
        + (if $what != "" then "  " + fg($dim) + $what + reset else "" end)
        + (if $cells != null then "  " + fg($line) + ("━" * $cells) + fg($rail) + ("─" * (5 - $cells)) + reset else "" end)
        + "  " + fg($faint) + $toks + reset
      )
    }
' <<<"$input"
