-
Notifications
You must be signed in to change notification settings - Fork 0
/
displayManager.ts
38 lines (32 loc) · 1.1 KB
/
displayManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { ColorFunction } from "./coloredInput.ts";
import { gray } from "@std/fmt/colors";
export async function refreshDisplay(
currentInput: string,
cursorPosition: number,
colorFn: ColorFunction,
suggestion?: string,
): Promise<void> {
const encoder = new TextEncoder();
// Clear the current line
await Deno.stdout.write(
encoder.encode(
"\r" +
" ".repeat(Math.max(currentInput.length, suggestion?.length || 0) + 1),
),
);
// Move cursor back to the start of the line
await Deno.stdout.write(encoder.encode("\r"));
// Print the current input with the specified color
await Deno.stdout.write(encoder.encode(colorFn(currentInput)));
// If there's a suggestion and the input is empty, show the suggestion
if (suggestion && currentInput.length === 0) {
await Deno.stdout.write(encoder.encode(gray(suggestion)));
await Deno.stdout.write(encoder.encode("\r"));
}
// Move cursor to the correct position
if (cursorPosition < currentInput.length) {
await Deno.stdout.write(
encoder.encode(`\u001b[${currentInput.length - cursorPosition}D`),
);
}
}