Skip to content

Commit

Permalink
Support disabling colorized output; custom colorized output.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Sep 25, 2020
1 parent cdaee04 commit 37d891c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 43 deletions.
33 changes: 28 additions & 5 deletions bin/eshost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const readline = require('readline');

const chalk = require('chalk');
const esh = require('eshost');
const styles = require('ansi-styles');
const Table = require('cli-table');
const uniqueTempDir = require('unique-temp-dir');
const yargs = require('yargs');
Expand Down Expand Up @@ -47,6 +48,9 @@ const yargv = yargs
.describe('coalesce', 'coalesce like output into a single entry')
.boolean('coalesce')
.alias('coalesce', 's')
.describe('color', 'specify the color to use in output, eg. --color.error red --color.header blue')
.describe('no-color', 'do not colorize output')
.boolean('no-color')
.describe('showSource', 'show input source')
.boolean('showSource')
.alias('showSource', 'i')
Expand Down Expand Up @@ -210,10 +214,23 @@ hosts = [ ... new Set(hosts) ]; // take unique elements

let reporterOptions = {
coalesce: argv.coalesce,
color: { error: 'magentaBright', header: 'grey', ...(argv.color || {}) },
showSource: argv.showSource,
unanimous: argv.unanimous
};

if (argv.noColors) {
reporterOptions.color = false;
}

let invalidColor = Object.values(reporterOptions.color).find(color => !styles[color]);

if (invalidColor) {
console.log(`Invalid color or style: "${invalidColor}"\n`);
console.log(`Choose from:\n${Object.keys(styles).map(style => `- ${style}\n`).join('')}`);
process.exit(1);
}

let reporter;
if (argv.table) {
reporter = new TableReporter(reporterOptions);
Expand All @@ -222,8 +239,11 @@ if (argv.table) {
}

if (argv.list || argv.add || argv.edit || argv.delete ||
argv['delete-all'] || argv['configure-esvu'] || argv['configure-jsvu']) {
console.log(chalk.grey(`Using config "${config.configPath}"`));
argv['delete-all'] || argv.configureEsvu || argv.configureJsvu) {
const message = `Using config "${config.configPath}"`;
console.log(
argv.noColors ? message : chalk.grey(message)
);
}
// list available hosts
if (argv.list) {
Expand Down Expand Up @@ -265,8 +285,8 @@ if (argv['delete-all']) {
process.exit(0);
}

if (argv['configure-esvu'] || argv['configure-jsvu']) {
const vu = argv['configure-esvu']
if (argv.configureEsvu || argv.configureJsvu) {
const vu = argv.configureEsvu
? 'esvu'
: 'jsvu';
const vuRoot = path.join(argv[`${vu}-root`] || os.homedir(), `.${vu}`);
Expand Down Expand Up @@ -372,7 +392,10 @@ async function runInHost(testable, host) {
reporter.result(host, result);
return runner.destroy();
}).catch(e => {
console.error(chalk.red(`Failure attempting to eval script in agent: ${e.stack}`));
const message = `Failure attempting to eval script in agent: ${e.stack}`;
console.error(
argv.noColors ? message : chalk.red(message)
);
});
}

Expand Down
21 changes: 14 additions & 7 deletions lib/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ module.exports = class Reporter {
this.source = source;

if (this.options.showSource) {
Reporter.printSource(source);
this.printSource(source);
}
}

result({name}, {stdout, error}) {
let resultCell = stdout.trim();
if (error) {
resultCell += `\n${chalk.red(`${error.name}: ${error.message}`)}`;
resultCell += this.options.color
? `\n${chalk[this.options.color.error](`${error.name}: ${error.message}`)}`
: `\n${error.name}: ${error.message}`;
}
resultCell = resultCell.replace(/\r/g, '');

Expand All @@ -47,6 +49,16 @@ module.exports = class Reporter {
return true;
}

printSource(source) {
const header = '## Source';
console.log(
this.options.color
? chalk[this.options.color.header](header)
: header
);
console.log(`${source}\n`);
}

static coalesce(records) {
const resultsMap = new Map();
for (const [ name, resultString ] of records) {
Expand All @@ -57,9 +69,4 @@ module.exports = class Reporter {
}
return [...resultsMap];
}

static printSource(source) {
console.log(chalk.blue('## Source'));
console.log(`${source}\n`);
}
}
18 changes: 11 additions & 7 deletions lib/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ module.exports = class DefaultReporter extends Reporter {
if (this.options.coalesce) {
for (const [ resultString, hosts ] of Reporter.coalesce(this.results)) {
const joinedHosts = hosts.join(", ").trim();
printHostResult(joinedHosts, resultString);
this.printHostResult(joinedHosts, resultString);
}

if (this.options.unanimous) {
process.exit(1);
}
} else {
this.results.forEach(row => {
printHostResult(row[0], row[1]);
this.printHostResult(row[0], row[1]);
});
}
}
}
}

function printHostResult(name, result) {
console.log(chalk.blue(`#### ${name}`));
console.log(`${result}\n`);
printHostResult(name, result) {
const header = `#### ${name}`;
console.log(
this.options.color
? chalk[this.options.color.header](header)
: header
);
console.log(`${result}\n`);
}
}
68 changes: 45 additions & 23 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"eshost": "bin/eshost.js"
},
"dependencies": {
"chalk": "^1.1.1",
"ansi-styles": "^4.2.1",
"chalk": "^4.1.0",
"cli-table": "^0.3.1",
"configstore": "^1.4.0",
"eshost": "^7.5.2",
Expand Down

0 comments on commit 37d891c

Please sign in to comment.