74 lines
1.6 KiB
JavaScript
74 lines
1.6 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~',
|
|
|
|
// These keys have several possible escape sequences
|
|
Home = 'LineHome',
|
|
End = 'LineEnd',
|
|
}
|
|
|
|
export const Ansi = {
|
|
ClearLine: ANSI_PREFIX + 'K',
|
|
ClearScreen: ANSI_PREFIX + '2J',
|
|
ResetCursor: ANSI_PREFIX + 'H',
|
|
HideCursor: ANSI_PREFIX + '?25l',
|
|
ShowCursor: ANSI_PREFIX + '?25h',
|
|
GetCursorLocation: ANSI_PREFIX + '6n',
|
|
moveCursor: function moveCursor(row: number, col: number): string {
|
|
// Convert to 1-based counting
|
|
row++;
|
|
col++;
|
|
|
|
return ANSI_PREFIX + `${row};${col}H`;
|
|
},
|
|
moveCursorForward: (col: number): string => ANSI_PREFIX + `${col}C`,
|
|
moveCursorDown: (row: number): string => ANSI_PREFIX + `${row}B`,
|
|
};
|
|
|
|
/**
|
|
* Convert input from ANSI escape sequences into a form
|
|
* that can be more easily mapped to editor commands
|
|
*
|
|
* @param parsed - the decoded chunk of input
|
|
*/
|
|
export function readKey(parsed: string): string {
|
|
// Return the input if it's unambiguous
|
|
if (parsed in KeyCommand) {
|
|
return parsed;
|
|
}
|
|
|
|
// Some keycodes have multiple potential inputs
|
|
switch (parsed) {
|
|
case '\x1b[1~':
|
|
case '\x1b[7~':
|
|
case '\x1bOH':
|
|
case '\x1b[H':
|
|
return KeyCommand.Home;
|
|
|
|
case '\x1b[4~':
|
|
case '\x1b[8~':
|
|
case '\x1bOF':
|
|
case '\x1b[F':
|
|
return KeyCommand.End;
|
|
|
|
default:
|
|
return parsed;
|
|
}
|
|
}
|
|
|
|
export default Ansi;
|