Skip to content

Commit

Permalink
Add support for proccess's output being shown in a grid
Browse files Browse the repository at this point in the history
  • Loading branch information
lneves12 committed Nov 4, 2020
1 parent b62a2a1 commit 054cbd3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"chalk": "^3.0.0",
"date-fns": "^2.16.1",
"lodash": "^4.17.20",
"neo-blessed": "^0.2.0",
"read-pkg": "^5.2.0",
"rxjs": "^6.6.3",
"spawn-command": "^0.0.2-1",
Expand Down
89 changes: 86 additions & 3 deletions src/flow-control/log-output.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
const Rx = require('rxjs');
const blessed = require("neo-blessed");

const DEFAULT_SCROLL_OPTIONS = {
scrollable: true,
input: true,
alwaysScroll: true,
scrollbar: {
ch: " ",
inverse: true
},
keys: true,
vi: true,
mouse: true
};

module.exports = class LogOutput {
constructor({ logger }) {
this.logger = logger;

this.screen = blessed.screen({
smartCSR: true,
dockBorders: false,
fullUnicode: true,
});

}

handle(commands) {
commands.forEach(command => {
command.stdout.subscribe(text => this.logger.logCommandText(text.toString(), command));
command.stderr.subscribe(text => this.logger.logCommandText(text.toString(), command));

// Quit on Escape, q, or Control-C.
this.screen.key(['escape', 'q', 'C-c'], function(ch, key) {

const killObservables = commands.map(command => {
return Rx.Observable.create(observer => {
command.kill('SIGINT', error => {
console.error(error);
observer.complete();
});
});
});

Rx.forkJoin(killObservables).subscribe({
complete() {
process.kill(process.pid, "SIGINT");
}
});
});

const numberOfRows = Math.ceil(commands.length / 2);

commands.forEach((command, index) => {

const leftPosition = index % 2 === 0 ? "0%": "50%";
const commandRow = Math.floor(index / 2) + 1;
const rowSize = (100 / numberOfRows);
const topPosition = (commandRow - 1) * rowSize;


const commandPrefix = this.logger.getPrefix(command);
const commandLog = blessed.box({
label: commandPrefix,
width: "50%",
height: `${rowSize}%`,
left: leftPosition,
top: `${topPosition}%`,
border: {
type: "line",
},
style: {
border: {
fg: command.prefixColor.toString()
}
}
});
const commandLogText = blessed.log(
Object.assign({}, DEFAULT_SCROLL_OPTIONS, {
parent: commandLog,
tags: true,
})
);
this.screen.append(commandLog);


command.stdout.subscribe(text => {
commandLogText.log(text.toString());
commandLogText.screen.render();
});
command.stderr.subscribe(text => {
commandLogText.log(text.toString());
commandLogText.screen.render();
});
});

return commands;
Expand Down

0 comments on commit 054cbd3

Please sign in to comment.