/
ansiUtils.mts
50 lines (37 loc) · 1.31 KB
/
ansiUtils.mts
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
39
40
41
42
43
44
45
46
47
48
49
50
import type { Direction } from 'node:tty';
import ansiEscapes from 'ansi-escapes';
import styles from 'ansi-styles';
export function green(text: string): string {
return styles.green.open + text + styles.green.close;
}
export function red(text: string): string {
return styles.red.open + text + styles.red.close;
}
export function yellow(text: string): string {
return styles.yellow.open + text + styles.yellow.close;
}
export function crlf(text: string): string {
return text.replace(/\n/g, '\r\n').replace(/\r+\r/g, '\r');
}
export function dim(text: string): string {
return styles.dim.open + text + styles.dim.close;
}
export type ColorFn = (text: string) => string;
export function combine(fn: ColorFn, ...fns: ColorFn[]): ColorFn {
return (text) => fns.reduce((acc, f) => f(acc), fn(text));
}
export function clearScreen() {
return ansiEscapes.clearScreen;
}
export function clearLine(dir: Direction) {
return dir > 0 ? ansiEscapes.eraseEndLine : dir < 0 ? ansiEscapes.eraseStartLine : ansiEscapes.eraseLine;
}
export function clearDown() {
return ansiEscapes.eraseDown;
}
export function moveCursor(dx: number, dy?: number | undefined) {
return ansiEscapes.cursorMove(dx, dy);
}
export function cursorTo(x: number, y?: number | undefined) {
return ansiEscapes.cursorTo(x, y);
}