3 const readline = require('readline');
4 const combos = require('./combos');
6 /* eslint-disable no-control-regex */
7 const metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/;
8 const fnKeyRe = /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/;
10 /* xterm/gnome ESC O letter */
15 /* xterm/rxvt ESC [ number ~ */
20 /* from Cygwin and used in libuv */
35 /* xterm ESC [ letter */
43 /* xterm/gnome ESC O letter */
51 /* xterm/rxvt ESC [ number ~ */
64 /* rxvt keys with modifiers */
94 function isShiftKey(code) {
95 return ['[a', '[b', '[c', '[d', '[e', '[2$', '[3$', '[5$', '[6$', '[7$', '[8$', '[Z'].includes(code)
98 function isCtrlKey(code) {
99 return [ 'Oa', 'Ob', 'Oc', 'Od', 'Oe', '[2^', '[3^', '[5^', '[6^', '[7^', '[8^'].includes(code)
102 const keypress = (s = '', event = {}) => {
115 if (Buffer.isBuffer(s)) {
116 if (s[0] > 127 && s[1] === void 0) {
118 s = '\x1b' + String(s);
122 } else if (s !== void 0 && typeof s !== 'string') {
125 s = key.sequence || '';
128 key.sequence = key.sequence || s || key.name;
134 } else if (s === '\n') {
135 // enter, should have been called linefeed
137 } else if (s === '\t') {
140 } else if (s === '\b' || s === '\x7f' || s === '\x1b\x7f' || s === '\x1b\b') {
141 // backspace or ctrl+h
142 key.name = 'backspace';
143 key.meta = s.charAt(0) === '\x1b';
144 } else if (s === '\x1b' || s === '\x1b\x1b') {
147 key.meta = s.length === 2;
148 } else if (s === ' ' || s === '\x1b ') {
150 key.meta = s.length === 2;
151 } else if (s <= '\x1a') {
153 key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
155 } else if (s.length === 1 && s >= '0' && s <= '9') {
158 } else if (s.length === 1 && s >= 'a' && s <= 'z') {
161 } else if (s.length === 1 && s >= 'A' && s <= 'Z') {
163 key.name = s.toLowerCase();
165 } else if ((parts = metaKeyCodeRe.exec(s))) {
166 // meta+character key
168 key.shift = /^[A-Z]$/.test(parts[1]);
169 } else if ((parts = fnKeyRe.exec(s))) {
172 if (segs[0] === '\u001b' && segs[1] === '\u001b') {
176 // ansi escape sequence
177 // reassemble the key code leaving out leading \x1b's,
178 // the modifier key bitflag and any meaningless "1;" sequence
179 let code = [parts[1], parts[2], parts[4], parts[6]].filter(Boolean).join('');
180 let modifier = (parts[3] || parts[5] || 1) - 1;
182 // Parse the key modifier
183 key.ctrl = !!(modifier & 4);
184 key.meta = !!(modifier & 10);
185 key.shift = !!(modifier & 1);
188 key.name = keyName[code];
189 key.shift = isShiftKey(code) || key.shift;
190 key.ctrl = isCtrlKey(code) || key.ctrl;
195 keypress.listen = (options = {}, onKeypress) => {
196 let { stdin } = options;
198 if (!stdin || (stdin !== process.stdin && !stdin.isTTY)) {
199 throw new Error('Invalid stream passed');
202 let rl = readline.createInterface({ terminal: true, input: stdin });
203 readline.emitKeypressEvents(stdin, rl);
205 let on = (buf, key) => onKeypress(buf, keypress(buf, key), rl);
206 let isRaw = stdin.isRaw;
208 if (stdin.isTTY) stdin.setRawMode(true);
209 stdin.on('keypress', on);
213 if (stdin.isTTY) stdin.setRawMode(isRaw);
214 stdin.removeListener('keypress', on);
222 keypress.action = (buf, key, customActions) => {
223 let obj = { ...combos, ...customActions };
225 key.action = obj.ctrl[key.name];
229 if (key.option && obj.option) {
230 key.action = obj.option[key.name];
235 key.action = obj.shift[key.name];
239 key.action = obj.keys[key.name];
243 module.exports = keypress;