Jar*_*eer 5 javascript ansi readline command-line-interface node.js
我正在编写一个简单的cli脚本,并希望为以下代码添加一些颜色:
rl.question('Enter destination path: ', function(answer) {
// ...
});
rl.write('/home/' + user + '/bin');
Run Code Online (Sandbox Code Playgroud)
哪个显示在终端中:
Enter destination path: /home/jmcateer/bin_
Run Code Online (Sandbox Code Playgroud)
但我想在提示中添加一些颜色,我做了以下操作:
rl.question('\u001b[1;36mEnter destination path:\u001b[0m ', function(answer) {
});
rl.write('/home/' + user + '/bin');
Run Code Online (Sandbox Code Playgroud)
并且命令行提示符最终显示:
Enter destination path: /home/jmcateer/bin_
Run Code Online (Sandbox Code Playgroud)
它的工作原理但是我有更多的空白空间我不喜欢.有没有人对如何处理这个有任何想法?
编辑:
我无法通过退格来删除空白区域...当我尝试使用退格键时,白色空间会像这样跳到另一端
Enter destination path: /home/jmcateer/bin_
Enter destination path: /home/jmcateer/bi _
Enter destination path: /home/jmcateer/b _
...
Enter destination path: _
Run Code Online (Sandbox Code Playgroud)
此时退格无效.
当然,您需要rl.write使用 CSI 序列修改字符串n D,其中nCSI 序列是将光标向后移动的字符数。
这是一个用于实验的片段:
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');
Run Code Online (Sandbox Code Playgroud)
注意到最后一行中的11和 the 了吗?D代表D向后移动的字符数。11显然是字符数。
有关所有有趣的终端代码,请参阅此:http://en.wikipedia.org/wiki/ANSI_escape_code