2013-07-02 10 views
5

Desidero visualizzare i dati su due righe della console. Voglio solo aggiornare le due linee ogni volta.Come aggiornare i dati su più righe della console

Quello che ho fatto fino ad ora è -

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); 

Il problema evidente di questa soluzione è che il cursore va alla parte superiore della finestra. Non lo voglio, dovrebbe invece mostrare il contenuto dove sia il cursore in quel momento. Probabilmente ho bisogno di ottenere la posizione corrente del cursore prima nella mia logica. C'è un modo per farlo?

alternativa e la soluzione più preferito sarebbe quella di ottenere un lib che può fare la stessa cosa

EDIT: ho visto alcune domande su StackOverflow che danno la possibilità di registrazione senza nuova linea, ma questo non è esattamente quello che voglio. Voglio più registrazioni non nuove.

+0

È possibile ottenere la posizione del cursore ([guarda questo gist] (https://gist.github.com/viatropos/3765464)) con bash ma non funziona in Windows. La soluzione più semplice che ho trovato: http://pastebin.com/y69by2QE (ma usa 'cursorTo (0, 0)'). –

risposta

0

ncurses è il più potente libreria che ho usato per controllare il terminale, v'è un eccellente pacchetto di NPM da MSCDEX che si lega alla libreria C https://npmjs.org/package/ncurses

Ma può essere che è po 'eccessivo per le vostre esigenze, qui è una soluzione alternativa, ma comporta l'uso di uno script bash:

sulla base di this gist ho messo insieme il seguente codice adattato per il tuo esempio, è possibile download it from gist o leggere qui, non dimenticate di dare diritti exec al script bash con:

chmod +x cursor-position.sh 

cursore-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); 
}); 
Problemi correlati