MCPcopy Create free account
hub / github.com/denoland/std / StaticLine

Class StaticLine

cli/unstable_static_line.ts:45–158  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43 * ```
44 */
45export class StaticLine {
46 #id = ++nextID;
47 #atTop: boolean;
48 #lines: number[];
49 #released = false;
50 /**
51 * Constructs a new instance.
52 *
53 * @param on What side of the terminal the line should be on.
54 */
55 constructor(on: "top" | "bottom" = "bottom") {
56 this.#atTop = on === "top";
57 this.#lines = this.#atTop ? linesAtTop : linesAtBottom;
58 this.#lines.push(this.#id);
59 const top = linesAtTop.length + 1;
60 const bottom = getRows() - linesAtBottom.length;
61 write(
62 (this.#atTop ? "" : Ansi.shiftUpAndInsert()) +
63 Ansi.setScrollableRegion(top, bottom) +
64 Ansi.setCursorPosition(bottom),
65 );
66 }
67
68 /**
69 * Overwrites the contents of the line.
70 *
71 * @param line The new content to write.
72 * @returns A promise that resolves when the line is written.
73 *
74 * @example Usage
75 * ```ts ignore
76 * import { delay } from "@std/async/delay";
77 * import { StaticLine } from "@std/cli/unstable-static-line";
78 *
79 * const id = setInterval(() => console.log(Math.random()), 1000);
80 *
81 * const line = new StaticLine();
82 * await line.write("Hello World!");
83 * await delay(1500);
84 * await line.write("How are you?");
85 * await delay(1500);
86 * await line.write("Doing good?");
87 * await delay(1500);
88 * await line.releaseLine();
89 * ```
90 */
91 async write(line: string): Promise<void> {
92 if (this.#released) throw new ReferenceError("Line has been released");
93 await write(
94 Ansi.SAVE_CURSOR +
95 Ansi.DISABLE_ORIGIN_MODE +
96 Ansi.DISABLE_AUTO_WRAP +
97 Ansi.setCursorPosition(
98 this.#atTop
99 ? linesAtTop.indexOf(this.#id) + 1
100 : getRows() - linesAtBottom.indexOf(this.#id),
101 ) +
102 Ansi.ERASE_LINE +

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected