2010-11-27 03:33:29 +01:00
|
|
|
// Can't test this when 'make test' doesn't assign a tty to the stdout.
|
|
|
|
// Yet another use-case for require('tty').spawn ?
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
2010-12-05 23:33:52 +01:00
|
|
|
var assert = require('assert');
|
|
|
|
var readline = require('readline');
|
2010-08-22 05:55:34 +02:00
|
|
|
|
|
|
|
var key = {
|
|
|
|
xterm: {
|
2014-05-12 05:23:21 +02:00
|
|
|
home: ['\x1b[H', {ctrl: true, name: 'a'}],
|
|
|
|
end: ['\x1b[F', {ctrl: true, name: 'e'}],
|
|
|
|
metab: ['\x1bb', {meta: true, name: 'b'}],
|
|
|
|
metaf: ['\x1bf', {meta: true, name: 'f'}],
|
|
|
|
metad: ['\x1bd', {meta: true, name: 'd'}]
|
2010-08-22 05:55:34 +02:00
|
|
|
},
|
|
|
|
gnome: {
|
2014-05-12 05:23:21 +02:00
|
|
|
home: ['\x1bOH', {ctrl: true, name: 'a'}],
|
|
|
|
end: ['\x1bOF', {ctrl: true, name: 'e'}]
|
2010-08-22 05:55:34 +02:00
|
|
|
},
|
|
|
|
rxvt: {
|
2014-05-12 05:23:21 +02:00
|
|
|
home: ['\x1b[7', {ctrl: true, name: 'a'}],
|
|
|
|
end: ['\x1b[8', {ctrl: true, name: 'e'}]
|
2010-08-23 19:21:27 +02:00
|
|
|
},
|
|
|
|
putty: {
|
2014-05-12 05:23:21 +02:00
|
|
|
home: ['\x1b[1~', {ctrl: true, name: 'a'}],
|
|
|
|
end: ['\x1b[>~', {ctrl: true, name: 'e'}]
|
2010-08-22 05:55:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-22 07:50:40 +02:00
|
|
|
var readlineFakeStream = function() {
|
|
|
|
var written_bytes = [];
|
2010-12-05 23:33:52 +01:00
|
|
|
var rl = readline.createInterface(
|
2014-05-12 05:23:21 +02:00
|
|
|
{ input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
completer: function(text) {
|
|
|
|
return [[], ''];
|
2010-12-05 23:33:52 +01:00
|
|
|
}
|
|
|
|
});
|
2014-05-12 05:23:21 +02:00
|
|
|
var _stdoutWrite = process.stdout.write;
|
|
|
|
process.stdout.write = function (data) {
|
|
|
|
data.split('').forEach(rl.written_bytes.push.bind(rl.written_bytes));
|
|
|
|
_stdoutWrite.apply(this, arguments);
|
|
|
|
}
|
2010-08-22 07:50:40 +02:00
|
|
|
rl.written_bytes = written_bytes;
|
|
|
|
return rl;
|
2010-08-22 05:55:34 +02:00
|
|
|
};
|
|
|
|
|
2010-08-22 07:50:40 +02:00
|
|
|
var rl = readlineFakeStream();
|
|
|
|
var written_bytes_length, refreshed;
|
2010-08-22 05:55:34 +02:00
|
|
|
|
|
|
|
rl.write('foo');
|
|
|
|
assert.equal(3, rl.cursor);
|
2014-05-12 05:23:21 +02:00
|
|
|
[key.xterm, key.rxvt, key.gnome, key.putty].forEach(function (key) {
|
|
|
|
rl.write.apply(rl, key.home);
|
|
|
|
assert.equal(0, rl.cursor);
|
|
|
|
rl.write.apply(rl, key.end);
|
|
|
|
assert.equal(3, rl.cursor);
|
|
|
|
});
|
2010-08-22 07:50:40 +02:00
|
|
|
|
|
|
|
rl = readlineFakeStream();
|
|
|
|
rl.write('foo bar.hop/zoo');
|
2014-05-12 05:23:21 +02:00
|
|
|
rl.write.apply(rl, key.xterm.home);
|
|
|
|
[
|
|
|
|
{cursor: 4, key: key.xterm.metaf},
|
|
|
|
{cursor: 7, key: key.xterm.metaf},
|
|
|
|
{cursor: 8, key: key.xterm.metaf},
|
|
|
|
{cursor: 11, key: key.xterm.metaf},
|
|
|
|
{cursor: 12, key: key.xterm.metaf},
|
|
|
|
{cursor: 15, key: key.xterm.metaf},
|
|
|
|
{cursor: 12, key: key.xterm.metab},
|
|
|
|
{cursor: 11, key: key.xterm.metab},
|
|
|
|
{cursor: 8, key: key.xterm.metab},
|
|
|
|
{cursor: 7, key: key.xterm.metab},
|
|
|
|
{cursor: 4, key: key.xterm.metab},
|
|
|
|
{cursor: 0, key: key.xterm.metab},
|
|
|
|
].forEach(function (action) {
|
|
|
|
written_bytes_length = rl.written_bytes.length;
|
|
|
|
rl.write.apply(rl, action.key);
|
|
|
|
assert.equal(action.cursor, rl.cursor);
|
|
|
|
refreshed = written_bytes_length !== rl.written_bytes.length;
|
|
|
|
assert.equal(true, refreshed);
|
|
|
|
});
|
2010-08-25 10:18:24 +02:00
|
|
|
|
|
|
|
rl = readlineFakeStream();
|
|
|
|
rl.write('foo bar.hop/zoo');
|
2014-05-12 05:23:21 +02:00
|
|
|
rl.write.apply(rl, key.xterm.home);
|
|
|
|
['bar.hop/zoo', '.hop/zoo', 'hop/zoo', '/zoo', 'zoo', ''].forEach(function (expectedLine) {
|
|
|
|
rl.write.apply(rl, key.xterm.metad);
|
|
|
|
assert.equal(0, rl.cursor);
|
|
|
|
assert.equal(expectedLine, rl.line);
|
|
|
|
});
|
|
|
|
rl.close();
|