2015-04-24 16 views
7

Per farla breve: voglio creare una CLI per un demone di sviluppo. Daemon emette diversi tipi di informazioni sullo stdout e voglio trasmettere tali informazioni all'utente in un'area dello schermo, in modo scorrevole. Sto lottando per far benedire lo stdout. Di seguito un semplice prototipo, che memorizza lo stdout, quindi le informazioni non sono mai complete.Come reindirizzare lo stdout in benedetto?

var blessed = require('blessed'); 

var screen = blessed.screen(), 
body = blessed.box({ 
    top: 1, 
    left: 0, 
    width: '100%', 
    height: '99%' 
}), 
statusbar = blessed.box({ 
    top: 0, 
    left: 0, 
    width: '100%', 
    height: 1, 
    style: { 
    fg: 'white', 
    bg: 'blue' 
    } 
}); 

screen.append(statusbar); 
screen.append(body); 

screen.key(['escape', 'q', 'C-c'], function(ch, key) { 
    return process.exit(0); 
}); 

function status(text) { statusbar.setContent(text); screen.render(); } 
function log(text) { body.insertLine(0, text); screen.render(); } 

status('TEST'); 

var spawn = require('child_process').spawn; 

yes = spawn('yes', ['it is']); 

ls.stdout.on('data', function (data) { 
    log(data.toString()); 
}); 

ls.stderr.on('data', function (data) { 
    log(data.toString()); 
}); 
+0

Cosa significa "ls" nel codice di esempio? Forse mi manca qualcosa, ma tutto quello che devi fare per ottenere 'stdout' dal tuo processo' yes' generato è ascoltare l'evento 'data' da 'yes.stdout' ... – mkhanoyan

+0

Ho bisogno di eseguire più comandi dopo l'altro , quindi "yes" =/bin/yes e "ls" =/bin/ls sto usando solo il nodo analogico, non ho generato un comando shell. – user1190411

risposta

0

La linea

var screen = blessed.screen() 

prende parametri per cambiare cui l'input viene, e dove l'uscita va troppo. Logicamente, sono chiamati ingresso e uscita:

var screen = blessed.screen({ input: some_input_stream, output: some_output_stream }) 

Quindi, basta chuck un flusso scrivibile (o duplex) nel parametro di uscita. Quindi, è possibile reindirizzare l'output ovunque si desideri.

Problemi correlati