|
| 1 | +// debug-terminfo.ts — pinpoint where the terminfo capability chain breaks. |
| 2 | +// Run directly in the terminal you are testing, from the repo root: |
| 3 | +// |
| 4 | +// deno run -A debug-terminfo.ts |
| 5 | +// node debug-terminfo.ts |
| 6 | +// |
| 7 | +// Walks the full chain and reports which link fails: environment identity, |
| 8 | +// detection (static Capabilities), the probe round-trip with a per-query |
| 9 | +// ✓/✗ breakdown, the parser's CapabilityEvents, and the term's merged |
| 10 | +// RuntimeCapabilities. The pointer shape section at the end exercises the |
| 11 | +// one capability render() consumes today; if no OSC 22 reply arrives, a |
| 12 | +// set-only check flashes the hand cursor so query-less terminals (ghostty) |
| 13 | +// can be told apart from terminals without OSC 22 at all. |
| 14 | +import { Buffer } from "node:buffer"; |
| 15 | +import process from "node:process"; |
| 16 | +import { |
| 17 | + type CapabilityEvent, |
| 18 | + close, |
| 19 | + createInput, |
| 20 | + createTerm, |
| 21 | + detectTerminal, |
| 22 | + open, |
| 23 | + text, |
| 24 | +} from "./mod.ts"; |
| 25 | + |
| 26 | +const decode = new TextDecoder(); |
| 27 | +const esc = (b: Uint8Array) => JSON.stringify(decode.decode(b)).slice(1, -1); |
| 28 | +const json = (v: unknown) => JSON.stringify(v); |
| 29 | + |
| 30 | +console.log("── environment"); |
| 31 | +console.log(" TERM:", process.env.TERM ?? "(unset)"); |
| 32 | +console.log( |
| 33 | + " TERM_PROGRAM:", |
| 34 | + process.env.TERM_PROGRAM ?? "(unset)", |
| 35 | + process.env.TERM_PROGRAM_VERSION ?? "", |
| 36 | +); |
| 37 | +console.log(" COLORTERM:", process.env.COLORTERM ?? "(unset)"); |
| 38 | +if (process.env.TMUX) { |
| 39 | + console.log(" ⚠ running inside tmux — queries may not pass through"); |
| 40 | +} |
| 41 | + |
| 42 | +const info = await detectTerminal(); |
| 43 | +console.log("── detection"); |
| 44 | +console.log(" probe bytes:", info.probe.length); |
| 45 | +console.log(" static capabilities:", json(info.capabilities)); |
| 46 | + |
| 47 | +if (!process.stdin.isTTY) { |
| 48 | + console.log(" ✗ stdin is not a TTY; run this directly in your terminal"); |
| 49 | + process.exit(1); |
| 50 | +} |
| 51 | + |
| 52 | +console.log("── probing (2s window, raw mode on; type nothing)…"); |
| 53 | +if (typeof process.stdin.setRawMode === "function") { |
| 54 | + process.stdin.setRawMode(true); |
| 55 | +} |
| 56 | +process.stdout.write(info.probe); |
| 57 | + |
| 58 | +const chunks: Buffer[] = []; |
| 59 | +const onData = (chunk: Buffer) => chunks.push(chunk); |
| 60 | +process.stdin.on("data", onData); |
| 61 | +await new Promise((r) => setTimeout(r, 2000)); |
| 62 | +process.stdin.removeListener("data", onData); |
| 63 | + |
| 64 | +const reply = Buffer.concat(chunks); |
| 65 | +console.log("── raw reply"); |
| 66 | +console.log(" bytes:", reply.length); |
| 67 | +console.log( |
| 68 | + " escaped:", |
| 69 | + reply.length ? esc(reply) : "(nothing — the terminal answered nothing)", |
| 70 | +); |
| 71 | + |
| 72 | +console.log("── which queries were answered"); |
| 73 | +const replyText = decode.decode(reply); |
| 74 | +const markers: [string, string][] = [ |
| 75 | + ["foreground (OSC 10)", "\x1b]10;"], |
| 76 | + ["background (OSC 11)", "\x1b]11;"], |
| 77 | + ["cursor color (OSC 12)", "\x1b]12;"], |
| 78 | + ["colors (OSC 21)", "\x1b]21;"], |
| 79 | + ["POINTER SHAPE (OSC 22)", "\x1b]22;"], |
| 80 | + ["XTGETTCAP (DCS)", "\x1bP"], |
| 81 | + ["sync output (DECRPM 2026)", "?2026"], |
| 82 | + ["kitty keyboard (?u)", "?u"], |
| 83 | + ["kitty graphics (APC)", "\x1b_G"], |
| 84 | +]; |
| 85 | +for (const [name, marker] of markers) { |
| 86 | + console.log(` ${replyText.includes(marker) ? "✓" : "✗"} ${name}`); |
| 87 | +} |
| 88 | + |
| 89 | +// The query tells us about reply support, not set support. Ghostty sets |
| 90 | +// shapes since 1.0 but never answers the query, so give the terminal a |
| 91 | +// chance to show what it can do: set the hand for 1.5s, then restore. |
| 92 | +if (!replyText.includes("\x1b]22;")) { |
| 93 | + console.log("── set-only check (watch your cursor)"); |
| 94 | + process.stdout.write("\x1b]22;pointer\x1b\\"); |
| 95 | + await new Promise((r) => setTimeout(r, 1500)); |
| 96 | + process.stdout.write("\x1b]22;default\x1b\\"); |
| 97 | + console.log( |
| 98 | + " the cursor should have flashed to a hand just now. If it did," + |
| 99 | + " this terminal can SET shapes but never answers the query" + |
| 100 | + " (ghostty behaves exactly like this) — detectTerminal()'s" + |
| 101 | + " known-support table should already cover it. If it did not" + |
| 102 | + " change, OSC 22 is not supported at all.", |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +if (typeof process.stdin.setRawMode === "function") { |
| 107 | + process.stdin.setRawMode(false); |
| 108 | +} |
| 109 | + |
| 110 | +console.log("── parser"); |
| 111 | +const input = await createInput({ terminfo: info }); |
| 112 | +let events = input.scan(new Uint8Array(reply)).events; |
| 113 | +events = [...events, ...input.scan(new Uint8Array(0)).events]; |
| 114 | +const caps = events.filter((e): e is CapabilityEvent => |
| 115 | + e.type === "capability" |
| 116 | +); |
| 117 | +if (caps.length === 0) { |
| 118 | + console.log(" (no capability events parsed)"); |
| 119 | +} |
| 120 | +for (const c of caps) { |
| 121 | + console.log(` capability: ${c.key} = ${json(c.value)}`); |
| 122 | +} |
| 123 | + |
| 124 | +console.log("── term (merged view)"); |
| 125 | +const term = await createTerm({ width: 40, height: 6, terminfo: info }); |
| 126 | +term.update(caps); |
| 127 | +console.log(" merged capabilities:", json(term.capabilities)); |
| 128 | +const staticCaps = info.capabilities as unknown as Record<string, unknown>; |
| 129 | +const mergedCaps = term.capabilities as unknown as Record<string, unknown>; |
| 130 | +const raised = Object.keys(mergedCaps).filter((k) => |
| 131 | + json(mergedCaps[k]) !== json(staticCaps[k]) |
| 132 | +); |
| 133 | +console.log( |
| 134 | + " raised by the probe:", |
| 135 | + raised.length > 0 ? raised.join(", ") : "(nothing — probe added no evidence)", |
| 136 | +); |
| 137 | + |
| 138 | +// Pointer shape is the one capability render() consumes today, so it doubles |
| 139 | +// as the end-to-end emission check. |
| 140 | +console.log("── pointer shape (renderer-spec §7.10)"); |
| 141 | +const result = term.render( |
| 142 | + [open("box", { cursor: "pointer" }), text("hover"), close()], |
| 143 | + { pointer: { x: 2, y: 2, down: false } }, |
| 144 | +); |
| 145 | +const emitted = decode |
| 146 | + .decode(result.output) |
| 147 | + .includes("\x1b]22;pointer\x1b\\"); |
| 148 | +console.log(" OSC 22 in render output:", emitted); |
| 149 | + |
| 150 | +console.log("── verdict"); |
| 151 | +if (emitted) { |
| 152 | + console.log( |
| 153 | + " ✓ render emits OSC 22" + |
| 154 | + (info.capabilities.pointerShape && !replyText.includes("\x1b]22;") |
| 155 | + ? " — statically granted by the known-support table (no query reply," + |
| 156 | + " which is expected for set-only terminals like ghostty)" |
| 157 | + : " — the probe confirmed support and the parser folded it in") + |
| 158 | + ". If the demos still show nothing, hover a key/button — and note the" + |
| 159 | + " shape only changes while the pointer is over an element that" + |
| 160 | + " declares one.", |
| 161 | + ); |
| 162 | +} else if (!replyText.includes("\x1b]22;")) { |
| 163 | + console.log( |
| 164 | + info.capabilities.pointerShape |
| 165 | + ? " ✗ no OSC 22 reply and no emission despite a static grant — please" + |
| 166 | + " report this with the escaped reply above." |
| 167 | + : " ✗ your terminal did not answer the OSC 22 query and it is not on" + |
| 168 | + " the known-support table. If the set-only check above moved your" + |
| 169 | + " cursor, it can set shapes but we cannot detect it — run the demos" + |
| 170 | + " with TTY_POINTER_SHAPES=1 (or nominate the terminal for the" + |
| 171 | + " table). If the cursor never changed, OSC 22 is not supported at" + |
| 172 | + " all (iTerm2, Terminal.app, VS Code, tmux without passthrough).", |
| 173 | + ); |
| 174 | +} else if (caps.length === 0) { |
| 175 | + console.log( |
| 176 | + " ✗ the terminal replied but the parser produced no capability events." + |
| 177 | + " Please report this with the escaped reply above.", |
| 178 | + ); |
| 179 | +} else { |
| 180 | + console.log( |
| 181 | + " ✗ capability recognized but render did not emit OSC 22. Please report" + |
| 182 | + " this with the escaped reply above.", |
| 183 | + ); |
| 184 | +} |
| 185 | +process.exit(0); |
0 commit comments