@sanohiro/casty 0.5.7 → 0.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/casty.js CHANGED
@@ -27,7 +27,7 @@ if (!process.env.CASTY_ENSURE_CHROME) {
27
27
  }
28
28
 
29
29
  import { startBrowser, setupPage, startScreencast, stopScreencast } from '../lib/browser.js';
30
- import { sendFrame, resetFrameCache, clearScreen, hideCursor, showCursor, cleanup as cleanupTmp, transport } from '../lib/kitty.js';
30
+ import { sendFrame, resetFrameCache, clearScreen, hideCursor, showCursor, cleanup as cleanupTmp, transport, setDisplaySize, disableDedup } from '../lib/kitty.js';
31
31
  import { enableMouse, disableMouse, startInputHandling } from '../lib/input.js';
32
32
  import { loadKeyBindings } from '../lib/keys.js';
33
33
  import { loadConfig } from '../lib/config.js';
@@ -97,10 +97,14 @@ async function getTermInfo({ keepAlive = false } = {}) {
97
97
 
98
98
  const pixelSize = await queryTermPixelSize({ keepAlive });
99
99
  if (pixelSize) {
100
- const cellWidth = pixelSize.width / cols;
101
- const cellHeight = pixelSize.height / rows;
100
+ // Align to cell boundaries: floor cell size, then multiply back
101
+ // This ensures image pixels == display pixels (no GPU interpolation blur)
102
+ const cellWidth = Math.floor(pixelSize.width / cols);
103
+ const cellHeight = Math.floor(pixelSize.height / rows);
104
+ const width = cellWidth * cols;
105
+ const height = cellHeight * rows;
102
106
  const zoom = calcZoom(cellWidth);
103
- return { cols, rows, width: pixelSize.width, height: pixelSize.height, cellWidth, cellHeight, zoom };
107
+ return { cols, rows, width, height, cellWidth, cellHeight, zoom };
104
108
  }
105
109
 
106
110
  const cellWidth = parseInt(process.env.CASTY_CELL_WIDTH) || 10;
@@ -122,8 +126,9 @@ async function main() {
122
126
  const browser = await browserP;
123
127
 
124
128
  // Reserve line 1 for URL bar, use the rest for browser display
125
- const barHeight = Math.round(term.cellHeight);
129
+ const barHeight = term.cellHeight;
126
130
  const viewHeight = term.height - barHeight;
131
+ setDisplaySize(term.cols, term.rows - 1);
127
132
 
128
133
  // Phase 2: CDP connection + page setup
129
134
  const { client, cssWidth, cssHeight } = await setupPage(browser, { ...term, height: viewHeight });
@@ -213,6 +218,7 @@ async function main() {
213
218
 
214
219
  process.on('SIGINT', shutdown);
215
220
  process.on('SIGTERM', shutdown);
221
+ process.on('SIGHUP', shutdown);
216
222
 
217
223
  // SIGWINCH: Follow resize + font size changes
218
224
  // Debounced (150ms) + guarded with pending flag to catch late resizes
@@ -223,25 +229,64 @@ async function main() {
223
229
  clearTimeout(resizeTimer);
224
230
  resizeTimer = setTimeout(handleResize, 150);
225
231
  });
232
+ // Direct screenshot — bypasses screencast's capturing flag
233
+ const screenshotOpts = { format: screenshotFormat, optimizeForSpeed: true, captureBeyondViewport: false };
234
+ if (screenshotFormat === 'jpeg') screenshotOpts.quality = 85;
235
+ async function directCapture() {
236
+ try {
237
+ const { data } = await client.send('Page.captureScreenshot', screenshotOpts);
238
+ if (data) onFrame(data);
239
+ } catch {}
240
+ }
241
+
226
242
  async function handleResize() {
227
243
  if (resizing) { pendingResize = true; return; }
228
244
  resizing = true;
229
245
  try {
246
+ // Stop old screencast FIRST to prevent stale frames
247
+ await stopScreencast(client, screencastCleanup);
248
+
230
249
  const t = await getTermInfo({ keepAlive: true });
231
- const vh = t.height - Math.round(t.cellHeight);
250
+ const vh = t.height - t.cellHeight;
232
251
  const cw = Math.round(t.width / t.zoom);
233
252
  const ch = Math.round(vh / t.zoom);
253
+ setDisplaySize(t.cols, t.rows - 1);
234
254
  console.error(`casty: resize ${cw}x${ch} (dev:${t.width}x${vh}) zoom:${t.zoom.toFixed(2)}`);
235
255
 
236
256
  urlBar.updateCellSize(t.cellWidth, t.cellHeight);
237
257
  clearScreen();
238
258
  resetFrameCache();
259
+ disableDedup(3000); // Force re-send for 3s (bcon may not display first frame)
239
260
 
240
- await stopScreencast(client, screencastCleanup);
241
261
  await client.send('Emulation.setDeviceMetricsOverride', {
242
262
  width: cw, height: ch, deviceScaleFactor: t.zoom, mobile: false,
243
263
  });
244
264
 
265
+ // Wait for Chrome to finish re-rendering by watching for a screencast frame
266
+ await new Promise(resolve => {
267
+ const onFirstFrame = ({ sessionId }) => {
268
+ client.send('Page.screencastFrameAck', { sessionId }).catch(() => {});
269
+ client.removeListener('Page.screencastFrame', onFirstFrame);
270
+ resolve();
271
+ };
272
+ client.on('Page.screencastFrame', onFirstFrame);
273
+ client.send('Page.startScreencast', {
274
+ format: 'jpeg', quality: 10,
275
+ maxWidth: Math.round(cw / 4), maxHeight: Math.round(ch / 4),
276
+ everyNthFrame: 1,
277
+ }).catch(() => resolve());
278
+ setTimeout(() => {
279
+ client.removeListener('Page.screencastFrame', onFirstFrame);
280
+ resolve();
281
+ }, 2000);
282
+ });
283
+ await client.send('Page.stopScreencast').catch(() => {});
284
+
285
+ // Capture hi-res frame (Chrome has finished rendering)
286
+ await directCapture();
287
+ urlBar.render();
288
+
289
+ // Restart screencast for ongoing change detection
245
290
  ({ forceCapture, cleanup: screencastCleanup } = await startScreencast(client, {
246
291
  width: cw,
247
292
  height: ch,
package/lib/kitty.js CHANGED
@@ -29,6 +29,16 @@ import { loadConfig } from './config.js';
29
29
  const tmpFile = join(tmpdir(), `casty-frame-${process.pid}.png`);
30
30
  const tmpPathB64 = Buffer.from(tmpFile).toString('base64');
31
31
 
32
+ // Display size in cells (set by caller, used for c=/r= parameters)
33
+ let _cols = 0;
34
+ let _rows = 0;
35
+
36
+ // Set display size (cols = terminal columns, rows = display rows excluding URL bar)
37
+ export function setDisplaySize(cols, rows) {
38
+ _cols = cols;
39
+ _rows = rows;
40
+ }
41
+
32
42
  // Detect transfer mode
33
43
  function detectTransport() {
34
44
  const config = loadConfig();
@@ -74,24 +84,39 @@ export function cleanup() {
74
84
 
75
85
  // Frame deduplication — skip identical consecutive frames
76
86
  let lastFrameData = '';
87
+ let _dedupDisabled = false;
88
+ let _dedupTimer = null;
89
+
90
+ // Temporarily disable dedup (e.g. after resize, bcon needs re-send)
91
+ export function disableDedup(ms = 3000) {
92
+ _dedupDisabled = true;
93
+ clearTimeout(_dedupTimer);
94
+ _dedupTimer = setTimeout(() => { _dedupDisabled = false; }, ms);
95
+ }
77
96
 
78
97
  // File transfer mode (fast: sends only path)
79
98
  // Prepends cursor-home to batch into a single write
80
99
  function sendFrameFile(base64Data) {
81
- if (base64Data.length === lastFrameData.length && base64Data === lastFrameData) return;
100
+ if (!_dedupDisabled && base64Data.length === lastFrameData.length && base64Data === lastFrameData) {
101
+ return;
102
+ }
82
103
  lastFrameData = base64Data;
83
104
  writeFileSync(tmpFile, Buffer.from(base64Data, 'base64'));
84
- process.stdout.write(`${CURSOR_HOME}\x1b_Ga=T,f=100,t=f,q=2,C=1,i=1;${tmpPathB64}\x1b\\`);
105
+ const crFile = _cols && _rows ? `,c=${_cols},r=${_rows}` : '';
106
+ process.stdout.write(`${CURSOR_HOME}\x1b_Ga=T,f=100,t=f,q=2,C=1,i=1${crFile};${tmpPathB64}\x1b\\`);
85
107
  }
86
108
 
87
109
  // Inline mode (4096B chunked, PNG only)
88
110
  // Prepends cursor-home and batches all chunks into a single stdout.write
89
111
  function sendFrameInline(pngBase64) {
90
- if (pngBase64.length === lastFrameData.length && pngBase64 === lastFrameData) return;
112
+ if (!_dedupDisabled && pngBase64.length === lastFrameData.length && pngBase64 === lastFrameData) {
113
+ return;
114
+ }
91
115
  lastFrameData = pngBase64;
92
116
  const CHUNK = 4096;
117
+ const crInline = _cols && _rows ? `,c=${_cols},r=${_rows}` : '';
93
118
  if (pngBase64.length <= CHUNK) {
94
- process.stdout.write(`${CURSOR_HOME}\x1b_Ga=T,f=100,q=2,C=1,i=1;${pngBase64}\x1b\\`);
119
+ process.stdout.write(`${CURSOR_HOME}\x1b_Ga=T,f=100,q=2,C=1,i=1${crInline};${pngBase64}\x1b\\`);
95
120
  return;
96
121
  }
97
122
  const parts = [CURSOR_HOME];
@@ -100,7 +125,7 @@ function sendFrameInline(pngBase64) {
100
125
  const chunk = pngBase64.slice(i, i + CHUNK);
101
126
  const more = i + CHUNK < pngBase64.length ? 1 : 0;
102
127
  if (i === 0) {
103
- parts.push(`\x1b_Ga=T,f=100,q=2,C=1,i=1,m=${more};${chunk}\x1b\\`);
128
+ parts.push(`\x1b_Ga=T,f=100,q=2,C=1,i=1${crInline},m=${more};${chunk}\x1b\\`);
104
129
  } else {
105
130
  parts.push(`\x1b_Gm=${more};${chunk}\x1b\\`);
106
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanohiro/casty",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "description": "TTY web browser using raw CDP and Kitty graphics protocol",
5
5
  "main": "bin/casty.js",
6
6
  "bin": {