2013-07-02 9 views
5

コンソールの2行にデータを表示したいとします。毎回2つの行を更新したいだけです。私が今まで行ってきた何コンソールの複数行のデータを更新する方法

がある -

var _logInline = function(alpha, bravo) { 
    process.stdout.cursorTo(0, 0); 
    process.stdout.clearLine(); 
    process.stdout.cursorTo(0); 
    process.stdout.write(alpha.toString()); 
    process.stdout.write('\n'); 
    process.stdout.clearLine(); 
    process.stdout.cursorTo(0); 
    process.stdout.write(bravo.toString()); 
    process.stdout.write('\n'); 

}; 

var delay = 1000; 
var time = 0; 
setInterval(function() { 
    time++; 
    _logInline('alpha-' + time, 'bravo-' + time * time); 
}, delay); 

この溶液を用いて明らかな問題は、カーソルがウィンドウの上部に行くということです。私はそれを望んでいない、代わりにカーソルが今のところどこにあるコンテンツを表示する必要があります。おそらく私は自分のロジックの中で現在のカーソルの位置を取得する必要があります。それを行う方法はありますか?私は改行せずにログのオプションを与えるstackoverflowの上のいくつかの質問を見てきましたが、これは正確ではありません :

代替と最も好ましい解決策は同じことに

EDITを行うことができますlibが取得することです私が望むもの私は複数の改行なしのロギングが必要です。

+0

あなたはカーソル位置を取得することができます( [これを見るgist](https://gist.github.com/viatropos/3765464))をbashで実行しますが、ウィンドウでは動作しません。私が見つけたもっとも簡単な解決策は、http://pastebin.com/y69by2QEです(ただし、 'cursorTo(0、0)'を使用してください)。 –

答えて

0

ncursesのは、私は、端末を制御するために使用された中で最も強力なライブラリである、Cライブラリhttps://npmjs.org/package/ncurses

に結合するが、それはあなたのニーズのために少しやり過ぎだかもしれMSCDEXによって優れたNPMパッケージがあり、ここにありますへのexec権限を与えることを忘れないでください、あなたはdownload it from gistかここでそれを読むことができthis gistに基づいて

私が一緒に置かれている次のコードは、あなたの例に適応し、 :代替ソリューションは、それはbashスクリプトを使用する必要がなく、 bashスクリプト:

chmod +x cursor-position.sh 

カーソル-position.js

module.exports = function(callback) { 
    require('child_process').exec('./cursor-position.sh', function(error, stdout, stderr){ 
    callback(error, JSON.parse(stdout)); 
    }); 
} 

cursor-position.sh

#!/bin/bash 
# based on a script from http://invisible-island.net/xterm/xterm.faq.html 
# http://stackoverflow.com/questions/2575037/how-to-get-the-cursor-position-in-bash 
exec < /dev/tty 
oldstty=$(stty -g) 
stty raw -echo min 0 
# on my system, the following line can be replaced by the line below it 
echo -en "\033[6n" > /dev/tty 
# tput u7 > /dev/tty # when TERM=xterm (and relatives) 
IFS=';' read -r -d R -a pos 
stty $oldstty 
# change from one-based to zero based so they work with: tput cup $row $col 
row=$((${pos[0]:2} - 1)) # strip off the esc-[ 
col=$((${pos[1]} - 1)) 
echo \{\"row\":$row,\"column\":$col\} 

index.js

var getCursorPosition = require('./cursor-position'); 

var _logInline = function(row, msg) { 
    if(row >= 0) row --; //litle correction 
    process.stdout.cursorTo(0, row); 
    process.stdout.clearLine(); 
    process.stdout.cursorTo(0, row); 
    process.stdout.write(msg.toString()); 
}; 

var delay = 1000; 
var time = 0; 

//Start by getting the current position 
getCursorPosition(function(error, init) { 
    setInterval(function() { 
     time++; 
     _logInline(init.row, 'alpha-' + time); 
     _logInline(init.row + 1, 'bravo-' + time * time); 
    }, delay); 
}); 
関連する問題