如何在祝福中重定向stdout?

时间:2021-11-09 00:04:55

Long story short: I want to build a CLI for a development daemon. Daemon outputs different kind of information to stdout and I want to relay that information to user in an area of screen, in scrollable fashion. I am struggling with getting stdout to blessed. Simple prototype below, which buffers stdout, so information is never complete.

长话短说:我想为开发守护进程构建一个CLI。守护进程向stdout输出不同种类的信息,我想以可滚动的方式将该信息传递给屏幕区域中的用户。我正在努力让stdout得到祝福。下面简单的原型,缓冲stdout,所以信息永远不会完整。

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

1 个解决方案

#1


0  

The line

var screen = blessed.screen()

Takes parameters in order to change where the input comes from, and where the output goes too. Logically, they're named input and output:

采用参数以改变输入的来源以及输出的位置。从逻辑上讲,它们被命名为输入和输出:

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

So, you just chuck a writeable (or duplex) stream into the output parameter. Then, you can pipe the output anywhere you wish.

因此,您只需将可写(或双工)流放入输出参数中。然后,您可以将输出管道输送到任何地方。

#1


0  

The line

var screen = blessed.screen()

Takes parameters in order to change where the input comes from, and where the output goes too. Logically, they're named input and output:

采用参数以改变输入的来源以及输出的位置。从逻辑上讲,它们被命名为输入和输出:

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

So, you just chuck a writeable (or duplex) stream into the output parameter. Then, you can pipe the output anywhere you wish.

因此,您只需将可写(或双工)流放入输出参数中。然后,您可以将输出管道输送到任何地方。