2012-08-22 25 views
4

我工作的一个简单的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'); 

而且comman d行提示最后显示:

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:     _ 

此时退格不起作用。

回答

0

当然,您需要修改您的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,并在最后一行DD代表要移回的字符数。 11显然是字符的数量。

为所有的乐趣终端将看到这个:http://en.wikipedia.org/wiki/ANSI_escape_code

+0

没有喜悦它没有任何效果,它不会出现是正常的字符,请看到我的编辑 – JaredMcAteer

4

当你调用rl.setPrompt(prompt, length)没有它的第二个参数,the internal _promptLength variable is set to the length of the prompt string; ANSI X3.64转义序列是而不是的解释。内部_getCursorPos方法计算光标位置从_promptLength] [3];因此,在长度中包含换码序列会导致游标位置远离应有的位置。

要完全解决此问题,当设置_promptLength时,Node的readline库应该解析ANSI转义序列。要解决此问题,可以手动计算不带转义序列的提示字符串的长度,并将其作为第二个参数传递给rl.setPrompt(prompt, length)

+0

它似乎并不'setPrompt'影响'question'可言。我会重构使用更详细的方法,看看是否有效。 – JaredMcAteer

2

我也遇到过类似的问题。 Basil Crow在问题原因方面的出色答案是正确的,因为它确实是导致长度故障的ANSI转义序列;然而,Interface.setPrompt()不只是问题 - 它是解决方案

似乎这个长度的误读(我巧妙地在bash中玩耍的时候避免的)影响整个接口对象的写入过程,即当未指定长度参数时,任何以任何容量调用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()通过两个写入字符串,字符串的真实长度为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); 
}); 


两者的结果都是一样的:output of the above scripts

或者你可以两者都做。或者,您可以直接修改readline.Interface.prototype(并且全局应用修补程序),而不是自己的对象实例。这里有很多选择。

希望这可以帮助别人!

还可以编辑,请参见:https://github.com/joyent/node/issues/3860