Skip to content

Commit 054cbd3

Browse files
lneves12lneves
authored andcommitted
Add support for proccess's output being shown in a grid
1 parent b62a2a1 commit 054cbd3

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"chalk": "^3.0.0",
3333
"date-fns": "^2.16.1",
3434
"lodash": "^4.17.20",
35+
"neo-blessed": "^0.2.0",
3536
"read-pkg": "^5.2.0",
3637
"rxjs": "^6.6.3",
3738
"spawn-command": "^0.0.2-1",

src/flow-control/log-output.js

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,95 @@
1+
const Rx = require('rxjs');
2+
const blessed = require("neo-blessed");
3+
4+
const DEFAULT_SCROLL_OPTIONS = {
5+
scrollable: true,
6+
input: true,
7+
alwaysScroll: true,
8+
scrollbar: {
9+
ch: " ",
10+
inverse: true
11+
},
12+
keys: true,
13+
vi: true,
14+
mouse: true
15+
};
16+
117
module.exports = class LogOutput {
218
constructor({ logger }) {
319
this.logger = logger;
20+
21+
this.screen = blessed.screen({
22+
smartCSR: true,
23+
dockBorders: false,
24+
fullUnicode: true,
25+
});
26+
427
}
528

629
handle(commands) {
7-
commands.forEach(command => {
8-
command.stdout.subscribe(text => this.logger.logCommandText(text.toString(), command));
9-
command.stderr.subscribe(text => this.logger.logCommandText(text.toString(), command));
30+
31+
// Quit on Escape, q, or Control-C.
32+
this.screen.key(['escape', 'q', 'C-c'], function(ch, key) {
33+
34+
const killObservables = commands.map(command => {
35+
return Rx.Observable.create(observer => {
36+
command.kill('SIGINT', error => {
37+
console.error(error);
38+
observer.complete();
39+
});
40+
});
41+
});
42+
43+
Rx.forkJoin(killObservables).subscribe({
44+
complete() {
45+
process.kill(process.pid, "SIGINT");
46+
}
47+
});
48+
});
49+
50+
const numberOfRows = Math.ceil(commands.length / 2);
51+
52+
commands.forEach((command, index) => {
53+
54+
const leftPosition = index % 2 === 0 ? "0%": "50%";
55+
const commandRow = Math.floor(index / 2) + 1;
56+
const rowSize = (100 / numberOfRows);
57+
const topPosition = (commandRow - 1) * rowSize;
58+
59+
60+
const commandPrefix = this.logger.getPrefix(command);
61+
const commandLog = blessed.box({
62+
label: commandPrefix,
63+
width: "50%",
64+
height: `${rowSize}%`,
65+
left: leftPosition,
66+
top: `${topPosition}%`,
67+
border: {
68+
type: "line",
69+
},
70+
style: {
71+
border: {
72+
fg: command.prefixColor.toString()
73+
}
74+
}
75+
});
76+
const commandLogText = blessed.log(
77+
Object.assign({}, DEFAULT_SCROLL_OPTIONS, {
78+
parent: commandLog,
79+
tags: true,
80+
})
81+
);
82+
this.screen.append(commandLog);
83+
84+
85+
command.stdout.subscribe(text => {
86+
commandLogText.log(text.toString());
87+
commandLogText.screen.render();
88+
});
89+
command.stderr.subscribe(text => {
90+
commandLogText.log(text.toString());
91+
commandLogText.screen.render();
92+
});
1093
});
1194

1295
return commands;

0 commit comments

Comments
 (0)