mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-16 08:25:14 +02:00
Initial open-source release
This commit is contained in:
commit
1a42152e6f
1199 changed files with 257054 additions and 0 deletions
143
packages/cli/src/memory-flow-interactive.ts
Normal file
143
packages/cli/src/memory-flow-interactive.ts
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
import { emitKeypressEvents } from 'node:readline';
|
||||
import {
|
||||
buildMemoryFlowViewModel,
|
||||
createInitialMemoryFlowInteractionState,
|
||||
reduceMemoryFlowInteractionState,
|
||||
renderMemoryFlowInteractive,
|
||||
type MemoryFlowInteractionCommand,
|
||||
type MemoryFlowInteractionState,
|
||||
type MemoryFlowReplayInput,
|
||||
} from '@klo/context/ingest';
|
||||
|
||||
interface KloMemoryFlowKey {
|
||||
name?: string;
|
||||
ctrl?: boolean;
|
||||
}
|
||||
|
||||
export interface KloMemoryFlowStdin {
|
||||
isTTY?: boolean;
|
||||
isRaw?: boolean;
|
||||
setRawMode?(value: boolean): void;
|
||||
resume?(): void;
|
||||
pause?(): void;
|
||||
on(event: 'keypress', listener: (chunk: string, key: KloMemoryFlowKey) => void): this;
|
||||
off?(event: 'keypress', listener: (chunk: string, key: KloMemoryFlowKey) => void): this;
|
||||
removeListener?(event: 'keypress', listener: (chunk: string, key: KloMemoryFlowKey) => void): this;
|
||||
}
|
||||
|
||||
interface KloMemoryFlowInteractiveIo {
|
||||
stdin?: KloMemoryFlowStdin;
|
||||
stdout: {
|
||||
isTTY?: boolean;
|
||||
columns?: number;
|
||||
write(chunk: string): void;
|
||||
};
|
||||
}
|
||||
|
||||
interface RenderMemoryFlowInteractiveOptions {
|
||||
prepareKeypressEvents?(stdin: KloMemoryFlowStdin): void;
|
||||
}
|
||||
|
||||
function defaultPrepareKeypressEvents(stdin: KloMemoryFlowStdin): void {
|
||||
emitKeypressEvents(stdin as Parameters<typeof emitKeypressEvents>[0]);
|
||||
}
|
||||
|
||||
export function memoryFlowCommandForKey(
|
||||
chunk: string,
|
||||
search: MemoryFlowInteractionState['search'],
|
||||
key: KloMemoryFlowKey,
|
||||
): MemoryFlowInteractionCommand | null {
|
||||
if (search.editing) {
|
||||
if (key.name === 'escape') return 'search-clear';
|
||||
if (key.name === 'return' || key.name === 'enter') return 'search-submit';
|
||||
if (key.name === 'backspace') return 'search-backspace';
|
||||
if (chunk.length === 1 && chunk >= ' ' && chunk !== '\u007f') {
|
||||
return { type: 'search-input', value: chunk };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (key.ctrl === true && key.name === 'c') {
|
||||
return 'quit';
|
||||
}
|
||||
|
||||
if (key.name === '/') return 'search-start';
|
||||
if (key.name === 'left') return 'left';
|
||||
if (key.name === 'right') return 'right';
|
||||
if (key.name === 'up') return 'up';
|
||||
if (key.name === 'down') return 'down';
|
||||
if (key.name === 'return' || key.name === 'enter') return 'enter';
|
||||
if (key.name === 'tab') return 'tab';
|
||||
if (key.name === 'f') return 'filter';
|
||||
if (key.name === 'p') return 'provenance';
|
||||
if (key.name === 't') return 'transcript';
|
||||
if (key.name === 'q' || key.name === 'escape') return 'quit';
|
||||
return null;
|
||||
}
|
||||
|
||||
function removeKeypressListener(
|
||||
stdin: KloMemoryFlowStdin,
|
||||
handler: (chunk: string, key: KloMemoryFlowKey) => void,
|
||||
): void {
|
||||
if (stdin.off) {
|
||||
stdin.off('keypress', handler);
|
||||
return;
|
||||
}
|
||||
stdin.removeListener?.('keypress', handler);
|
||||
}
|
||||
|
||||
function repaint(input: MemoryFlowReplayInput, state: MemoryFlowInteractionState, io: KloMemoryFlowInteractiveIo): void {
|
||||
const view = buildMemoryFlowViewModel(input);
|
||||
io.stdout.write('\u001b[2J\u001b[H');
|
||||
io.stdout.write(renderMemoryFlowInteractive(view, state, { terminalWidth: io.stdout.columns }));
|
||||
}
|
||||
|
||||
export async function renderMemoryFlowInteractively(
|
||||
input: MemoryFlowReplayInput,
|
||||
io: KloMemoryFlowInteractiveIo,
|
||||
options: RenderMemoryFlowInteractiveOptions = {},
|
||||
): Promise<void> {
|
||||
const stdin = io.stdin;
|
||||
if (stdin?.isTTY !== true) {
|
||||
const view = buildMemoryFlowViewModel(input);
|
||||
io.stdout.write(
|
||||
renderMemoryFlowInteractive(view, createInitialMemoryFlowInteractionState(view), {
|
||||
terminalWidth: io.stdout.columns,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const view = buildMemoryFlowViewModel(input);
|
||||
let state = createInitialMemoryFlowInteractionState(view);
|
||||
const previousRawMode = stdin.isRaw === true;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const cleanup = (): void => {
|
||||
removeKeypressListener(stdin, handleKeypress);
|
||||
stdin.setRawMode?.(previousRawMode);
|
||||
stdin.pause?.();
|
||||
};
|
||||
|
||||
const handleKeypress = (_chunk: string, key: KloMemoryFlowKey): void => {
|
||||
const command = memoryFlowCommandForKey(_chunk, state.search, key);
|
||||
if (!command) {
|
||||
return;
|
||||
}
|
||||
|
||||
state = reduceMemoryFlowInteractionState(state, command, view);
|
||||
repaint(input, state, io);
|
||||
|
||||
if (state.shouldQuit) {
|
||||
cleanup();
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
(options.prepareKeypressEvents ?? defaultPrepareKeypressEvents)(stdin);
|
||||
stdin.setRawMode?.(true);
|
||||
stdin.resume?.();
|
||||
stdin.on('keypress', handleKeypress);
|
||||
repaint(input, state, io);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue