2012-08-22 28 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