@teaui/core
Version:
A high-level terminal UI library for Node
148 lines • 3.23 kB
JavaScript
import { underline } from '../ansi.js';
/**
* Convert a HotKey to a display string like 'C-x' suitable for Legend items.
*/
export function hotKeyToString(hotKey) {
if (typeof hotKey === 'string') {
return hotKey;
}
let str = '';
if (hotKey.ctrl)
str += 'C-';
if (hotKey.alt)
str += 'A-';
if (hotKey.gui)
str += 'G-';
if (hotKey.shift)
str += 'S-';
str += hotKey.char;
return str;
}
export function toHotKeyDef(hotKey) {
if (typeof hotKey !== 'string') {
return hotKey;
}
// hotkey string supports:
// C- control
// A- alt
// G- gui
// S- shift
const ctrl = hotKey.includes('C-');
const alt = hotKey.includes('A-');
const gui = hotKey.includes('G-');
const shift = hotKey.includes('S-');
const char = mapKey(hotKey.replace(/^([CAGS]-)*/, '').toLowerCase());
return { char, ctrl, alt, gui, shift };
}
/**
* Maps a key name to its sigil representation.
*/
const KEY_SIGILS = {
enter: '⤦',
return: '⤦',
up: '↑',
down: '↓',
left: '←',
right: '→',
cmd: '⌘',
command: '⌘',
ctrl: '⌃',
control: '⌃',
alt: '⌥',
option: '⌥',
opt: '⌥',
shift: '⇧',
escape: '␛',
esc: '␛',
tab: '⇥',
space: '␣',
backspace: '⌫',
delete: '⌦',
pageup: '⇞',
pagedown: '⇟',
home: '↖',
end: '↘',
};
export function mapKey(key) {
return KEY_SIGILS[key.toLowerCase()] ?? key;
}
export function isKeyPrintable(event) {
switch (event.name) {
case 'up':
case 'down':
case 'left':
case 'right':
case 'pageup':
case 'pagedown':
case 'home':
case 'end':
case 'insert':
case 'clear':
case 'enter':
case 'return':
case 'escape':
case 'tab':
case 'delete':
case 'backspace':
case 'f1':
case 'f2':
case 'f3':
case 'f4':
case 'f5':
case 'f6':
case 'f7':
case 'f8':
case 'f9':
case 'f10':
case 'f11':
case 'f12':
return false;
}
if ((event.char.codePointAt(0) ?? 0) < 32) {
return false;
}
return true;
}
export const match = (key, event) => {
if ((key.ctrl ?? false) !== event.ctrl) {
return false;
}
if ((key.alt ?? false) !== event.alt) {
return false;
}
if ((key.gui ?? false) !== event.gui) {
return false;
}
if ((key.shift ?? false) !== event.shift) {
return false;
}
return key.char === event.name;
};
export function styleTextForHotKey(text, key_) {
const key = toHotKeyDef(key_);
const alt = '⌥';
const shift = '⇧';
const ctrl = '⌃';
let mod = '';
if (key.ctrl) {
mod += ctrl;
}
if (key.alt) {
mod += alt;
}
if (key.gui) {
mod += '⌘';
}
if (key.shift) {
mod += shift;
}
if (!mod && text) {
return text;
}
mod = underline(mod + key.char);
if (!text) {
return mod;
}
return `${text} ${mod}`;
}
//# sourceMappingURL=key.js.map