capsule-ai-cli
Version:
The AI Model Orchestrator - Intelligent multi-model workflows with device-locked licensing
139 lines • 4.32 kB
JavaScript
import ansiEscapes from 'ansi-escapes';
import cliCursor from 'cli-cursor';
import terminalSize from 'terminal-size';
export class TerminalController {
cleanupHandlers = [];
isAlternateScreen = false;
isBracketedPasteEnabled = false;
savedScreenContent = '';
static getSize() {
const size = terminalSize();
return {
columns: size.columns || 80,
rows: size.rows || 24
};
}
enterAlternateScreen() {
process.stdout.write(ansiEscapes.enterAlternativeScreen);
this.isAlternateScreen = true;
}
exitAlternateScreen() {
if (this.isAlternateScreen) {
process.stdout.write(ansiEscapes.exitAlternativeScreen);
this.isAlternateScreen = false;
}
}
clearScreen() {
process.stdout.write(ansiEscapes.clearScreen);
}
clearAndResetScreen() {
process.stdout.write(ansiEscapes.clearScreen + ansiEscapes.cursorTo(0, 0));
}
moveCursorTo(x, y) {
process.stdout.write(ansiEscapes.cursorTo(x - 1, y - 1));
}
clearLine() {
process.stdout.write(ansiEscapes.eraseLine);
}
clearLineAndReturn() {
process.stdout.write(ansiEscapes.eraseLine + '\r');
}
clearToEndOfLine() {
process.stdout.write(ansiEscapes.eraseEndLine);
}
clearScreenBelow() {
process.stdout.write('\x1b[J');
}
showCursor() {
cliCursor.show();
}
hideCursor() {
cliCursor.hide();
}
enableBracketedPaste() {
process.stdout.write('\x1b[?2004h');
this.isBracketedPasteEnabled = true;
}
disableBracketedPaste() {
if (this.isBracketedPasteEnabled) {
process.stdout.write('\x1b[?2004l');
this.isBracketedPasteEnabled = false;
}
}
saveCursorPosition() {
process.stdout.write(ansiEscapes.cursorSavePosition);
}
restoreCursorPosition() {
process.stdout.write(ansiEscapes.cursorRestorePosition);
}
scrollUp(lines = 1) {
for (let i = 0; i < lines; i++) {
process.stdout.write(ansiEscapes.scrollUp);
}
}
scrollDown(lines = 1) {
for (let i = 0; i < lines; i++) {
process.stdout.write(ansiEscapes.scrollDown);
}
}
cursorUp(lines = 1) {
process.stdout.write(ansiEscapes.cursorUp(lines));
}
cursorDown(lines = 1) {
process.stdout.write(ansiEscapes.cursorDown(lines));
}
cursorForward(columns = 1) {
process.stdout.write(ansiEscapes.cursorForward(columns));
}
cursorBackward(columns = 1) {
process.stdout.write(ansiEscapes.cursorBackward(columns));
}
writeAt(x, y, text) {
this.saveCursorPosition();
this.moveCursorTo(x, y);
process.stdout.write(text);
this.restoreCursorPosition();
}
clearLineAndWrite(lineNumber, text) {
this.moveCursorTo(1, lineNumber);
this.clearLine();
process.stdout.write(text);
}
setupInteractiveMode() {
this.enterAlternateScreen();
this.hideCursor();
this.enableBracketedPaste();
this.clearAndResetScreen();
this.registerCleanupHandlers();
}
cleanup() {
this.exitAlternateScreen();
this.showCursor();
this.disableBracketedPaste();
process.stdout.write('\n');
}
registerCleanupHandlers() {
const cleanupFn = () => this.cleanup();
this.cleanupHandlers = [
() => process.removeListener('exit', cleanupFn),
() => process.removeListener('SIGINT', cleanupFn),
() => process.removeListener('SIGTERM', cleanupFn),
() => process.removeListener('SIGHUP', cleanupFn)
];
process.on('exit', cleanupFn);
process.on('SIGINT', cleanupFn);
process.on('SIGTERM', cleanupFn);
process.on('SIGHUP', cleanupFn);
}
unregisterCleanupHandlers() {
this.cleanupHandlers.forEach(handler => handler());
this.cleanupHandlers = [];
}
onResize(callback) {
process.stdout.on('resize', () => {
callback(TerminalController.getSize());
});
}
}
export const terminalController = new TerminalController();
//# sourceMappingURL=terminal-controller.js.map