Skip to content

Commit

Permalink
FIX: fix dashboard mode
Browse files Browse the repository at this point in the history
  • Loading branch information
royriojas committed Feb 4, 2017
1 parent d37bca5 commit 5268a40
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ npm i -g shell-executor
## Usage

```
# or sx as it is alias for shell-exec
Usage: shell-exec [options] cmd1, cmd2, ... cmdn
Options:
-d, --dashboard Show the processes in a nice dashboard if space allows it. No more than 9 commands are allowed in this mode
-b, --bail Stop execution as soon as one of the task exit with an exit code different than 0 or an error happened
-o, --sortOutput Sort the stdout and stderr output from the commands
-h, --help Show this help
-v, --version Outputs the version number
-q, --quiet Show only the summary info
-q, --quiet Show only the summary info - default: false
--colored-output Use colored output in logs
--stack if true, uncaught errors will show the stack trace if available
```

## Examples
Expand Down
93 changes: 74 additions & 19 deletions source/src/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,82 @@ export const getGridAndScreen = () => {
};
};

const getPositionByIndex = (index) => {
const positions = [
{ row: 0, col: 0 },
{ row: 0, col: 4 },
{ row: 0, col: 8 },
{ row: 6, col: 0 },
{ row: 6, col: 4 },
{ row: 6, col: 8 },
];
const layouts = {
1: [{ row: 0, col: 0, rowSpan: 12, colSpan: 12 }],
2: [
{ row: 0, col: 0, rowSpan: 12, colSpan: 6 },
{ row: 0, col: 6, rowSpan: 12, colSpan: 6 },
],
3: [
{ row: 0, col: 0, rowSpan: 6, colSpan: 6 },
{ row: 0, col: 6, rowSpan: 6, colSpan: 6 },
{ row: 6, col: 0, rowSpan: 6, colSpan: 12 },
],
4: [
{ row: 0, col: 0, rowSpan: 6, colSpan: 6 },
{ row: 0, col: 6, rowSpan: 6, colSpan: 6 },
{ row: 6, col: 0, rowSpan: 6, colSpan: 6 },
{ row: 6, col: 6, rowSpan: 6, colSpan: 6 },
],
5: [
{ row: 0, col: 0, rowSpan: 6, colSpan: 4 },
{ row: 0, col: 4, rowSpan: 6, colSpan: 4 },
{ row: 0, col: 8, rowSpan: 6, colSpan: 4 },
{ row: 6, col: 0, rowSpan: 6, colSpan: 6 },
{ row: 6, col: 6, rowSpan: 6, colSpan: 6 },
],
6: [
{ row: 0, col: 0, rowSpan: 6, colSpan: 4 },
{ row: 0, col: 4, rowSpan: 6, colSpan: 4 },
{ row: 0, col: 8, rowSpan: 6, colSpan: 4 },
{ row: 6, col: 0, rowSpan: 6, colSpan: 4 },
{ row: 6, col: 4, rowSpan: 6, colSpan: 4 },
{ row: 6, col: 8, rowSpan: 6, colSpan: 4 },
],
7: [
{ row: 0, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 0, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 0, col: 8, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 8, rowSpan: 4, colSpan: 4 },
{ row: 8, col: 0, rowSpan: 4, colSpan: 12 },
],
8: [
{ row: 0, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 0, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 0, col: 8, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 8, rowSpan: 4, colSpan: 4 },
{ row: 8, col: 0, rowSpan: 4, colSpan: 6 },
{ row: 8, col: 6, rowSpan: 4, colSpan: 6 },
],
9: [
{ row: 0, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 0, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 0, col: 8, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 4, col: 8, rowSpan: 4, colSpan: 4 },
{ row: 8, col: 0, rowSpan: 4, colSpan: 4 },
{ row: 8, col: 4, rowSpan: 4, colSpan: 4 },
{ row: 8, col: 8, rowSpan: 4, colSpan: 4 },
],
};

// TODO: Generalize this, no need to have fixed layouts.
const getLayoutByIndexAndCount = (index, count) => {
const positions = layouts[count];
return positions[index];
};

export const setProcessLogToGrid = (cmd, grid, index) => {
const { row, col } = getPositionByIndex(index);
export const setProcessLogToGrid = (cmd, grid, index, count) => {
const { row, col, rowSpan, colSpan } = getLayoutByIndexAndCount(index, count);

const box = grid.set(row, col, 6, 4, blessed.box, {
const box = grid.set(row, col, rowSpan, colSpan, blessed.box, {
label: cmd.substr(0, 40),
padding: { top: 0, left: 0, right: 0, bottom: 0 },
padding: { top: 1, left: 1, right: 1, bottom: 1 },
border: {
type: 'line',
},
Expand Down Expand Up @@ -67,21 +124,19 @@ export const setProcessLogToGrid = (cmd, grid, index) => {
let cp;
return {
start() {
cp = spawn(cmd, { stdio: 'pipe' });
cp = spawn(cmd, { stdio: 'pipe', detached: true });

addListener(cp.stdout);
addListener(cp.stderr);

cp.on('close', (exitCode) => {
log.log(`process exit with code ${ exitCode }`);
cp.__closed = true;
});
},
stop() {
if (cp && !cp.exitCode) {
cp.stdout.removeAllListeners('data');
cp.stderr.removeAllListeners('data');
cp.removeAllListeners('close');
cp.kill('SIGINT');
if (cp && !cp.exitCode && !cp.__closed) {
process.kill(-cp.pid, 'SIGTERM');
}
},
};
Expand Down
12 changes: 10 additions & 2 deletions source/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ module.exports = {
_executeCommandsInDashboard(program, cmds) {

const { grid, screen } = getGridAndScreen();
const count = cmds.length;

const commands = cmds.reduce((seq, cmd, index) => {
const command = setProcessLogToGrid(cmd, grid, index);
const command = setProcessLogToGrid(cmd, grid, index, count);
command.start();
seq.push(command);
return seq;
}, []);

screen.key(['escape', 'q', 'C-c'], () => {
commands.forEach(cmd => cmd.stop());
commands.forEach((cmd) => {
program.subtle('stopping', cmd);
cmd.stop();
});
return process.exit(0); // eslint-disable-line
});

Expand Down Expand Up @@ -149,6 +153,10 @@ module.exports = {
if (opts.dashboard) {
this._executeCommandsInDashboard(program, cmds);
} else {
if (cmds.length > 9) {
error('in dashboard mode a maximum of 9 commands are allowed');
return;
}
this._execute(program, cmds);
}
},
Expand Down

0 comments on commit 5268a40

Please sign in to comment.