{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "terminal",
  "title": "Terminal",
  "description": "A console log with tagged lines, a prompt with history, and a follow-the-end scroll.",
  "dependencies": [
    "@ui-sdk/react@0.1.0-beta.5",
    "lucide-react@1.73.0"
  ],
  "registryDependencies": [
    "https://www.cyberspec.dev/r/cyber.json",
    "https://www.cyberspec.dev/r/button.json"
  ],
  "files": [
    {
      "path": "registry/cyber/ui/terminal.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { ChevronsDown } from \"lucide-react\"\nimport {\n  ScrollArea,\n  ScrollAreaContent,\n  ScrollAreaScrollbar,\n  ScrollAreaScrollToEnd,\n  ScrollAreaThumb,\n  ScrollAreaViewport,\n} from \"@ui-sdk/react/scroll-area\"\nimport \"@/registry/cyber/ui/cyber/button.css\"\nimport \"@/registry/cyber/ui/cyber/terminal.css\"\n\ntype Kind = \"sys\" | \"agent\" | \"user\" | \"ok\" | \"warn\" | \"err\"\n\ntype TerminalLine = {\n  id: string\n  kind: Kind\n  // Printed as written: it wraps, and its line breaks and leading spaces are\n  // kept, so a caller can indent sub-lines.\n  text: string\n  // The time as it's printed (\"21:04\", \"00:12.4\").\n  time?: string\n}\n\n// What kind of line it is, as the word printed before it, and as the word a\n// screen reader says in its place: \"AGT\" read aloud is three letters.\nconst tags: Record<Kind, string> = { sys: \"SYS\", agent: \"AGT\", user: \"USR\", ok: \"OK\", warn: \"WARN\", err: \"ERR\" }\nconst spoken: Record<Kind, string> = { sys: \"System\", agent: \"Agent\", user: \"User\", ok: \"OK\", warn: \"Warning\", err: \"Error\" }\n\n// How many commands the prompt remembers.\nconst historyLimit = 100\n\n// One line of the log. It's memoized on the line itself, so a log of\n// hundreds renders only what's new when a line arrives.\nconst Line = React.memo(function Line({ line, fresh }: { line: TerminalLine; fresh: boolean }) {\n  return (\n    <div data-kit=\"terminal-line\" data-kind={line.kind} data-new={fresh ? \"\" : undefined}>\n      {line.time != null && <span data-kit=\"terminal-time\">{line.time}</span>}\n      <span data-kit=\"terminal-tag\" aria-hidden>\n        {tags[line.kind]}\n      </span>\n      <span data-sr-only>{spoken[line.kind]}: </span>\n      <span data-kit=\"terminal-text\">{line.text}</span>\n    </div>\n  )\n})\n\n// A console: the log of a session on a dark panel, and under it a prompt to\n// answer with. It follows the newest line as lines arrive, lets go when\n// someone scrolls up to read, and follows again once they're back at the\n// bottom (or press the Latest key that appears meanwhile).\n//\n// - `label` names the log, and is printed over it. The prompt is named\n//   \"<label> command\".\n// - `lines`: `{ id, kind, text, time? }`, oldest first. The kind is printed\n//   as a word as well as a color: SYS, AGT, USR, OK, WARN, ERR.\n// - `onCommand`: with it, the prompt row is there. Enter sends what's typed\n//   and clears the field, which keeps focus; ArrowUp and ArrowDown walk back\n//   and forward through what was sent before.\n// - `prompt`: the glyph before the field, \">\" by default.\n// - `busy`: something is still working. A caret blinks after the last line,\n//   and \"Working\" is said once.\n// - `height`: of the whole console, a number of pixels or any CSS length.\n//\n// New lines are announced politely. A line's text is always whole in the\n// page the moment it arrives; only its entrance is animated, never its text.\nfunction Terminal({\n  label,\n  lines,\n  onCommand,\n  prompt = \">\",\n  busy = false,\n  placeholder,\n  height,\n  style,\n  ...props\n}: Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  label: string\n  lines: TerminalLine[]\n  onCommand?: (text: string) => void\n  prompt?: string\n  busy?: boolean\n  placeholder?: string\n  height?: number | string\n}) {\n  const labelId = React.useId()\n  const input = React.useRef<HTMLInputElement>(null)\n  const [value, setValue] = React.useState(\"\")\n\n  // The commands sent so far, where in them the arrow keys have walked to\n  // (the end is the line being typed), and that line, kept while walking.\n  const history = React.useRef<string[]>([])\n  const cursor = React.useRef(0)\n  const draft = React.useRef(\"\")\n\n  // The line that arrived last, if it arrived after the console was first\n  // shown: only that one tears in. A log that mounts full, or is replaced\n  // wholesale, doesn't animate line by line.\n  const lastId = lines.length ? lines[lines.length - 1].id : undefined\n  const [tail, setTail] = React.useState<{ last: string | undefined; fresh: string | undefined }>({ last: lastId, fresh: undefined })\n  if (tail.last !== lastId) setTail({ last: lastId, fresh: lastId })\n\n  // The time column is as wide as the longest time, counted in characters:\n  // a fixed width, so a line arriving never re-measures the ones before it.\n  const timeWidth = React.useMemo(() => lines.reduce((width, line) => Math.max(width, line.time?.length ?? 0), 0), [lines])\n\n  const submit = (event: React.FormEvent<HTMLFormElement>) => {\n    event.preventDefault()\n    const text = value.trim()\n    if (!text) return\n    const sent = history.current\n    if (sent[sent.length - 1] !== text) sent.push(text)\n    if (sent.length > historyLimit) sent.shift()\n    cursor.current = sent.length\n    draft.current = \"\"\n    setValue(\"\")\n    onCommand?.(text)\n    input.current?.focus()\n  }\n\n  const walk = (event: React.KeyboardEvent<HTMLInputElement>) => {\n    if (event.nativeEvent.isComposing || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return\n    const sent = history.current\n    if (event.key === \"ArrowUp\") {\n      if (!sent.length) return\n      // The caret would jump to the start of the field otherwise.\n      event.preventDefault()\n      if (cursor.current === sent.length) draft.current = value\n      cursor.current = Math.max(0, cursor.current - 1)\n      setValue(sent[cursor.current])\n    } else if (event.key === \"ArrowDown\") {\n      if (cursor.current >= sent.length) return\n      event.preventDefault()\n      cursor.current += 1\n      setValue(cursor.current === sent.length ? draft.current : sent[cursor.current])\n    }\n  }\n\n  return (\n    <div\n      data-kit=\"terminal\"\n      data-busy={busy ? \"\" : undefined}\n      data-times={timeWidth ? \"\" : undefined}\n      style={\n        {\n          \"--terminal-h\": typeof height === \"number\" ? `${height}px` : height,\n          \"--terminal-time\": timeWidth || undefined,\n          ...style,\n        } as React.CSSProperties\n      }\n      {...props}\n    >\n      {/* The frame lights when the log itself has focus, which it takes\n          whenever there's something to scroll. */}\n      <div data-kit=\"terminal-log\" data-frame={onCommand ? \"tr\" : \"tr br\"} data-frame-focus=\"within\">\n        <div data-kit=\"terminal-head\">\n          <span data-kit=\"terminal-lamp\" aria-hidden />\n          <span data-kit=\"terminal-label\" id={labelId}>\n            {label}\n          </span>\n          <span data-micro aria-hidden>\n            TTY-07 // LN {String(lines.length).padStart(3, \"0\")}\n          </span>\n        </div>\n        <ScrollArea data-kit=\"terminal-scroll\" stickToEnd type=\"auto\">\n          <ScrollAreaViewport data-kit=\"terminal-viewport\" role=\"log\" aria-live=\"polite\" aria-labelledby={labelId}>\n            <ScrollAreaContent data-kit=\"terminal-lines\">\n              {lines.map((line) => (\n                <Line key={line.id} line={line} fresh={line.id === tail.fresh} />\n              ))}\n              {busy && (\n                <div data-kit=\"terminal-busy\">\n                  <span data-kit=\"terminal-caret\" data-motion=\"essential\" aria-hidden />\n                  <span data-sr-only>Working</span>\n                </div>\n              )}\n            </ScrollAreaContent>\n          </ScrollAreaViewport>\n          <ScrollAreaScrollbar data-kit=\"terminal-scrollbar\" orientation=\"vertical\">\n            <ScrollAreaThumb data-kit=\"terminal-thumb\" />\n          </ScrollAreaScrollbar>\n          {/* Only there while the view is away from the end. */}\n          <span data-kit=\"terminal-latest\">\n            <ScrollAreaScrollToEnd data-kit=\"button\" data-frame=\"notch br\" data-press-flash=\"\" data-tone=\"signal\" data-size=\"sm\">\n              <ChevronsDown aria-hidden />\n              Latest\n              <span data-sr-only> in {label}</span>\n            </ScrollAreaScrollToEnd>\n          </span>\n        </ScrollArea>\n      </div>\n      {onCommand && (\n        <form data-kit=\"terminal-prompt\" data-frame=\"br\" data-frame-focus=\"within\" onSubmit={submit}>\n          {/* A label, so a press anywhere on the row lands in the field. */}\n          <label data-kit=\"terminal-field\">\n            <span data-kit=\"terminal-glyph\" aria-hidden>\n              {prompt}\n            </span>\n            <input\n              ref={input}\n              data-kit=\"terminal-input\"\n              type=\"text\"\n              value={value}\n              onChange={(event) => setValue(event.target.value)}\n              onKeyDown={walk}\n              aria-label={`${label} command`}\n              placeholder={placeholder}\n              autoComplete=\"off\"\n              autoCapitalize=\"none\"\n              autoCorrect=\"off\"\n              spellCheck={false}\n              enterKeyHint=\"send\"\n            />\n          </label>\n        </form>\n      )}\n    </div>\n  )\n}\n\nexport { Terminal }\nexport type { TerminalLine }\n",
      "type": "registry:ui",
      "target": "@ui/terminal.tsx"
    },
    {
      "path": "registry/cyber/ui/cyber/terminal.css",
      "content": "@layer ui-sdk, cyber.tokens, cyber.base, cyber.ui, cyber.blocks, cyber.fx, cyber.a11y;\n\n@layer cyber.ui {\n  /* A column: the log's frame, and under it the prompt's. The console is a\n     size container, so its type and padding can step down in a narrow\n     column without the page's width coming into it. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal\"] {\n    /* A shade under the panels around it: a screen, not a card. */\n    --terminal-fill: color-mix(in srgb, var(--cy-void) 60%, var(--cy-panel));\n    --terminal-text: var(--cy-text-sm);\n    --terminal-pad: 12px;\n\n    container: cy-terminal / inline-size;\n    display: flex;\n    flex-direction: column;\n    gap: 4px;\n    width: 100%;\n    min-width: 0;\n    height: var(--terminal-h, 280px);\n    font-family: var(--cy-mono);\n    /* A log is read character by character: => stays two characters. */\n    font-variant-ligatures: none;\n    text-align: start;\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-log\"] {\n    --frame-cut: var(--cy-cut);\n    --frame-line: var(--cy-line-deco);\n    --frame-fill: var(--terminal-fill);\n    --frame-image: none;\n\n    flex: 1 1 auto;\n    display: flex;\n    flex-direction: column;\n    min-width: 0;\n    min-height: 0;\n  }\n\n  /* What the log is called, with a lamp before it and a line count after. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-head\"] {\n    flex: none;\n    display: flex;\n    align-items: center;\n    gap: 8px;\n    min-height: 28px;\n    /* The trailing end clears the frame's cut corner. */\n    padding-block: 4px;\n    padding-inline: var(--terminal-pad) calc(var(--cy-cut) + 8px);\n    border-block-end: 1px solid var(--cy-line-deco);\n\n    & > [data-micro] {\n      flex: none;\n      margin-inline-start: auto;\n    }\n  }\n\n  /* Hollow while idle, solid in the signal color while something works.\n     The caret and the word \"Working\" say the same, so it's only a lamp. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-lamp\"] {\n    flex: none;\n    width: 7px;\n    height: 7px;\n    border: 1px solid var(--cy-line-ctl);\n    transition:\n      background-color var(--cy-t-fast) linear,\n      border-color var(--cy-t-fast) linear;\n\n    [data-busy] & {\n      border-color: var(--cy-signal);\n      background: var(--cy-signal);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-label\"] {\n    min-width: 0;\n    font-family: var(--cy-font);\n    font-size: var(--cy-text-sm);\n    font-weight: 600;\n    line-height: 1.2;\n    letter-spacing: var(--cy-track);\n    text-transform: uppercase;\n    overflow-wrap: anywhere;\n    color: var(--cy-ink);\n  }\n\n  /* The scroll area takes what the head leaves. Its viewport is the real\n     scroller; the SDK keeps the end in view and makes it a tab stop while\n     there's somewhere to scroll to. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-scroll\"] {\n    position: relative;\n    flex: 1 1 auto;\n    min-width: 0;\n    min-height: 0;\n\n    /* A shade along the top while there's more above: the log is cut off\n       there, not finished. It only switches, so scrolling repaints nothing\n       else. */\n    &::after {\n      content: \"\";\n      position: absolute;\n      inset-block-start: 0;\n      inset-inline: 0 12px;\n      height: 14px;\n      background: linear-gradient(to bottom, var(--terminal-fill), transparent);\n      opacity: 0;\n      pointer-events: none;\n      transition: opacity var(--cy-t) linear;\n    }\n    &[data-overflow-y]:not([data-at-y-start])::after {\n      opacity: 1;\n    }\n  }\n\n  /* The frame shows focus for the viewport (data-frame-focus=\"within\"), so\n     the viewport's own ring is cut off rather than drawn twice. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-viewport\"] {\n    width: 100%;\n    height: 100%;\n    clip-path: inset(0);\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-lines\"] {\n    min-width: 0;\n    /* The trailing end leaves the scrollbar its lane. */\n    padding-block: 8px 10px;\n    padding-inline: var(--terminal-pad) calc(var(--terminal-pad) + 8px);\n    font-size: var(--terminal-text);\n    line-height: 1.5;\n    /* A line tearing in moves sideways for a moment; that mustn't give the\n       log something to scroll to. */\n    overflow-x: clip;\n  }\n\n  /* A line: its time, its kind as a word, and its text. The columns are\n     fixed widths counted in characters, not measured from the content, and\n     each line is laid out on its own, so one arriving costs one line of\n     layout however long the log is. */\n  [data-skin=\"cyber\"] :is([data-kit=\"terminal-line\"], [data-kit=\"terminal-busy\"]) {\n    --kind: var(--cy-ink-mute);\n\n    display: grid;\n    grid-template-columns: [tag] 4ch [text] minmax(0, 1fr);\n    column-gap: 1ch;\n    align-items: baseline;\n    padding-block: 1px;\n    contain: layout style;\n    color: var(--kind);\n\n    [data-times] & {\n      grid-template-columns: [time] calc(var(--terminal-time, 5) * 1ch) [tag] 4ch [text] minmax(0, 1fr);\n    }\n  }\n\n  /* Each kind has its color, and its word says the same. System lines\n     recede, the agent speaks in the signal color, the person in near-white. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-line\"] {\n    &[data-kind=\"agent\"] {\n      --kind: var(--cy-signal);\n    }\n    &[data-kind=\"user\"] {\n      --kind: var(--cy-ink-hi);\n    }\n    &[data-kind=\"ok\"] {\n      --kind: var(--cy-ok);\n    }\n    &[data-kind=\"warn\"] {\n      --kind: var(--cy-flag);\n    }\n    &[data-kind=\"err\"] {\n      --kind: var(--cy-ink);\n    }\n\n    /* The newest line switches on. One line, once; the rest never move. */\n    &[data-new] {\n      animation: var(--cy-fx-in) 220ms var(--cy-ease-snap) backwards;\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-time\"] {\n    grid-column: time;\n    font-variant-numeric: tabular-nums;\n    white-space: nowrap;\n    color: var(--cy-ink-mute);\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-tag\"] {\n    grid-column: tag;\n    justify-self: start;\n    font-weight: 700;\n    white-space: nowrap;\n    user-select: none;\n\n    /* An error is in the accent, which is also the resting color of the\n       whole kit: its word sits on a lit fill, so it can't be missed or\n       mistaken. The fill is a shadow, so the column keeps its width. */\n    [data-kind=\"err\"] > & {\n      color: var(--cy-ink-hi);\n      background: var(--cy-fill-active);\n      box-shadow: 0 0 0 2px var(--cy-fill-active);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-text\"] {\n    grid-column: text;\n    min-width: 0;\n    white-space: pre-wrap;\n    overflow-wrap: anywhere;\n  }\n\n  /* Still working: a caret where the next line's text will start. It\n     breathes rather than blinks, so it's there at every glance. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-caret\"] {\n    grid-column: text;\n    align-self: center;\n    width: 1ch;\n    height: 1.15em;\n    margin-block: 0.175em;\n    background: var(--cy-signal);\n    animation: var(--cy-fx-pulse) 1.6s var(--cy-ease-snap) infinite;\n  }\n\n  /* The scrollbar: a thin lane down the trailing edge, there whenever the\n     log is longer than its window. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-scrollbar\"] {\n    width: 10px;\n    padding: 2px;\n    opacity: 0;\n    transition: opacity var(--cy-t-fast) linear;\n\n    &[data-visible] {\n      opacity: 1;\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-thumb\"] {\n    width: 100%;\n    min-height: 24px;\n    background: var(--cy-line-ctl);\n    transition: background-color var(--cy-t-fast) linear;\n\n    @media (hover: hover) {\n      &:hover {\n        background: var(--cy-line-hot);\n      }\n    }\n    &[data-dragging] {\n      background: var(--cy-line-hot);\n    }\n  }\n\n  /* The key back to the newest line. It stands on a patch of the log's own\n     fill, so the text passing under it doesn't show through. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-latest\"] {\n    position: absolute;\n    inset-block-end: 8px;\n    inset-inline-end: 18px;\n    display: flex;\n    background: var(--terminal-fill);\n    opacity: 0;\n    pointer-events: none;\n    transition: opacity var(--cy-t-fast) linear;\n\n    &:has(> [data-visible]) {\n      opacity: 1;\n      pointer-events: auto;\n    }\n  }\n\n  /* The prompt: a control of its own under the log, with the kit's state\n     language. Idle its edge is the control line, hovered it's hot, and with\n     the field focused the frame draws the ring and takes the hover fill. */\n  [data-skin=\"cyber\"] [data-kit=\"terminal-prompt\"] {\n    --frame-cut: var(--cy-cut-sm);\n    --frame-line: var(--cy-line-ctl);\n    --frame-fill: var(--terminal-fill);\n    --frame-image: none;\n\n    flex: none;\n    display: flex;\n    min-width: 0;\n    transition:\n      background-color var(--cy-t-fast) linear,\n      border-color var(--cy-t-fast) linear;\n\n    @media (hover: hover) {\n      &:hover {\n        --frame-line: var(--cy-line-hot);\n      }\n    }\n\n    &:focus-within {\n      --frame-fill: var(--cy-fill-hover);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-field\"] {\n    flex: 1;\n    display: flex;\n    align-items: center;\n    gap: 1ch;\n    min-width: 0;\n    min-height: calc(var(--cy-h) - 2px);\n    padding-inline: var(--terminal-pad) calc(var(--cy-cut-sm) + 6px);\n    font-size: var(--terminal-text);\n    cursor: text;\n\n    /* On touch the kit sets every field at 16px, so iOS doesn't zoom into\n       it; the glyph steps up beside it. */\n    @media (pointer: coarse) {\n      font-size: var(--cy-text-md);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-glyph\"] {\n    flex: none;\n    font-weight: 700;\n    color: var(--cy-ink);\n    user-select: none;\n    transition: color var(--cy-t-fast) linear;\n\n    [data-kit=\"terminal-prompt\"]:focus-within & {\n      color: var(--cy-signal);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"terminal-input\"] {\n    flex: 1;\n    width: 100%;\n    min-width: 0;\n    min-height: calc(var(--cy-h) - 2px);\n    padding: 0;\n    border: 0;\n    background: none;\n    color: var(--cy-ink-hi);\n    caret-color: var(--cy-signal);\n    /* The prompt's frame shows focus for the field, so the field's own\n       ring is cut off rather than drawn twice. */\n    clip-path: inset(0);\n\n    &::placeholder {\n      color: var(--cy-ink-mute);\n      opacity: 1;\n    }\n  }\n\n  /* A narrow column (256px is the least the kit asks for): the type steps\n     down, the head keeps only its name, and a line stops being three\n     columns. Its time and its word lead the text and the text wraps back\n     under them with a small hanging indent, which gives a 200px log 27\n     characters to a row where the columns would leave it 16. */\n  @container cy-terminal (max-width: 351px) {\n    [data-skin=\"cyber\"] :is([data-kit=\"terminal-log\"], [data-kit=\"terminal-prompt\"]) {\n      --terminal-text: var(--cy-text-xs);\n      --terminal-pad: 9px;\n    }\n\n    [data-skin=\"cyber\"] [data-kit=\"terminal-head\"] > [data-micro] {\n      display: none;\n    }\n\n    [data-skin=\"cyber\"] :is([data-kit=\"terminal-line\"], [data-kit=\"terminal-busy\"]) {\n      display: block;\n      padding-inline-start: 2ch;\n      text-indent: -2ch;\n    }\n\n    [data-skin=\"cyber\"] :is([data-kit=\"terminal-time\"], [data-kit=\"terminal-tag\"], [data-kit=\"terminal-caret\"]) {\n      display: inline-block;\n      margin-inline-end: 1ch;\n      text-indent: 0;\n      vertical-align: baseline;\n    }\n\n    /* A line with no time, in a log that has them: its word still starts\n       where the others do. */\n    [data-skin=\"cyber\"] [data-times] [data-kit=\"terminal-tag\"]:first-child {\n      margin-inline-start: calc((var(--terminal-time, 5) + 1) * 1ch);\n    }\n\n    [data-skin=\"cyber\"] [data-kit=\"terminal-caret\"] {\n      margin-block: 0;\n      vertical-align: text-bottom;\n    }\n  }\n}\n",
      "type": "registry:file",
      "target": "@ui/cyber/terminal.css"
    }
  ],
  "type": "registry:ui"
}
