@sanohiro/casty 0.5.7 → 0.5.8

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 });
@@ -223,25 +228,64 @@ async function main() {
223
228
  clearTimeout(resizeTimer);
224
229
  resizeTimer = setTimeout(handleResize, 150);
225
230
  });
231
+ // Direct screenshot — bypasses screencast's capturing flag
232
+ const screenshotOpts = { format: screenshotFormat, optimizeForSpeed: true, captureBeyondViewport: false };
233
+ if (screenshotFormat === 'jpeg') screenshotOpts.quality = 85;
234
+ async function directCapture() {
235
+ try {
236
+ const { data } = await client.send('Page.captureScreenshot', screenshotOpts);
237
+ if (data) onFrame(data);
238
+ } catch {}
239
+ }
240
+
226
241
  async function handleResize() {
227
242
  if (resizing) { pendingResize = true; return; }
228
243
  resizing = true;
229
244
  try {
245
+ // Stop old screencast FIRST to prevent stale frames
246
+ await stopScreencast(client, screencastCleanup);
247
+
230
248
  const t = await getTermInfo({ keepAlive: true });
231
- const vh = t.height - Math.round(t.cellHeight);
249
+ const vh = t.height - t.cellHeight;
232
250
  const cw = Math.round(t.width / t.zoom);
233
251
  const ch = Math.round(vh / t.zoom);
252
+ setDisplaySize(t.cols, t.rows - 1);
234
253
  console.error(`casty: resize ${cw}x${ch} (dev:${t.width}x${vh}) zoom:${t.zoom.toFixed(2)}`);
235
254
 
236
255
  urlBar.updateCellSize(t.cellWidth, t.cellHeight);
237
256
  clearScreen();
238
257
  resetFrameCache();
258
+ disableDedup(3000); // Force re-send for 3s (bcon may not display first frame)
239
259
 
240
- await stopScreencast(client, screencastCleanup);
241
260
  await client.send('Emulation.setDeviceMetricsOverride', {
242
261
  width: cw, height: ch, deviceScaleFactor: t.zoom, mobile: false,
243
262
  });
244
263
 
264
+ // Wait for Chrome to finish re-rendering by watching for a screencast frame
265
+ await new Promise(resolve => {
266
+ const onFirstFrame = ({ sessionId }) => {
267
+ client.send('Page.screencastFrameAck', { sessionId }).catch(() => {});
268
+ client.removeListener('Page.screencastFrame', onFirstFrame);
269
+ resolve();
270
+ };
271
+ client.on('Page.screencastFrame', onFirstFrame);
272
+ client.send('Page.startScreencast', {
273
+ format: 'jpeg', quality: 10,
274
+ maxWidth: Math.round(cw / 4), maxHeight: Math.round(ch / 4),
275
+ everyNthFrame: 1,
276
+ }).catch(() => resolve());
277
+ setTimeout(() => {
278
+ client.removeListener('Page.screencastFrame', onFirstFrame);
279
+ resolve();
280
+ }, 2000);
281
+ });
282
+ await client.send('Page.stopScreencast').catch(() => {});
283
+
284
+ // Capture hi-res frame (Chrome has finished rendering)
285
+ await directCapture();
286
+ urlBar.render();
287
+
288
+ // Restart screencast for ongoing change detection
245
289
  ({ forceCapture, cleanup: screencastCleanup } = await startScreencast(client, {
246
290
  width: cw,
247
291
  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.8",
4
4
  "description": "TTY web browser using raw CDP and Kitty graphics protocol",
5
5
  "main": "bin/casty.js",
6
6
  "bin": {