11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst { PassThrough } = require('stream');
41cb0ef41Sopenharmony_ciconst readline = require('readline');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cicommon.skipIfDumbTerminal();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci{
101cb0ef41Sopenharmony_ci  const input = new PassThrough();
111cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
121cb0ef41Sopenharmony_ci    terminal: true,
131cb0ef41Sopenharmony_ci    input: input
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  rl.on('line', common.mustCall((data) => {
171cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'abc');
181cb0ef41Sopenharmony_ci  }));
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  input.end('abc');
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci{
241cb0ef41Sopenharmony_ci  const input = new PassThrough();
251cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
261cb0ef41Sopenharmony_ci    terminal: true,
271cb0ef41Sopenharmony_ci    input: input
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  rl.on('line', common.mustNotCall('must not be called before newline'));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  input.write('abc');
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  const input = new PassThrough();
371cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
381cb0ef41Sopenharmony_ci    terminal: true,
391cb0ef41Sopenharmony_ci    input: input
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  rl.on('line', common.mustCall((data) => {
431cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'abc');
441cb0ef41Sopenharmony_ci  }));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  input.write('abc\n');
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const input = new PassThrough();
511cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
521cb0ef41Sopenharmony_ci    terminal: true,
531cb0ef41Sopenharmony_ci    input: input
541cb0ef41Sopenharmony_ci  });
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  rl.write('foo');
571cb0ef41Sopenharmony_ci  assert.strictEqual(rl.cursor, 3);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const key = {
601cb0ef41Sopenharmony_ci    xterm: {
611cb0ef41Sopenharmony_ci      home: ['\x1b[H', { ctrl: true, name: 'a' }],
621cb0ef41Sopenharmony_ci      end: ['\x1b[F', { ctrl: true, name: 'e' }],
631cb0ef41Sopenharmony_ci    },
641cb0ef41Sopenharmony_ci    gnome: {
651cb0ef41Sopenharmony_ci      home: ['\x1bOH', { ctrl: true, name: 'a' }],
661cb0ef41Sopenharmony_ci      end: ['\x1bOF', { ctrl: true, name: 'e' }]
671cb0ef41Sopenharmony_ci    },
681cb0ef41Sopenharmony_ci    rxvt: {
691cb0ef41Sopenharmony_ci      home: ['\x1b[7', { ctrl: true, name: 'a' }],
701cb0ef41Sopenharmony_ci      end: ['\x1b[8', { ctrl: true, name: 'e' }]
711cb0ef41Sopenharmony_ci    },
721cb0ef41Sopenharmony_ci    putty: {
731cb0ef41Sopenharmony_ci      home: ['\x1b[1~', { ctrl: true, name: 'a' }],
741cb0ef41Sopenharmony_ci      end: ['\x1b[>~', { ctrl: true, name: 'e' }]
751cb0ef41Sopenharmony_ci    }
761cb0ef41Sopenharmony_ci  };
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  [key.xterm, key.gnome, key.rxvt, key.putty].forEach(function(key) {
791cb0ef41Sopenharmony_ci    rl.write.apply(rl, key.home);
801cb0ef41Sopenharmony_ci    assert.strictEqual(rl.cursor, 0);
811cb0ef41Sopenharmony_ci    rl.write.apply(rl, key.end);
821cb0ef41Sopenharmony_ci    assert.strictEqual(rl.cursor, 3);
831cb0ef41Sopenharmony_ci  });
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci{
881cb0ef41Sopenharmony_ci  const input = new PassThrough();
891cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
901cb0ef41Sopenharmony_ci    terminal: true,
911cb0ef41Sopenharmony_ci    input: input
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  const key = {
951cb0ef41Sopenharmony_ci    xterm: {
961cb0ef41Sopenharmony_ci      home: ['\x1b[H', { ctrl: true, name: 'a' }],
971cb0ef41Sopenharmony_ci      metab: ['\x1bb', { meta: true, name: 'b' }],
981cb0ef41Sopenharmony_ci      metaf: ['\x1bf', { meta: true, name: 'f' }],
991cb0ef41Sopenharmony_ci    }
1001cb0ef41Sopenharmony_ci  };
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  rl.write('foo bar.hop/zoo');
1031cb0ef41Sopenharmony_ci  rl.write.apply(rl, key.xterm.home);
1041cb0ef41Sopenharmony_ci  [
1051cb0ef41Sopenharmony_ci    { cursor: 4, key: key.xterm.metaf },
1061cb0ef41Sopenharmony_ci    { cursor: 7, key: key.xterm.metaf },
1071cb0ef41Sopenharmony_ci    { cursor: 8, key: key.xterm.metaf },
1081cb0ef41Sopenharmony_ci    { cursor: 11, key: key.xterm.metaf },
1091cb0ef41Sopenharmony_ci    { cursor: 12, key: key.xterm.metaf },
1101cb0ef41Sopenharmony_ci    { cursor: 15, key: key.xterm.metaf },
1111cb0ef41Sopenharmony_ci    { cursor: 12, key: key.xterm.metab },
1121cb0ef41Sopenharmony_ci    { cursor: 11, key: key.xterm.metab },
1131cb0ef41Sopenharmony_ci    { cursor: 8, key: key.xterm.metab },
1141cb0ef41Sopenharmony_ci    { cursor: 7, key: key.xterm.metab },
1151cb0ef41Sopenharmony_ci    { cursor: 4, key: key.xterm.metab },
1161cb0ef41Sopenharmony_ci    { cursor: 0, key: key.xterm.metab },
1171cb0ef41Sopenharmony_ci  ].forEach(function(action) {
1181cb0ef41Sopenharmony_ci    rl.write.apply(rl, action.key);
1191cb0ef41Sopenharmony_ci    assert.strictEqual(rl.cursor, action.cursor);
1201cb0ef41Sopenharmony_ci  });
1211cb0ef41Sopenharmony_ci}
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci{
1241cb0ef41Sopenharmony_ci  const input = new PassThrough();
1251cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
1261cb0ef41Sopenharmony_ci    terminal: true,
1271cb0ef41Sopenharmony_ci    input: input
1281cb0ef41Sopenharmony_ci  });
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  const key = {
1311cb0ef41Sopenharmony_ci    xterm: {
1321cb0ef41Sopenharmony_ci      home: ['\x1b[H', { ctrl: true, name: 'a' }],
1331cb0ef41Sopenharmony_ci      metad: ['\x1bd', { meta: true, name: 'd' }]
1341cb0ef41Sopenharmony_ci    }
1351cb0ef41Sopenharmony_ci  };
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  rl.write('foo bar.hop/zoo');
1381cb0ef41Sopenharmony_ci  rl.write.apply(rl, key.xterm.home);
1391cb0ef41Sopenharmony_ci  [
1401cb0ef41Sopenharmony_ci    'bar.hop/zoo',
1411cb0ef41Sopenharmony_ci    '.hop/zoo',
1421cb0ef41Sopenharmony_ci    'hop/zoo',
1431cb0ef41Sopenharmony_ci    '/zoo',
1441cb0ef41Sopenharmony_ci    'zoo',
1451cb0ef41Sopenharmony_ci    '',
1461cb0ef41Sopenharmony_ci  ].forEach(function(expectedLine) {
1471cb0ef41Sopenharmony_ci    rl.write.apply(rl, key.xterm.metad);
1481cb0ef41Sopenharmony_ci    assert.strictEqual(rl.cursor, 0);
1491cb0ef41Sopenharmony_ci    assert.strictEqual(rl.line, expectedLine);
1501cb0ef41Sopenharmony_ci  });
1511cb0ef41Sopenharmony_ci}
152