Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(trail): picker redraw counts wrapped rows, so no stray title in an 80-column terminal

Rushabh Nagda337725ea7fac
diff --git a/src/cli-picker.ts b/src/cli-picker.ts
index cd96f5bb..bee6a483 100644
--- a/src/cli-picker.ts
+++ b/src/cli-picker.ts
@@ -352,18 +352,61 @@ export async function runPicker(
return state ? { hosts: pickedHostIds(state), telemetry: pickedTelemetry(state) } : null;
}
+/** Columns a character takes on a terminal: 2 for East Asian wide characters and emoji, else 1. */
+function charWidth(cp: number): number {
+ return (cp >= 0x1100 && cp <= 0x115f) ||
+ (cp >= 0x2e80 && cp <= 0xa4cf) ||
+ (cp >= 0xac00 && cp <= 0xd7a3) ||
+ (cp >= 0xf900 && cp <= 0xfaff) ||
+ (cp >= 0xfe30 && cp <= 0xfe4f) ||
+ (cp >= 0xff00 && cp <= 0xff60) ||
+ (cp >= 0xffe0 && cp <= 0xffe6) ||
+ (cp >= 0x1f300 && cp <= 0x1faff) ||
+ (cp >= 0x20000 && cp <= 0x3fffd)
+ ? 2
+ : 1;
+}
+
+/** How many columns `line` takes on screen: colour codes take none. */
+export function displayWidth(line: string): number {
+ let w = 0;
+ for (const ch of line.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '')) w += charWidth(ch.codePointAt(0)!);
+ return w;
+}
+
+/**
+ * How many terminal rows `text` (followed by a newline) takes at `columns`
+ * wide. A line longer than the terminal wraps onto more rows, and the redraw
+ * has to move up over all of them: counting lines instead left the top of the
+ * picker (its title) behind on every redraw in an 80-column terminal.
+ */
+export function renderedRows(text: string, columns: number): number {
+ const cols = columns > 0 ? columns : 80;
+ return text.split('\n').reduce((n, line) => n + Math.max(1, Math.ceil(displayWidth(line) / cols)), 0);
+}
+
+/** The terminal a picker draws on and reads keys from; the real one unless a test hands in its own. */
+export interface PickerIO {
+ out: { write(s: string): unknown; columns?: number };
+ stdin: NodeJS.EventEmitter & { isRaw?: boolean; setRawMode?: (raw: boolean) => unknown; resume(): unknown; pause(): unknown };
+}
+
/** Run a picker on the terminal until enter or esc: the final state, or null when cancelled. */
-async function drivePicker(initial: PickerState, title?: string | null): Promise<PickerState | null> {
+export async function drivePicker(
+ initial: PickerState,
+ title?: string | null,
+ io: PickerIO = { out: process.stderr, stdin: process.stdin },
+): Promise<PickerState | null> {
let state = initial;
- const out = process.stderr;
- const stdin = process.stdin;
+ const { out, stdin } = io;
+ // Rows on screen, not lines: a line wider than the terminal wraps.
let lastLines = 0;
const draw = () => {
if (lastLines > 0) out.write(`\x1b[${lastLines}A\x1b[0J`);
const text = renderPicker(state, true, { title });
out.write(`${text}\n`);
- lastLines = text.split('\n').length;
+ lastLines = renderedRows(text, out.columns ?? 80);
};
const wasRaw = Boolean(stdin.isRaw);
diff --git a/test/cli-picker.test.ts b/test/cli-picker.test.ts
index d79ad09e..a207c714 100644
--- a/test/cli-picker.test.ts
+++ b/test/cli-picker.test.ts
@@ -27,7 +27,11 @@ import {
type PickerKey,
keptLearnings,
learningsPickerState,
+ displayWidth,
+ drivePicker,
+ renderedRows,
} from '../src/cli-picker.js';
+import { EventEmitter } from 'node:events';
function fresh(): string { return mkdtempSync(join(tmpdir(), 'graft-picker-')); }
@@ -368,3 +372,85 @@ test('a learning from PR reviews says how many PRs flagged it, in the same list
assert.match(shown, /\[x\] Use pnpm, not npm\s+for your team · told Claude in 6 sessions/);
assert.match(shown, /\[x\] Return a 500 when db\.query fails\s+for your team · flagged in review on 3 PRs/);
});
+
+/**
+ * Just enough of a terminal to watch a redraw: `columns` wide, lines wrap
+ * (with xterm's deferred wrap at the last column), `\n` starts a new row,
+ * `ESC[nA` moves up, `ESC[0J` clears to the end of the screen, colours are
+ * ignored. `screen()` is what a person would see.
+ */
+function terminal(columns: number) {
+ const rows: string[][] = [[]];
+ let row = 0;
+ let col = 0;
+ const at = (r: number) => (rows[r] ??= []);
+ return {
+ columns,
+ write(s: string) {
+ for (let i = 0; i < s.length; ) {
+ const csi = /^\x1b\[([0-9;?]*)([A-Za-z])/.exec(s.slice(i));
+ if (csi) {
+ const n = Number(csi[1] || '1');
+ if (csi[2] === 'A') { row = Math.max(0, row - n); col = Math.min(col, columns - 1); }
+ if (csi[2] === 'J') { at(row).length = Math.min(at(row).length, col); rows.length = row + 1; }
+ i += csi[0].length;
+ continue;
+ }
+ const ch = String.fromCodePoint(s.codePointAt(i)!);
+ i += ch.length;
+ if (ch === '\n') { row++; col = 0; at(row); continue; }
+ const w = displayWidth(ch);
+ if (col + w > columns) { row++; col = 0; }
+ at(row)[col] = ch;
+ if (w === 2) at(row)[col + 1] = '';
+ col += w;
+ }
+ return true;
+ },
+ screen(): string[] {
+ return rows.map((r) => Array.from(r, (c) => c ?? ' ').join('').trimEnd());
+ },
+ };
+}
+
+function fakeStdin() {
+ return Object.assign(new EventEmitter(), {
+ isRaw: false,
+ setRawMode(raw: boolean) { this.isRaw = raw; },
+ resume() {},
+ pause() {},
+ });
+}
+
+test('renderedRows counts the rows a wrapped line takes, not just the lines', () => {
+ assert.equal(renderedRows('a\nb', 80), 2);
+ assert.equal(renderedRows('x'.repeat(80), 80), 1, 'exactly the width is one row');
+ assert.equal(renderedRows('x'.repeat(81), 80), 2);
+ assert.equal(renderedRows(`\x1b[2m${'x'.repeat(80)}\x1b[0m`, 80), 1, 'colour codes take no room');
+ assert.equal(renderedRows('', 80), 1);
+ assert.equal(renderedRows('漢'.repeat(41), 80), 2, 'wide characters take two columns');
+});
+
+test('in an 80-column terminal, redrawing the picker over a wrapped line leaves no copy of its title behind', async () => {
+ const repo = fresh(); const home = fresh(); // nothing installed: the "+ N not detected (…)" line is long
+ const state = initialPickerState(planInit(repo, { home }), repo, home);
+ const text = renderPicker(state, true);
+ assert.ok(text.split('\n').some((l) => displayWidth(l) > 80), 'a line wider than 80 columns, so it wraps');
+
+ const term = terminal(80);
+ term.write('$ graft init\n');
+ const stdin = fakeStdin();
+ const picked = drivePicker(state, undefined, { out: term, stdin });
+ const titles = () => term.screen().filter((l) => l.includes('pick the agents your team uses')).length;
+ assert.equal(titles(), 1);
+ for (const key of ['\x1b[B', '\x1b[B', '\x1b[A', 'm', 'm', ' ']) {
+ stdin.emit('data', Buffer.from(key));
+ assert.equal(titles(), 1, `one title after ${JSON.stringify(key)}`);
+ assert.equal(term.screen()[0], '$ graft init', 'what was above the picker stays');
+ }
+ stdin.emit('data', Buffer.from('\r'));
+ assert.ok(await picked);
+ // Done, the picker erases itself completely.
+ assert.equal(titles(), 0);
+ assert.deepEqual(term.screen().filter(Boolean), ['$ graft init']);
+});