Add super-fast dumpToTerminal method to CastConverter

- Added dumpToTerminal() method that builds entire cast content into one string and writes it all at once for maximum performance
- Handles resize ('r') events by tracking final terminal dimensions and applying them before writing content
- Processes output ('o') events into a single concatenated string for the fastest possible loading
- Ignores input ('i') events during dump as they're not needed for display
- Updated session cards to use the new fast dump method instead of manual parsing
- Results in dramatically faster session card loading and refresh

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mario Zechner 2025-06-18 05:40:01 +02:00
parent 2617ef4199
commit 0cef07ccfc
2 changed files with 56 additions and 17 deletions

View file

@ -2,6 +2,7 @@ import { LitElement, html, PropertyValues } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import './terminal.js';
import type { Terminal } from './terminal.js';
import { CastConverter } from '../utils/cast-converter.js';
export interface Session {
id: string;
@ -99,25 +100,11 @@ export class SessionCard extends LitElement {
const castContent = await response.text();
// Clear terminal and write snapshot data
// Clear terminal first
this.terminal.clear();
// Parse cast file and write content
const lines = castContent.trim().split('\n');
for (const line of lines) {
if (line.trim()) {
try {
const event = JSON.parse(line);
if (event.length >= 3 && event[1] === 'o') {
// Output event: [timestamp, 'o', data]
this.terminal.write(event[2], false); // Don't follow cursor for snapshot
}
} catch (_e) {
// Skip invalid lines
continue;
}
}
}
// Use the new super-fast dump method that handles everything in one operation
await CastConverter.dumpToTerminal(this.terminal, castContent);
// Scroll to bottom after loading
this.terminal.queueCallback(() => {

View file

@ -208,4 +208,56 @@ export class CastConverter {
}
}
}
/**
* Dump entire cast content to terminal instantly as a single write operation.
* This is the fastest way to load cast content - builds one string and writes it all at once.
* Handles resize events by applying the final dimensions to the terminal.
*
* @param terminal - DOM terminal instance with write() and setTerminalSize() methods
* @param castContent - Raw cast file content
* @returns Promise that resolves when dump is complete
*/
static async dumpToTerminal(
terminal: {
write: (data: string, followCursor?: boolean) => void;
setTerminalSize?: (cols: number, rows: number) => void;
},
castContent: string
): Promise<void> {
const converted = this.convertCast(castContent);
// Track final terminal dimensions from resize events
let finalCols = converted.header?.width || 80;
let finalRows = converted.header?.height || 24;
// Build up output string and track final resize dimensions
const outputChunks: string[] = [];
for (const event of converted.events) {
if (event.type === 'o') {
// Output event - add to content
outputChunks.push(event.data);
} else if (event.type === 'r') {
// Resize event - track final dimensions
const match = event.data.match(/^(\d+)x(\d+)$/);
if (match) {
finalCols = parseInt(match[1], 10);
finalRows = parseInt(match[2], 10);
}
}
// Ignore 'i' (input) events for dump
}
// Apply final terminal size first if we have resize capability
if (terminal.setTerminalSize) {
terminal.setTerminalSize(finalCols, finalRows);
}
// Write all content at once as a single operation (fastest possible)
const allContent = outputChunks.join('');
if (allContent) {
terminal.write(allContent, false); // Don't follow cursor during dump for performance
}
}
}