scroll/src/common/ansi.ts

74 lines
1.6 KiB
JavaScript
Raw Normal View History

/**
* ANSI/VT terminal escape code handling
*/
export const ANSI_PREFIX = '\x1b[';
/**
* ANSI escapes for various inputs
*/
export enum KeyCommand {
2023-11-10 19:40:05 -05:00
ArrowUp = ANSI_PREFIX + 'A',
ArrowDown = ANSI_PREFIX + 'B',
ArrowRight = ANSI_PREFIX + 'C',
ArrowLeft = ANSI_PREFIX + 'D',
2023-11-15 08:32:25 -05:00
Delete = ANSI_PREFIX + '3~',
2023-11-10 19:40:05 -05:00
PageUp = ANSI_PREFIX + '5~',
PageDown = ANSI_PREFIX + '6~',
// These keys have several possible escape sequences
Home = 'LineHome',
End = 'LineEnd',
2023-11-08 11:11:19 -05:00
}
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++;
2023-11-10 19:40:05 -05:00
return ANSI_PREFIX + `${row};${col}H`;
},
2023-11-10 19:40:05 -05:00
moveCursorForward: (col: number): string => ANSI_PREFIX + `${col}C`,
moveCursorDown: (row: number): string => ANSI_PREFIX + `${row}B`,
2023-11-08 11:11:19 -05:00
};
2023-11-16 16:00:03 -05:00
/**
* 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;
}
}
2023-11-08 11:11:19 -05:00
export default Ansi;