138 lines
2.7 KiB
JavaScript
138 lines
2.7 KiB
JavaScript
/**
|
|
* ANSI/VT terminal escape code handling
|
|
*/
|
|
|
|
export const ANSI_PREFIX = '\x1b[';
|
|
|
|
/**
|
|
* ANSI escapes for various inputs
|
|
*/
|
|
export enum KeyCommand {
|
|
ArrowUp = ANSI_PREFIX + 'A',
|
|
ArrowDown = ANSI_PREFIX + 'B',
|
|
ArrowRight = ANSI_PREFIX + 'C',
|
|
ArrowLeft = ANSI_PREFIX + 'D',
|
|
Delete = ANSI_PREFIX + '3~',
|
|
PageUp = ANSI_PREFIX + '5~',
|
|
PageDown = ANSI_PREFIX + '6~',
|
|
Enter = '\r',
|
|
Escape = '\x1b',
|
|
Backspace = '\x7f',
|
|
|
|
// These keys have several possible escape sequences
|
|
Home = 'LineHome',
|
|
End = 'LineEnd',
|
|
}
|
|
|
|
/**
|
|
* Values for Basic ANSI colors and formatting
|
|
*/
|
|
export enum AnsiColor {
|
|
TypeRGB = 2,
|
|
Type256 = 5,
|
|
|
|
Invert = 7,
|
|
|
|
// Foreground Colors
|
|
FgBlack = 30,
|
|
FgRed,
|
|
FgGreen,
|
|
FgYellow,
|
|
FgBlue,
|
|
FgMagenta,
|
|
FgCyan,
|
|
FgWhite,
|
|
FgDefault,
|
|
|
|
// Background Colors
|
|
BgBlack = 40,
|
|
BgRed,
|
|
BgGreen,
|
|
BgYellow,
|
|
BgBlue,
|
|
BgMagenta,
|
|
BgCyan,
|
|
BgWhite,
|
|
BgDefault,
|
|
|
|
// Bright Foreground Colors
|
|
FgBrightBlack = 90,
|
|
FgBrightRed,
|
|
FgBrightGreen,
|
|
FgBrightYellow,
|
|
FgBrightBlue,
|
|
FgBrightMagenta,
|
|
FgBrightCyan,
|
|
FgBrightWhite,
|
|
|
|
// Bright Background Colors
|
|
BgBrightBlack = 100,
|
|
BgBrightRed,
|
|
BgBrightGreen,
|
|
BgBrightYellow,
|
|
BgBrightBlue,
|
|
BgBrightMagenta,
|
|
BgBrightCyan,
|
|
BgBrightWhite,
|
|
}
|
|
|
|
export enum Ground {
|
|
Fore = AnsiColor.FgDefault,
|
|
Back = AnsiColor.BgDefault,
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// ANSI escape code generation fns
|
|
// ----------------------------------------------------------------------------
|
|
const code = (
|
|
param: string | number | string[] | number[],
|
|
suffix: string = '',
|
|
): string => {
|
|
if (Array.isArray(param)) {
|
|
param = param.join(';');
|
|
}
|
|
|
|
return [ANSI_PREFIX, param, suffix].join('');
|
|
};
|
|
|
|
const moveCursor = (row: number, col: number): string => {
|
|
// Convert to 1-based counting
|
|
row++;
|
|
col++;
|
|
|
|
return code([row, col], 'H');
|
|
};
|
|
const moveCursorForward = (col: number): string => code(col, 'C');
|
|
const moveCursorDown = (row: number): string => code(row, 'B');
|
|
const textFormat = (param: string | number | string[] | number[]): string =>
|
|
code(param, 'm');
|
|
const color = (value: AnsiColor): string => textFormat(value);
|
|
const color256 = (value: number, ground: Ground = Ground.Fore): string =>
|
|
textFormat([ground, AnsiColor.Type256, value]);
|
|
const rgb = (
|
|
r: number,
|
|
g: number,
|
|
b: number,
|
|
ground: Ground = Ground.Fore,
|
|
): string => textFormat([ground, AnsiColor.TypeRGB, r, g, b]);
|
|
|
|
export const Ansi = {
|
|
ClearLine: code('K'),
|
|
ClearScreen: code('2J'),
|
|
ResetCursor: code('H'),
|
|
HideCursor: code('?25l'),
|
|
ShowCursor: code('?25h'),
|
|
GetCursorLocation: code('6n'),
|
|
InvertColor: textFormat(AnsiColor.Invert),
|
|
ResetFormatting: textFormat(''),
|
|
moveCursor,
|
|
moveCursorForward,
|
|
moveCursorDown,
|
|
textFormat,
|
|
color,
|
|
color256,
|
|
rgb,
|
|
};
|
|
|
|
export default Ansi;
|