我工作的一个简单的CLI脚本,想一些颜色添加到下面的代码:
rl.question('Enter destination path: ', function(answer) {
// ...
});
rl.write('/home/' + user + '/bin');
其显示在终端:
Enter destination path: /home/jmcateer/bin_
但我想一些颜色添加到提示我做了以下内容:
rl.question('\u001b[1;36mEnter destination path:\u001b[0m ', function(answer) {
});
rl.write('/home/' + user + '/bin');
和命令行提示符最终显示:
Enter destination path: /home/jmcateer/bin_
它的工作原理,但有我宁愿不存在的空白,数额巨大。 有没有人对如何处理这个任何想法?
编辑:
我无法通过它退格键删除空白......当我尝试使用退格键的白色空间跳转到另一端,像这样
Enter destination path: /home/jmcateer/bin_
Enter destination path: /home/jmcateer/bi _
Enter destination path: /home/jmcateer/b _
...
Enter destination path: _
在这一点上退格键不起作用。
当你调用rl.setPrompt(prompt, length)
没有它的第二个参数, 所述内部_promptLength
变量设置为提示的字符串的长度 ; ANSI X3.64转义序列不会被解释。 内部_getCursorPos
方法计算从光标位置_promptLength
] [3]; 如这样,在光标的长度导致的转义序列的包含被定位进一步远离比它应该的。
为了充分解决这个问题,节点的输入库应该在设置时解析ANSI转义序列_promptLength
。 要解决此问题,可以手动计算提示的字符串的长度没有转义序列并传递作为第二个参数,以rl.setPrompt(prompt, length)
。
我也遇到了类似的问题。 罗勒乌鸦是他在这个问题的确是被造成长毛刺ANSI转义序列的原因优秀的答案正确; 然而, Interface.setPrompt()
不只是这个问题,这是解决方案 !
看来,这个误读长度的(我的东西,而在bash玩弄巧妙地回避) 影响整个界面对象的WriteOut的过程 ,即任何调用Interface.setPrompt()
以任何身分长度参数保留未指定时,将困扰。
为了克服这个问题,你可以做两件事情之一:
重新定义Interface.setPrompt()
总是使得类似方法指定提示输出的长度Interface.question()
函数适当地再次:
// I'm using the 'color' npm library for the sake of convenience. It is not required
// and you can use normal regex to strip color codes and what not.
var colors = require('colors'),
readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
/* Overcome some bugs in the Nodejs readline implementation */
rl._setPrompt = rl.setPrompt;
rl.setPrompt = function(prompt, length)
{
rl._setPrompt(prompt, length ? length : prompt.split(/[\r\n]/).pop().stripColors.length);
};
var str = '[' + '?'.green + '] Blackbeard walks under the black flag with a ____? ';
rl.question(str, function(answer)
{
var answ = 'scallywag swagger';
console.log(
'You answered "' + ((answer == answ) ? answer.green : answer.red)
+ '". The correct answer is', '"' + answ.green + '".');
});
重新定义Interface.write()
使用Interface.setPrompt()
通过两个WriteOut的串和串到setPrompt方法的真实长度:
var colors = require('colors'),
readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
/* Overcome some bugs in the Nodejs readline implementation */
rl._write = rl.write;
rl.write = function(d, key)
{
// "key" functionality is lost, but if you care enough you can add it back
rl.setPrompt(d, d.split(/[\r\n]/).pop().stripColors.length);
rl.prompt(true);
};
var str = '[' + '?'.green + '] Blackbeard walks under the black flag with a ____? ';
rl.write(str);
rl.on('line', function(answer)
{
var answ = 'scallywag swagger';
console.log(
'You answered "' + ((answer == answ) ? answer.green : answer.red)
+ '". The correct answer is', '"' + answ.green + '".');
rl.prompt(true);
});
两者的结果都是一样的:
或者你也可以两者都做。 或者你可以修改readline.Interface.prototype
直接(并已应用于全球您的固定)而不是对象实例本身的。 很多在这里的选项。
希望这可以帮助别人!
:也EDIT,见https://github.com/joyent/node/issues/3860
当然,你要修改rl.write
与CSI序列串n D
哪里n
是将你的光标回的字符数。
这里是一个片段进行实验:
var rl = require('readline').createInterface({input: process.stdin, output: process.stdout});
rl.question('\u001b[1;36mEnter destination path: \u001b[0m', function(answer) {
});
rl.write('\u001b[11 D/home/jp/bin');
注意11
和D
的最后一行? 该D
代表字符数回迁。 11
显然是那么的字符数。
看到这个对于所有的乐趣终端代码: http://en.wikipedia.org/wiki/ANSI_escape_code