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
24 changes: 13 additions & 11 deletions src/commands/benchmark-job/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import {
type BenchmarkJob,
} from "../../services/benchmarkJobService.js";
import { outputError } from "../../utils/output.js";
import {
enterAlternateScreenBuffer,
exitAlternateScreenBuffer,
hideCursor,
showCursor,
clearScreen,
} from "../../utils/screen.js";
import {
isJobCompleted,
fetchAllRunsProgress,
Expand All @@ -25,28 +32,23 @@ function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

// Terminal control utilities for full-screen mode
// Terminal control utilities for in-place rendering
const ANSI = {
hideCursor: "\x1b[?25l",
showCursor: "\x1b[?25h",
clearScreen: "\x1b[2J",
moveTo: (row: number, col: number) => `\x1b[${row};${col}H`,
clearLine: "\x1b[2K",
enterAltScreen: "\x1b[?1049h",
exitAltScreen: "\x1b[?1049l",
};

// Enter full-screen mode
function enterFullScreen(): void {
process.stdout.write(ANSI.enterAltScreen);
process.stdout.write(ANSI.hideCursor);
process.stdout.write(ANSI.clearScreen);
enterAlternateScreenBuffer();
hideCursor();
clearScreen();
}

// Exit full-screen mode
function exitFullScreen(): void {
process.stdout.write(ANSI.showCursor);
process.stdout.write(ANSI.exitAltScreen);
showCursor();
exitAlternateScreenBuffer();
}

// Track how many lines the last render wrote so we can clear stale lines
Expand Down
9 changes: 9 additions & 0 deletions src/utils/screen.ts

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also move, or document a follow-up to move, benchmark-job watch to use these helpers (right now it does its own handling of the raw escape sequences, but that also means isInAlternateScreenBuffer() is false when we're in the benchmark-job watch alternate screen buffer -- potential footgun)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrated benchmark-job watch to the shared helpers (replaced its raw enterAltScreen/exitAltScreen/cursor/clear sequences). isInAlternateScreenBuffer() now reports true while in the watch alt-screen, so the global SIGINT handler restores it correctly. (ac87f8b)

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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth adding a small test to verify isInAlternateScreenBuffer() starts as false, is true after enterAlternateScreenBuffer(), and false after exitAlternateScreenBuffer()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests/__tests__/utils/screen.test.ts covering false → true → false around enter/exit. (ac87f8b)

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
37 changes: 37 additions & 0 deletions tests/__tests__/utils/screen.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Tests for terminal screen buffer utilities.
*/

import { describe, it, expect, beforeEach, afterEach } from "@jest/globals";
import { processUtils } from "../../../src/utils/processUtils.js";
import {
isInAlternateScreenBuffer,
enterAlternateScreenBuffer,
exitAlternateScreenBuffer,
} from "../../../src/utils/screen.js";

describe("alternate screen buffer tracking", () => {
let originalWrite: typeof processUtils.stdout.write;

beforeEach(() => {
// Swallow the escape sequences so they don't leak into test output.
originalWrite = processUtils.stdout.write;
processUtils.stdout.write = () => true;
});

afterEach(() => {
// Leave the buffer flag in a clean state for other tests.
exitAlternateScreenBuffer();
processUtils.stdout.write = originalWrite;
});

it("tracks enter and exit transitions", () => {
expect(isInAlternateScreenBuffer()).toBe(false);

enterAlternateScreenBuffer();
expect(isInAlternateScreenBuffer()).toBe(true);

exitAlternateScreenBuffer();
expect(isInAlternateScreenBuffer()).toBe(false);
});
});
Loading