Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class TerminalManager {
readonly xterm: Terminal;
private readonly fitAddon: FitAddon;
private resizeObserver: ResizeObserver | null = null;
private dataHandler: ((data: string) => void) | null = null;

constructor() {
this.xterm = new Terminal({
Expand Down Expand Up @@ -39,6 +40,11 @@ export class TerminalManager {

this.fitAddon = new FitAddon();
this.xterm.loadAddon(this.fitAddon);

// Register onData once in constructor to prevent duplicate handlers on restart
this.xterm.onData((data: string) => {
this.dataHandler?.(data);
});
}

/** Mount the terminal into a DOM element and start auto-resize. */
Expand All @@ -61,9 +67,11 @@ export class TerminalManager {
this.xterm.write(data);
}

/** Register a handler for user keystrokes (sent to shell stdin). */
/** Register a handler for user keystrokes (sent to shell stdin).
* Only one handler can be registered at a time; subsequent calls replace the previous handler.
*/
onData(handler: (data: string) => void): void {
this.xterm.onData(handler);
this.dataHandler = handler;
}

/** Current terminal dimensions for pty resize. */
Expand Down