• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const PassThrough = require('stream').PassThrough;
4const assert = require('assert');
5const Interface = require('readline').Interface;
6
7class FakeInput extends PassThrough {}
8
9function extend(k) {
10  return Object.assign({ ctrl: false, meta: false, shift: false }, k);
11}
12
13
14const fi = new FakeInput();
15const fo = new FakeInput();
16new Interface({ input: fi, output: fo, terminal: true });
17
18let keys = [];
19fi.on('keypress', (s, k) => {
20  keys.push(k);
21});
22
23
24function addTest(sequences, expectedKeys) {
25  if (!Array.isArray(sequences)) {
26    sequences = [ sequences ];
27  }
28
29  if (!Array.isArray(expectedKeys)) {
30    expectedKeys = [ expectedKeys ];
31  }
32
33  expectedKeys = expectedKeys.map(extend);
34
35  keys = [];
36
37  sequences.forEach((sequence) => {
38    fi.write(sequence);
39  });
40  assert.deepStrictEqual(keys, expectedKeys);
41}
42
43// Simulate key interval test cases
44// Returns a function that takes `next` test case and returns a thunk
45// that can be called to run tests in sequence
46// e.g.
47// addKeyIntervalTest(..)
48//   (addKeyIntervalTest(..)
49//      (addKeyIntervalTest(..)(noop)))()
50// where noop is a terminal function(() => {}).
51
52const addKeyIntervalTest = (sequences, expectedKeys, interval = 550,
53                            assertDelay = 550) => {
54  const fn = common.mustCall((next) => () => {
55
56    if (!Array.isArray(sequences)) {
57      sequences = [ sequences ];
58    }
59
60    if (!Array.isArray(expectedKeys)) {
61      expectedKeys = [ expectedKeys ];
62    }
63
64    expectedKeys = expectedKeys.map(extend);
65
66    const keys = [];
67    fi.on('keypress', (s, k) => keys.push(k));
68
69    const emitKeys = ([head, ...tail]) => {
70      if (head) {
71        fi.write(head);
72        setTimeout(() => emitKeys(tail), interval);
73      } else {
74        setTimeout(() => {
75          next();
76          assert.deepStrictEqual(keys, expectedKeys);
77        }, assertDelay);
78      }
79    };
80    emitKeys(sequences);
81  });
82  return fn;
83};
84
85// Regular alphanumerics
86addTest('io.JS', [
87  { name: 'i', sequence: 'i' },
88  { name: 'o', sequence: 'o' },
89  { name: undefined, sequence: '.' },
90  { name: 'j', sequence: 'J', shift: true },
91  { name: 's', sequence: 'S', shift: true },
92]);
93
94// Named characters
95addTest('\n\r\t\x1b\n\x1b\r\x1b\t', [
96  { name: 'enter', sequence: '\n' },
97  { name: 'return', sequence: '\r' },
98  { name: 'tab', sequence: '\t' },
99  { name: 'enter', sequence: '\x1b\n', meta: true },
100  { name: 'return', sequence: '\x1b\r', meta: true },
101  { name: 'tab', sequence: '\x1b\t', meta: true },
102]);
103
104// Space and backspace
105addTest('\b\x7f\x1b\b\x1b\x7f\x1b\x1b  \x1b ', [
106  { name: 'backspace', sequence: '\b' },
107  { name: 'backspace', sequence: '\x7f' },
108  { name: 'backspace', sequence: '\x1b\b', meta: true },
109  { name: 'backspace', sequence: '\x1b\x7f', meta: true },
110  { name: 'space', sequence: '\x1b\x1b ', meta: true },
111  { name: 'space', sequence: ' ' },
112  { name: 'space', sequence: '\x1b ', meta: true },
113]);
114
115// Escape key
116addTest('\x1b\x1b\x1b', [
117  { name: 'escape', sequence: '\x1b\x1b\x1b', meta: true },
118]);
119
120// Escape sequence
121addTest('\x1b]', [{ name: undefined, sequence: '\x1B]', meta: true }]);
122
123// Control keys
124addTest('\x01\x0b\x10', [
125  { name: 'a', sequence: '\x01', ctrl: true },
126  { name: 'k', sequence: '\x0b', ctrl: true },
127  { name: 'p', sequence: '\x10', ctrl: true },
128]);
129
130// Alt keys
131addTest('a\x1baA\x1bA', [
132  { name: 'a', sequence: 'a' },
133  { name: 'a', sequence: '\x1ba', meta: true },
134  { name: 'a', sequence: 'A', shift: true },
135  { name: 'a', sequence: '\x1bA', meta: true, shift: true },
136]);
137
138// xterm/gnome ESC [ letter (with modifiers)
139addTest('\x1b[2P\x1b[3P\x1b[4P\x1b[5P\x1b[6P\x1b[7P\x1b[8P\x1b[3Q\x1b[8Q\x1b[3R\x1b[8R\x1b[3S\x1b[8S', [
140  { name: 'f1', sequence: '\x1b[2P', code: '[P', shift: true, meta: false, ctrl: false },
141  { name: 'f1', sequence: '\x1b[3P', code: '[P', shift: false, meta: true, ctrl: false },
142  { name: 'f1', sequence: '\x1b[4P', code: '[P', shift: true, meta: true, ctrl: false },
143  { name: 'f1', sequence: '\x1b[5P', code: '[P', shift: false, meta: false, ctrl: true },
144  { name: 'f1', sequence: '\x1b[6P', code: '[P', shift: true, meta: false, ctrl: true },
145  { name: 'f1', sequence: '\x1b[7P', code: '[P', shift: false, meta: true, ctrl: true },
146  { name: 'f1', sequence: '\x1b[8P', code: '[P', shift: true, meta: true, ctrl: true },
147  { name: 'f2', sequence: '\x1b[3Q', code: '[Q', meta: true },
148  { name: 'f2', sequence: '\x1b[8Q', code: '[Q', shift: true, meta: true, ctrl: true },
149  { name: 'f3', sequence: '\x1b[3R', code: '[R', meta: true },
150  { name: 'f3', sequence: '\x1b[8R', code: '[R', shift: true, meta: true, ctrl: true },
151  { name: 'f4', sequence: '\x1b[3S', code: '[S', meta: true },
152  { name: 'f4', sequence: '\x1b[8S', code: '[S', shift: true, meta: true, ctrl: true },
153]);
154
155// xterm/gnome ESC O letter
156addTest('\x1bOP\x1bOQ\x1bOR\x1bOS', [
157  { name: 'f1', sequence: '\x1bOP', code: 'OP' },
158  { name: 'f2', sequence: '\x1bOQ', code: 'OQ' },
159  { name: 'f3', sequence: '\x1bOR', code: 'OR' },
160  { name: 'f4', sequence: '\x1bOS', code: 'OS' },
161]);
162
163// xterm/rxvt ESC [ number ~ */
164addTest('\x1b[11~\x1b[12~\x1b[13~\x1b[14~', [
165  { name: 'f1', sequence: '\x1b[11~', code: '[11~' },
166  { name: 'f2', sequence: '\x1b[12~', code: '[12~' },
167  { name: 'f3', sequence: '\x1b[13~', code: '[13~' },
168  { name: 'f4', sequence: '\x1b[14~', code: '[14~' },
169]);
170
171// From Cygwin and used in libuv
172addTest('\x1b[[A\x1b[[B\x1b[[C\x1b[[D\x1b[[E', [
173  { name: 'f1', sequence: '\x1b[[A', code: '[[A' },
174  { name: 'f2', sequence: '\x1b[[B', code: '[[B' },
175  { name: 'f3', sequence: '\x1b[[C', code: '[[C' },
176  { name: 'f4', sequence: '\x1b[[D', code: '[[D' },
177  { name: 'f5', sequence: '\x1b[[E', code: '[[E' },
178]);
179
180// Common
181addTest('\x1b[15~\x1b[17~\x1b[18~\x1b[19~\x1b[20~\x1b[21~\x1b[23~\x1b[24~', [
182  { name: 'f5', sequence: '\x1b[15~', code: '[15~' },
183  { name: 'f6', sequence: '\x1b[17~', code: '[17~' },
184  { name: 'f7', sequence: '\x1b[18~', code: '[18~' },
185  { name: 'f8', sequence: '\x1b[19~', code: '[19~' },
186  { name: 'f9', sequence: '\x1b[20~', code: '[20~' },
187  { name: 'f10', sequence: '\x1b[21~', code: '[21~' },
188  { name: 'f11', sequence: '\x1b[23~', code: '[23~' },
189  { name: 'f12', sequence: '\x1b[24~', code: '[24~' },
190]);
191
192// xterm ESC [ letter
193addTest('\x1b[A\x1b[B\x1b[C\x1b[D\x1b[E\x1b[F\x1b[H', [
194  { name: 'up', sequence: '\x1b[A', code: '[A' },
195  { name: 'down', sequence: '\x1b[B', code: '[B' },
196  { name: 'right', sequence: '\x1b[C', code: '[C' },
197  { name: 'left', sequence: '\x1b[D', code: '[D' },
198  { name: 'clear', sequence: '\x1b[E', code: '[E' },
199  { name: 'end', sequence: '\x1b[F', code: '[F' },
200  { name: 'home', sequence: '\x1b[H', code: '[H' },
201]);
202
203// xterm/gnome ESC O letter
204addTest('\x1bOA\x1bOB\x1bOC\x1bOD\x1bOE\x1bOF\x1bOH', [
205  { name: 'up', sequence: '\x1bOA', code: 'OA' },
206  { name: 'down', sequence: '\x1bOB', code: 'OB' },
207  { name: 'right', sequence: '\x1bOC', code: 'OC' },
208  { name: 'left', sequence: '\x1bOD', code: 'OD' },
209  { name: 'clear', sequence: '\x1bOE', code: 'OE' },
210  { name: 'end', sequence: '\x1bOF', code: 'OF' },
211  { name: 'home', sequence: '\x1bOH', code: 'OH' },
212]);
213
214// Old xterm shift-arrows
215addTest('\x1bO2A\x1bO2B', [
216  { name: 'up', sequence: '\x1bO2A', code: 'OA', shift: true },
217  { name: 'down', sequence: '\x1bO2B', code: 'OB', shift: true },
218]);
219
220// xterm/rxvt ESC [ number ~
221addTest('\x1b[1~\x1b[2~\x1b[3~\x1b[4~\x1b[5~\x1b[6~', [
222  { name: 'home', sequence: '\x1b[1~', code: '[1~' },
223  { name: 'insert', sequence: '\x1b[2~', code: '[2~' },
224  { name: 'delete', sequence: '\x1b[3~', code: '[3~' },
225  { name: 'end', sequence: '\x1b[4~', code: '[4~' },
226  { name: 'pageup', sequence: '\x1b[5~', code: '[5~' },
227  { name: 'pagedown', sequence: '\x1b[6~', code: '[6~' },
228]);
229
230// putty
231addTest('\x1b[[5~\x1b[[6~', [
232  { name: 'pageup', sequence: '\x1b[[5~', code: '[[5~' },
233  { name: 'pagedown', sequence: '\x1b[[6~', code: '[[6~' },
234]);
235
236// rxvt
237addTest('\x1b[7~\x1b[8~', [
238  { name: 'home', sequence: '\x1b[7~', code: '[7~' },
239  { name: 'end', sequence: '\x1b[8~', code: '[8~' },
240]);
241
242// gnome terminal
243addTest('\x1b[A\x1b[B\x1b[2A\x1b[2B', [
244  { name: 'up', sequence: '\x1b[A', code: '[A' },
245  { name: 'down', sequence: '\x1b[B', code: '[B' },
246  { name: 'up', sequence: '\x1b[2A', code: '[A', shift: true },
247  { name: 'down', sequence: '\x1b[2B', code: '[B', shift: true },
248]);
249
250// `rxvt` keys with modifiers.
251addTest('\x1b[20~\x1b[2$\x1b[2^\x1b[3$\x1b[3^\x1b[5$\x1b[5^\x1b[6$\x1b[6^\x1b[7$\x1b[7^\x1b[8$\x1b[8^', [
252  { name: 'f9', sequence: '\x1b[20~', code: '[20~' },
253  { name: 'insert', sequence: '\x1b[2$', code: '[2$', shift: true },
254  { name: 'insert', sequence: '\x1b[2^', code: '[2^', ctrl: true },
255  { name: 'delete', sequence: '\x1b[3$', code: '[3$', shift: true },
256  { name: 'delete', sequence: '\x1b[3^', code: '[3^', ctrl: true },
257  { name: 'pageup', sequence: '\x1b[5$', code: '[5$', shift: true },
258  { name: 'pageup', sequence: '\x1b[5^', code: '[5^', ctrl: true },
259  { name: 'pagedown', sequence: '\x1b[6$', code: '[6$', shift: true },
260  { name: 'pagedown', sequence: '\x1b[6^', code: '[6^', ctrl: true },
261  { name: 'home', sequence: '\x1b[7$', code: '[7$', shift: true },
262  { name: 'home', sequence: '\x1b[7^', code: '[7^', ctrl: true },
263  { name: 'end', sequence: '\x1b[8$', code: '[8$', shift: true },
264  { name: 'end', sequence: '\x1b[8^', code: '[8^', ctrl: true },
265]);
266
267// Misc
268addTest('\x1b[Z', [
269  { name: 'tab', sequence: '\x1b[Z', code: '[Z', shift: true },
270]);
271
272// xterm + modifiers
273addTest('\x1b[20;5~\x1b[6;5^', [
274  { name: 'f9', sequence: '\x1b[20;5~', code: '[20~', ctrl: true },
275  { name: 'pagedown', sequence: '\x1b[6;5^', code: '[6^', ctrl: true },
276]);
277
278addTest('\x1b[H\x1b[5H\x1b[1;5H', [
279  { name: 'home', sequence: '\x1b[H', code: '[H' },
280  { name: 'home', sequence: '\x1b[5H', code: '[H', ctrl: true },
281  { name: 'home', sequence: '\x1b[1;5H', code: '[H', ctrl: true },
282]);
283
284// Escape sequences broken into multiple data chunks
285addTest('\x1b[D\x1b[C\x1b[D\x1b[C'.split(''), [
286  { name: 'left', sequence: '\x1b[D', code: '[D' },
287  { name: 'right', sequence: '\x1b[C', code: '[C' },
288  { name: 'left', sequence: '\x1b[D', code: '[D' },
289  { name: 'right', sequence: '\x1b[C', code: '[C' },
290]);
291
292// Escape sequences mixed with regular ones
293addTest('\x1b[DD\x1b[2DD\x1b[2^D', [
294  { name: 'left', sequence: '\x1b[D', code: '[D' },
295  { name: 'd', sequence: 'D', shift: true },
296  { name: 'left', sequence: '\x1b[2D', code: '[D', shift: true },
297  { name: 'd', sequence: 'D', shift: true },
298  { name: 'insert', sequence: '\x1b[2^', code: '[2^', ctrl: true },
299  { name: 'd', sequence: 'D', shift: true },
300]);
301
302// Color sequences
303addTest('\x1b[31ma\x1b[39ma', [
304  { name: 'undefined', sequence: '\x1b[31m', code: '[31m' },
305  { name: 'a', sequence: 'a' },
306  { name: 'undefined', sequence: '\x1b[39m', code: '[39m' },
307  { name: 'a', sequence: 'a' },
308]);
309
310// `rxvt` keys with modifiers.
311addTest('\x1b[a\x1b[b\x1b[c\x1b[d\x1b[e', [
312  { name: 'up', sequence: '\x1b[a', code: '[a', shift: true },
313  { name: 'down', sequence: '\x1b[b', code: '[b', shift: true },
314  { name: 'right', sequence: '\x1b[c', code: '[c', shift: true },
315  { name: 'left', sequence: '\x1b[d', code: '[d', shift: true },
316  { name: 'clear', sequence: '\x1b[e', code: '[e', shift: true },
317]);
318
319addTest('\x1bOa\x1bOb\x1bOc\x1bOd\x1bOe', [
320  { name: 'up', sequence: '\x1bOa', code: 'Oa', ctrl: true },
321  { name: 'down', sequence: '\x1bOb', code: 'Ob', ctrl: true },
322  { name: 'right', sequence: '\x1bOc', code: 'Oc', ctrl: true },
323  { name: 'left', sequence: '\x1bOd', code: 'Od', ctrl: true },
324  { name: 'clear', sequence: '\x1bOe', code: 'Oe', ctrl: true },
325]);
326
327// Reduce array of addKeyIntervalTest(..) right to left
328// with () => {} as initial function.
329const runKeyIntervalTests = [
330  // Escape character
331  addKeyIntervalTest('\x1b', [
332    { name: 'escape', sequence: '\x1b', meta: true },
333  ]),
334  // Chain of escape characters.
335  addKeyIntervalTest('\x1b\x1b\x1b\x1b'.split(''), [
336    { name: 'escape', sequence: '\x1b', meta: true },
337    { name: 'escape', sequence: '\x1b', meta: true },
338    { name: 'escape', sequence: '\x1b', meta: true },
339    { name: 'escape', sequence: '\x1b', meta: true },
340  ]),
341].reverse().reduce((acc, fn) => fn(acc), () => {});
342
343// Run key interval tests one after another.
344runKeyIntervalTests();
345