File size: 7,336 Bytes
7e3630c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as fs from "node:fs";

const HIDDEN_MODE = "\x1b[8m";
const CLEAR_LINE_AND_RETURN = "\x1b[2K\r";
const RESET_ATTRIBUTES = "\x1b[0m";

const CELL_SIZE_QUERY = "\x1b[16t";
const TERMINAL_SIZE_QUERY = "\x1b[14t";
const KITTY_QUERY = "\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\";
const ITERM_CELL_SIZE_QUERY = "\x1b]1337;ReportCellSize\x07";
const DEVICE_ATTRIBUTES_QUERY = "\x1b[c";

// biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally parsing escape sequence responses
const CELL_SIZE_REGEX = /\x1b\[6;(\d+);(\d+);?t/;
// biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally parsing escape sequence responses
const TERMINAL_SIZE_REGEX = /\x1b\[4;(\d+);(\d+);?t/;
// biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally parsing escape sequence responses
const KITTY_RESPONSE_REGEX = /\x1b_Gi=31;(.+?)\x1b\\/;
const ITERM_CELL_SIZE_REGEX = /ReportCellSize=([\d.]+);([\d.]+);([\d.]+)/;
// biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally parsing escape sequence responses
const DEVICE_ATTRIBUTES_REGEX = /\x1b\[\?(\d+(?:;\d+)*)c/;

const RESPONSE_PATTERNS_FOR_STRIPPING: RegExp[] = [
  // biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally stripping escape sequence responses
  /\x1b\[6;\d+;\d+;?t/g,
  // biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally stripping escape sequence responses
  /\x1b\[4;\d+;\d+;?t/g,
  // biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally stripping escape sequence responses
  /\x1b_Gi=31;.+?\x1b\\/g,
  // biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally stripping escape sequence responses
  /\x1b\]1337;ReportCellSize=[\d.]+;[\d.]+;[\d.]+\x07/g,
  // biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally stripping escape sequence responses
  /\x1b\[\?\d+(?:;\d+)*c/g,
];

export interface TerminalQueryResult {
  cellWidth: number | undefined;
  cellHeight: number | undefined;
  terminalWidth: number | undefined;
  terminalHeight: number | undefined;
  supportsKittyGraphics: boolean;
  supportsSixelGraphics: boolean;
  iterm2CellWidth: number | undefined;
  iterm2CellHeight: number | undefined;
  iterm2Scale: number | undefined;
}

/**
 * Detect terminal capabilities by sending a batch of escape sequence queries
 * and accumulating the responses in a buffer matched by regex.
 *
 * The Device Attributes response (`\x1b[?Nc`) is used as a sentinel to signal
 * end of terminal response, with a 1000ms fallback timeout.
 * The function overrides `stdin.push`, preventing data from being picked up
 * by Ink's `useInput` hook. `stdin.push` is restored on cleanup, and any
 * user input during this period is pushed back into the stream.
 */
export function queryTerminal(
  stdin: NodeJS.ReadStream,
  setRawMode: (value: boolean) => void,
  signal?: AbortSignal,
): Promise<TerminalQueryResult> {
  return new Promise((resolve) => {
    if (signal?.aborted || !process.stdin.isTTY || !process.stdout.isTTY) {
      resolve({
        cellWidth: undefined,
        cellHeight: undefined,
        terminalWidth: undefined,
        terminalHeight: undefined,
        supportsKittyGraphics: false,
        supportsSixelGraphics: false,
        iterm2CellWidth: undefined,
        iterm2CellHeight: undefined,
        iterm2Scale: undefined,
      });
      return;
    }

    setRawMode(true);

    let buffer = "";
    let cellSizeReceived = false;
    let terminalSizeReceived = false;
    let kittyReceived = false;
    let itermCellSizeReceived = false;
    let sentinelReceived = false;
    let timeoutId: NodeJS.Timeout;
    let done = false;

    const result: TerminalQueryResult = {
      cellWidth: undefined,
      cellHeight: undefined,
      terminalWidth: undefined,
      terminalHeight: undefined,
      supportsKittyGraphics: false,
      supportsSixelGraphics: false,
      iterm2CellWidth: undefined,
      iterm2CellHeight: undefined,
      iterm2Scale: undefined,
    };

    const origPush: typeof stdin.push = stdin.push.bind(stdin);

    const cleanup = () => {
      if (done) return;
      done = true;
      if (timeoutId) {
        clearTimeout(timeoutId);
      }
      signal?.removeEventListener("abort", onAbort);
      stdin.push = origPush;
      setRawMode(false);

      let filtered = buffer;
      for (const regex of RESPONSE_PATTERNS_FOR_STRIPPING) {
        filtered = filtered.replace(regex, "");
      }

      if (filtered.length > 0) {
        stdin.push(filtered);
      }

      resolve(result);
    };

    const onAbort = () => {
      cleanup();
    };

    signal?.addEventListener("abort", onAbort, { once: true });

    timeoutId = setTimeout(cleanup, 1000);

    const processChunk = (data: string) => {
      buffer += data;

      if (!cellSizeReceived) {
        const match = buffer.match(CELL_SIZE_REGEX);
        if (match?.[1] && match?.[2]) {
          cellSizeReceived = true;
          const height = parseInt(match[1], 10);
          const width = parseInt(match[2], 10);
          if (!Number.isNaN(height) && !Number.isNaN(width)) {
            result.cellWidth = width;
            result.cellHeight = height;
          }
        }
      }

      if (!terminalSizeReceived) {
        const match = buffer.match(TERMINAL_SIZE_REGEX);
        if (match?.[1] && match?.[2]) {
          terminalSizeReceived = true;
          const height = parseInt(match[1], 10);
          const width = parseInt(match[2], 10);
          if (!Number.isNaN(height) && !Number.isNaN(width)) {
            result.terminalWidth = width;
            result.terminalHeight = height;
          }
        }
      }

      if (!kittyReceived) {
        const match = buffer.match(KITTY_RESPONSE_REGEX);
        if (match?.[1]?.includes("OK")) {
          kittyReceived = true;
          result.supportsKittyGraphics = true;
        }
      }

      if (!itermCellSizeReceived) {
        const match = buffer.match(ITERM_CELL_SIZE_REGEX);
        if (match?.[1] && match?.[2] && match?.[3]) {
          itermCellSizeReceived = true;
          const height = parseFloat(match[1]);
          const width = parseFloat(match[2]);
          const scale = parseFloat(match[3]);
          if (
            !Number.isNaN(height) &&
            !Number.isNaN(width) &&
            !Number.isNaN(scale)
          ) {
            result.iterm2CellWidth = width;
            result.iterm2CellHeight = height;
            result.iterm2Scale = scale;
          }
        }
      }

      if (!sentinelReceived && DEVICE_ATTRIBUTES_REGEX.test(buffer)) {
        sentinelReceived = true;
        const daMatch = buffer.match(DEVICE_ATTRIBUTES_REGEX);
        if (daMatch?.[1]) {
          result.supportsSixelGraphics = daMatch[1].split(";").includes("4");
        }
        cleanup();
      }
    };

    stdin.push = (chunk) => {
      const str = Buffer.isBuffer(chunk) ? chunk.toString() : String(chunk);
      processChunk(str);
      return true;
    };

    const query =
      HIDDEN_MODE +
      CELL_SIZE_QUERY +
      TERMINAL_SIZE_QUERY +
      KITTY_QUERY +
      ITERM_CELL_SIZE_QUERY +
      DEVICE_ATTRIBUTES_QUERY +
      CLEAR_LINE_AND_RETURN +
      RESET_ATTRIBUTES;

    try {
      fs.writeSync(process.stdout.fd, query);
    } catch {
      cleanup();
    }
  });
}