Skip to content

Commit a42ed95

Browse files
committed
✨ add clear option to alternateBuffer
Add `{ clear?: boolean }` option to `ALTSCREEN()` and `alternateBuffer()`. When `clear` is false, uses xterm private mode 47 to switch without clearing the alternate buffer contents. Closes #13
1 parent 5372af8 commit a42ed95

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

settings.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ export function settings(...sequence: Setting[]): Setting {
1919
};
2020
}
2121

22-
export function alternateBuffer(): Setting {
22+
export function alternateBuffer(
23+
options?: { clear?: boolean },
24+
): Setting {
2325
return {
24-
apply: ALTSCREEN(),
26+
apply: ALTSCREEN(options),
2527
revert: MAINSCREEN(),
2628
};
2729
}

termcodes.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,27 @@ export function HIDECURSOR(): Uint8Array {
5555
}
5656

5757
/**
58-
* Switch to the alternate screen buffer (xterm private mode 1049).
58+
* Switch to the alternate screen buffer.
5959
*
60-
* Saves the cursor and switches to a clean alternate screen. Use
61-
* {@link MAINSCREEN} to switch back.
60+
* Saves the cursor and switches to the alternate screen. When `clear` is
61+
* `true` (the default), the alternate buffer is cleared on entry. When
62+
* `false`, the existing contents are preserved.
63+
*
64+
* Use {@link MAINSCREEN} to switch back.
6265
*
6366
* @see {@link https://invisible-island.net/xterm/ctlseqs/ctlseqs.html | xterm control sequences}
6467
*/
65-
export function ALTSCREEN(): Uint8Array {
66-
return CSI("?1049h");
68+
export function ALTSCREEN(options?: { clear?: boolean }): Uint8Array {
69+
let { clear = true } = options ?? {};
70+
if (clear) {
71+
return CSI("?1049h");
72+
} else {
73+
return CSI("?47h");
74+
}
6775
}
6876

6977
/**
70-
* Switch back to the main screen buffer (xterm private mode 1049).
78+
* Switch back to the main screen buffer.
7179
*
7280
* Restores the cursor and returns to the main screen with scrollback intact.
7381
*

test/settings.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ describe("settings", () => {
1818
expect(str(s.apply)).toBe("\x1b[?1049h");
1919
expect(str(s.revert)).toBe("\x1b[?1049l");
2020
});
21+
22+
it("uses mode 47 when clear is false", () => {
23+
let s = alternateBuffer({ clear: false });
24+
expect(str(s.apply)).toBe("\x1b[?47h");
25+
expect(str(s.revert)).toBe("\x1b[?1049l");
26+
});
2127
});
2228

2329
describe("cursor", () => {

0 commit comments

Comments
 (0)