-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingle.js
More file actions
executable file
·651 lines (551 loc) · 19.4 KB
/
single.js
File metadata and controls
executable file
·651 lines (551 loc) · 19.4 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#!/usr/bin/env node
// bukowski - flicker-free terminal viewport for Ink-based CLI apps
const path = require('path');
const { execSync } = require('child_process');
const pty = require('node-pty');
const { Terminal } = require('@xterm/headless');
const { SerializeAddon } = require('@xterm/addon-serialize');
const { VimHandler } = require('./vim');
const { SearchHandler } = require('./search');
// Load quotes from quotes.txt (format: text\n— author\n\n)
const fs = require('fs');
const quotesPath = path.join(__dirname, 'quotes.txt');
let QUOTES = [];
try {
const raw = fs.readFileSync(quotesPath, 'utf8');
QUOTES = raw.split(/\n\n+/).filter(Boolean).map(block => {
const lines = block.trim().split('\n');
const author = lines.pop().replace(/^—\s*/, '');
const text = lines.join(' ');
return { text, author };
});
} catch (e) {
QUOTES = [{ text: "Let there be light.", author: "bukowski" }];
}
function showSplash() {
const quote = QUOTES[Math.floor(Math.random() * QUOTES.length)];
const cols = process.stdout.columns || 80;
const rows = process.stdout.rows || 24;
// Center the quote
const lines = quote.text.match(new RegExp(`.{1,${cols - 4}}(\\s|$)`, 'g')) || [quote.text];
const authorLine = `— ${quote.author}`;
const startRow = Math.floor((rows - lines.length - 2) / 2);
let frame = '\x1b[2J\x1b[H'; // Clear screen
frame += '\x1b[?25l'; // Hide cursor
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
const col = Math.floor((cols - line.length) / 2);
frame += `\x1b[${startRow + i};${col}H\x1b[3m${line}\x1b[0m`; // Italic
}
const authorCol = Math.floor((cols - authorLine.length) / 2);
frame += `\x1b[${startRow + lines.length + 1};${authorCol}H\x1b[2m${authorLine}\x1b[0m`; // Dim
process.stdout.write(frame);
}
// Find Claude CLI
const claudeBin = execSync('readlink -f "$(which claude)"', { encoding: 'utf8' }).trim();
const claudeDir = path.dirname(claudeBin);
const claudePath = path.join(claudeDir, 'cli.js');
// Alt screen buffer
process.stdout.write('\x1b[?1049h');
// Show splash
showSplash();
class Viewport {
constructor() {
const cols = process.stdout.columns || 80;
const rows = parseInt(process.env.BUKOWSKI_ROWS) || (process.stdout.rows || 24);
this.term = new Terminal({
cols,
rows,
scrollback: parseInt(process.env.BUKOWSKI_SCROLLBACK) || 10000,
allowProposedApi: true
});
this.serialize = new SerializeAddon();
this.term.loadAddon(this.serialize);
this.offset = 0;
this.followTail = true;
this.drawScheduled = false;
// Vim mode state
this.mode = 'insert'; // 'insert' | 'normal' | 'visual' | 'vline'
this.commandPending = false; // true after Ctrl+Space, awaiting mode key
this.prevMode = 'insert'; // track where we came from for visual mode
// Normal mode virtual cursor
this.normalCursor = { line: 0, col: 0 };
// Visual mode state (used by VimHandler)
this.visualAnchor = { line: 0, col: 0 };
this.visualCursor = { line: 0, col: 0 };
// Handlers
this.vim = new VimHandler(this);
this.search = new SearchHandler(this);
}
get height() { return Math.max(1, process.stdout.rows - 1); }
get width() { return process.stdout.columns || 80; }
push(data) {
try {
this.term.write(data);
} catch (e) {
// xterm.js can fail on buffer overflow - log but don't crash
process.stderr.write(`\n[viewport] xterm error: ${e.message}\n`);
}
this.scheduleDraw();
}
scheduleDraw() {
// Throttle: draw at most every 16ms (~60fps), always latest frame
if (!this.drawScheduled) {
this.drawScheduled = true;
setTimeout(() => {
this.drawScheduled = false;
if (this.followTail) {
this.scrollToBottom();
}
this.draw();
}, 16);
}
}
get contentHeight() {
const buffer = this.term.buffer.active;
return buffer.baseY + buffer.cursorY + 1;
}
scrollToBottom() {
this.offset = Math.max(0, this.contentHeight - this.height);
}
scroll(n) {
const totalLines = this.contentHeight;
const maxOffset = Math.max(0, totalLines - this.height);
const oldOffset = this.offset;
this.offset = Math.max(0, Math.min(maxOffset, this.offset + n));
this.followTail = (this.offset >= maxOffset);
if (this.offset !== oldOffset) {
this.draw();
}
}
getLine(index) {
const buffer = this.term.buffer.active;
if (index < 0 || index >= buffer.length) return '';
const line = buffer.getLine(index);
if (!line) return '';
let result = '';
let lastSgr = '';
for (let i = 0; i < line.length; i++) {
const cell = line.getCell(i);
if (!cell) break;
const char = cell.getChars();
if (!char) continue;
// Build SGR sequence based on cell attributes
let sgr = [];
// Attributes
if (cell.isBold()) sgr.push(1);
if (cell.isDim()) sgr.push(2);
if (cell.isItalic()) sgr.push(3);
if (cell.isUnderline()) sgr.push(4);
if (cell.isBlink()) sgr.push(5);
if (cell.isInverse()) sgr.push(7);
if (cell.isInvisible()) sgr.push(8);
if (cell.isStrikethrough()) sgr.push(9);
// Foreground color - check color mode (xterm.js uses bit flags)
// 0x0 = default, 0x1000000 = 16-color, 0x2000000 = 256-color, 0x3000000 = RGB
const fgMode = cell.getFgColorMode();
if (fgMode === 0x1000000) {
const fg = cell.getFgColor();
if (fg < 8) sgr.push(30 + fg);
else sgr.push(90 + fg - 8);
} else if (fgMode === 0x2000000) {
sgr.push(38, 5, cell.getFgColor());
} else if (fgMode === 0x3000000) {
// RGB - getFgColor returns packed RGB
const rgb = cell.getFgColor();
const r = (rgb >> 16) & 0xFF;
const g = (rgb >> 8) & 0xFF;
const b = rgb & 0xFF;
sgr.push(38, 2, r, g, b);
}
// Background color
const bgMode = cell.getBgColorMode();
if (bgMode === 0x1000000) {
const bg = cell.getBgColor();
if (bg < 8) sgr.push(40 + bg);
else sgr.push(100 + bg - 8);
} else if (bgMode === 0x2000000) {
sgr.push(48, 5, cell.getBgColor());
} else if (bgMode === 0x3000000) {
const rgb = cell.getBgColor();
const r = (rgb >> 16) & 0xFF;
const g = (rgb >> 8) & 0xFF;
const b = rgb & 0xFF;
sgr.push(48, 2, r, g, b);
}
// Only emit SGR if changed from previous cell
const sgrStr = sgr.join(';');
if (sgrStr !== lastSgr) {
if (sgr.length > 0) {
result += `\x1b[0;${sgrStr}m`;
} else if (lastSgr !== '') {
result += '\x1b[0m';
}
lastSgr = sgrStr;
}
result += char;
}
// Reset at end of line if we had styling
if (lastSgr !== '') {
result += '\x1b[0m';
}
return result.replace(/\s+$/, ''); // Trim trailing whitespace
}
draw() {
const buffer = this.term.buffer.active;
const totalLines = this.contentHeight;
const { height, width, offset } = this;
const maxOffset = Math.max(0, totalLines - height);
// Visual selection
const inVisual = this.mode === 'visual' || this.mode === 'vline';
const anchor = this.visualAnchor;
const cursor = this.visualCursor;
// Determine selection bounds
let selStart, selEnd;
if (inVisual) {
if (anchor.line < cursor.line || (anchor.line === cursor.line && anchor.col <= cursor.col)) {
selStart = anchor;
selEnd = cursor;
} else {
selStart = cursor;
selEnd = anchor;
}
}
// Build visible lines
const visible = [];
for (let i = 0; i < height; i++) {
const lineIdx = offset + i;
let line = lineIdx < totalLines ? this.getLine(lineIdx) : '';
const plainLine = lineIdx < totalLines ? this.getLineText(lineIdx) : '';
if (this.mode === 'normal') {
// NORMAL mode: show virtual cursor + search highlights
if (this.search.matches.length > 0) {
line = this.highlightSearchMatches(lineIdx, line, plainLine);
}
if (lineIdx === this.normalCursor.line) {
line = this.insertCursorMarker(line, plainLine, this.normalCursor.col);
}
} else if (this.mode === 'insert') {
// INSERT mode: only search highlights
if (this.search.matches.length > 0) {
line = this.highlightSearchMatches(lineIdx, line, plainLine);
}
} else if (this.mode === 'vline') {
// V-LINE mode: use character-by-character highlighting (same as visual)
const isSelected = lineIdx >= selStart.line && lineIdx <= selEnd.line;
if (isSelected) {
// Highlight entire line by setting hlStart=0 and hlEnd=line length
const fullLineStart = { line: lineIdx, col: 0 };
const fullLineEnd = { line: lineIdx, col: Math.max(0, plainLine.length - 1) };
line = this.highlightVisualSelection(lineIdx, line, plainLine, fullLineStart, fullLineEnd, cursor);
}
} else if (this.mode === 'visual') {
// VISUAL mode: character-level highlighting
line = this.highlightVisualSelection(lineIdx, line, plainLine, selStart, selEnd, cursor);
}
visible.push(line);
}
// Status bar
const from = totalLines ? offset + 1 : 0;
const to = Math.min(offset + height, totalLines);
const pctStr = offset <= 0 ? 'Top' : (offset >= maxOffset ? 'Bot' : `${Math.round(100 * offset / maxOffset)}%`);
// Mode indicator
const modeNames = { insert: 'INSERT', normal: 'NORMAL', visual: 'VISUAL', vline: 'V-LINE' };
const modeStr = this.commandPending ? '[C-SPC]' : `[${modeNames[this.mode]}]`;
// Search prompt or tips
let leftPart;
if (this.search.isActive) {
leftPart = this.search.getStatus();
} else {
const scrollIndicator = this.followTail ? '' : '[SCROLL] ';
const searchStatus = this.search.getStatus();
let selInfo = '';
if (this.mode === 'normal') {
selInfo = `L${this.normalCursor.line + 1}:C${this.normalCursor.col + 1} `;
} else if (inVisual && selStart && selEnd) {
const lineCount = selEnd.line - selStart.line + 1;
if (this.mode === 'vline') {
selInfo = `${lineCount} line${lineCount > 1 ? 's' : ''} `;
} else {
selInfo = `L${cursor.line + 1}:C${cursor.col + 1} `;
}
}
const tips = inVisual ? 'y:yank ' : '';
leftPart = `${modeStr} ${selInfo}${scrollIndicator}${searchStatus ? searchStatus + ' ' : ''}${tips}Esc:exit`;
}
const pos = `[${from}-${to}/${totalLines}] ${pctStr}`;
const status = ` ${leftPart} ${pos} `;
const statusPadded = status.length > width ? status.slice(-width) : status.padStart(width);
// Render frame with sync update mode (DEC 2026) for flicker-free output
let frame = '\x1b[?2026h'; // Begin sync update
frame += '\x1b[?25l\x1b[H'; // Hide cursor, home
for (let i = 0; i < visible.length; i++) {
frame += `\x1b[${i + 1};1H\x1b[2K${visible[i]}`;
}
frame += `\x1b[${height + 1};1H\x1b[2K\x1b[7m${statusPadded}\x1b[0m`;
frame += '\x1b[?25h';
frame += '\x1b[?2026l'; // End sync update
process.stdout.write(frame);
}
resize(cols, rows) {
this.term.resize(cols, 1000);
this.draw();
}
// Get plain text for a line (no ANSI codes)
getLineText(index) {
const buffer = this.term.buffer.active;
if (index < 0 || index >= buffer.length) return '';
const line = buffer.getLine(index);
if (!line) return '';
let result = '';
for (let i = 0; i < line.length; i++) {
const cell = line.getCell(i);
if (!cell) break;
const char = cell.getChars();
if (char) result += char;
}
return result.replace(/\s+$/, '');
}
// Search delegation
enterSearch() { this.search.enter(); }
nextMatch() { this.search.next(); }
prevMatch() { this.search.prev(); }
// Highlight character-level visual selection
highlightVisualSelection(lineIdx, line, plainLine, selStart, selEnd, cursor) {
if (lineIdx < selStart.line || lineIdx > selEnd.line) {
// Not in selection, but maybe show cursor
if (lineIdx === cursor.line) {
return this.insertCursorMarker(line, plainLine, cursor.col);
}
return line;
}
// Determine highlight range for this line
let hlStart = 0;
let hlEnd = plainLine.length;
if (lineIdx === selStart.line) {
hlStart = selStart.col;
}
if (lineIdx === selEnd.line) {
hlEnd = selEnd.col + 1;
}
// Build highlighted line using plain text positions
// Since line has ANSI codes, we work character by character
let result = '';
let plainIdx = 0;
let i = 0;
while (i < line.length) {
// Check for ANSI escape sequence
if (line[i] === '\x1b') {
const escEnd = line.indexOf('m', i);
if (escEnd !== -1) {
result += line.substring(i, escEnd + 1);
i = escEnd + 1;
continue;
}
}
// Regular character
const char = line[i];
const inHighlight = plainIdx >= hlStart && plainIdx < hlEnd;
const isCursor = lineIdx === cursor.line && plainIdx === cursor.col;
if (isCursor) {
// Cursor: underline + inverse
result += `\x1b[4;7m${char}\x1b[24;27m`;
} else if (inHighlight) {
// Selection: inverse
result += `\x1b[7m${char}\x1b[27m`;
} else {
result += char;
}
plainIdx++;
i++;
}
// If cursor is past end of line, show it
if (lineIdx === cursor.line && cursor.col >= plainLine.length) {
result += `\x1b[4;7m \x1b[24;27m`;
}
return result;
}
// Highlight search matches on a line
highlightSearchMatches(lineIdx, line, plainLine) {
const matches = this.search.matches.filter(m => m.line === lineIdx);
if (matches.length === 0) return line;
const currentMatch = this.search.matches[this.search.index];
const isCurrentLine = currentMatch && currentMatch.line === lineIdx;
let result = '';
let plainIdx = 0;
let i = 0;
while (i < line.length) {
// Skip ANSI escape sequences
if (line[i] === '\x1b') {
const escEnd = line.indexOf('m', i);
if (escEnd !== -1) {
result += line.substring(i, escEnd + 1);
i = escEnd + 1;
continue;
}
}
// Check if this position is in a match
let inMatch = false;
let isCurrentMatchPos = false;
for (const m of matches) {
if (plainIdx >= m.col && plainIdx < m.col + m.length) {
inMatch = true;
if (isCurrentLine && currentMatch && m.col === currentMatch.col) {
isCurrentMatchPos = true;
}
break;
}
}
if (isCurrentMatchPos) {
// Current match: bright yellow bg + black fg
result += `\x1b[30;103m${line[i]}\x1b[0m`;
} else if (inMatch) {
// Other matches: yellow bg
result += `\x1b[43m${line[i]}\x1b[49m`;
} else {
result += line[i];
}
plainIdx++;
i++;
}
return result;
}
// Insert cursor marker at position (for lines not in selection)
insertCursorMarker(line, plainLine, col) {
let result = '';
let plainIdx = 0;
let i = 0;
while (i < line.length) {
if (line[i] === '\x1b') {
const escEnd = line.indexOf('m', i);
if (escEnd !== -1) {
result += line.substring(i, escEnd + 1);
i = escEnd + 1;
continue;
}
}
if (plainIdx === col) {
result += `\x1b[4;7m${line[i]}\x1b[24;27m`;
} else {
result += line[i];
}
plainIdx++;
i++;
}
if (col >= plainLine.length) {
result += `\x1b[4;7m \x1b[24;27m`;
}
return result;
}
}
// Cleanup
function cleanup() {
process.stdout.write('\x1b[?1000l\x1b[?1006l');
process.stdout.write('\x1b[?25h');
process.stdout.write('\x1b[?1049l');
}
process.on('exit', cleanup);
// Delay startup to show splash
const SPLASH_DURATION = 2000; // 2 seconds
const VIRTUAL_ROWS = parseInt(process.env.BUKOWSKI_ROWS) || (process.stdout.rows || 24);
setTimeout(() => {
const vp = new Viewport();
// Spawn Claude with large virtual terminal (matches xterm buffer)
const claude = pty.spawn('node', [claudePath, ...process.argv.slice(2)], {
name: 'xterm-256color',
cols: process.stdout.columns || 80,
rows: VIRTUAL_ROWS,
cwd: process.cwd(),
env: { ...process.env, FORCE_COLOR: '1' }
});
claude.onData(data => vp.push(data));
// Input handling
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', (data) => {
const str = data.toString();
// Mouse (SGR: \x1b[<btn;x;yM)
const mouseMatch = str.match(/\x1b\[<(\d+);(\d+);(\d+)([Mm])/);
if (mouseMatch) {
const btn = parseInt(mouseMatch[1]);
if (mouseMatch[4] === 'M') {
if (btn === 64) vp.scroll(-3);
else if (btn === 65) vp.scroll(3);
}
return;
}
// Search mode input
if (vp.search.isActive) {
vp.search.handleKey(str);
return;
}
// Ctrl+Space (0x00) triggers command mode
if (str === '\x00') {
vp.commandPending = true;
vp.draw();
return;
}
// Escape: cancel pending or return to insert mode
if (str === '\x1b') {
if (vp.commandPending) {
vp.commandPending = false;
vp.draw();
return;
}
if (vp.mode !== 'insert') {
vp.mode = 'insert';
vp.vim.reset();
vp.draw();
return;
}
// Pass Escape to Claude in insert mode
claude.write(data);
return;
}
// Command pending - select mode
if (vp.commandPending) {
vp.commandPending = false;
const wasInsert = vp.mode === 'insert';
if (str === 'i') { vp.mode = 'insert'; vp.draw(); return; }
if (str === 'n') {
// Initialize normal cursor to Claude's cursor position
const buffer = vp.term.buffer.active;
vp.normalCursor.line = buffer.baseY + buffer.cursorY;
vp.normalCursor.col = buffer.cursorX;
vp.mode = 'normal';
vp.vim.ensureLineVisible(vp.normalCursor.line);
vp.draw();
return;
}
if (str === 'v') { vp.vim.enterVisual('char', wasInsert ? 'insert' : vp.mode); return; }
if (str === 'V') { vp.vim.enterVisual('line', wasInsert ? 'insert' : vp.mode); return; }
vp.draw();
return;
}
// Viewport keys (work in any mode)
if (str === '\x1b[5~') { vp.scroll(-vp.height); return; } // PgUp
if (str === '\x1b[6~') { vp.scroll(vp.height); return; } // PgDn
if (str === '\x1b[1;5A') { vp.scroll(-1); return; } // Ctrl+Up
if (str === '\x1b[1;5B') { vp.scroll(1); return; } // Ctrl+Down
if (str === '\x0c') { vp.draw(); return; } // Ctrl+L
// Normal/visual mode - handle vim keys
if (vp.mode !== 'insert') {
vp.vim.handleKey(str);
return;
}
// Insert mode - pass to Claude
claude.write(data);
});
// Mouse mode
process.stdout.write('\x1b[?1000h\x1b[?1006h');
// Resize - only change columns, keep virtual rows constant
process.stdout.on('resize', () => {
const cols = process.stdout.columns || 80;
claude.resize(cols, VIRTUAL_ROWS);
vp.resize(cols, VIRTUAL_ROWS);
});
// Signal handlers
process.on('SIGINT', () => { cleanup(); claude.kill(); process.exit(0); });
process.on('SIGTERM', () => { cleanup(); claude.kill(); process.exit(0); });
claude.onExit(({ exitCode }) => { cleanup(); process.exit(exitCode); });
}, SPLASH_DURATION);