Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#!/usr/bin/env node

import { exitAlternateScreenBuffer } from "./utils/screen.js";
import {
exitAlternateScreenBuffer,
isInAlternateScreenBuffer,
} from "./utils/screen.js";
import { processUtils } from "./utils/processUtils.js";
import { createProgram } from "./utils/commands.js";
import { getApiKeyErrorMessage, checkBaseDomain } from "./utils/config.js";

// Global Ctrl+C handler to ensure it always exits
processUtils.on("SIGINT", () => {
// Force exit immediately, clearing alternate screen buffer
exitAlternateScreenBuffer();
// Only restore the alternate screen buffer if we actually entered it.
// Unconditionally sending the exit sequence when no TUI is active causes
// the terminal to restore a stale saved-cursor position, jumping the
// cursor and garbling any plain-text output (e.g. `rli d ssh` wait loop).
if (isInAlternateScreenBuffer()) {
exitAlternateScreenBuffer();
}
processUtils.stdout.write("\n");
processUtils.exit(130); // Standard exit code for SIGINT
});
Expand Down
9 changes: 9 additions & 0 deletions src/utils/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@

import { processUtils } from "./processUtils.js";

let _inAlternateScreenBuffer = false;

/** Returns true if the alternate screen buffer is currently active. */
export function isInAlternateScreenBuffer(): boolean {
return _inAlternateScreenBuffer;
}

/**
* Enter the alternate screen buffer.
* This provides a fullscreen experience where content won't mix with
* previous terminal output. Like vim or top.
*/
export function enterAlternateScreenBuffer(): void {
_inAlternateScreenBuffer = true;
processUtils.stdout.write("\x1b[?1049h");
}

Expand All @@ -23,6 +31,7 @@ export function enterAlternateScreenBuffer(): void {
* This returns the terminal to its original state before enterAlternateScreen() was called.
*/
export function exitAlternateScreenBuffer(): void {
_inAlternateScreenBuffer = false;
processUtils.stdout.write("\x1b[?1049l");
}

Expand Down
Loading