Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --markdown formatting #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions bin/eshost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const readline = require('readline');

const chalk = require('chalk');
const esh = require('eshost');
const Table = require('cli-table');
const uniqueTempDir = require('unique-temp-dir');
const yargs = require('yargs');

Expand Down Expand Up @@ -44,6 +43,8 @@ const yargv = yargs
.describe('table', 'output in a table')
.boolean('table')
.alias('table', 't')
.describe('markdown', 'output as markdown')
.boolean('markdown')
.describe('coalesce', 'coalesce like output into a single entry')
.boolean('coalesce')
.alias('coalesce', 's')
Expand Down Expand Up @@ -211,7 +212,8 @@ hosts = [ ... new Set(hosts) ]; // take unique elements
let reporterOptions = {
coalesce: argv.coalesce,
showSource: argv.showSource,
unanimous: argv.unanimous
unanimous: argv.unanimous,
markdown: argv.markdown
};

let reporter;
Expand All @@ -227,7 +229,7 @@ if (argv.list || argv.add || argv.edit || argv.delete ||
}
// list available hosts
if (argv.list) {
hostManager.list(config);
hostManager.list(config, argv);
process.exit(0);
}

Expand Down
52 changes: 46 additions & 6 deletions lib/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class Reporter {
constructor(options = {}) {
this.options = options;
this.source = undefined;
this.results = [];
this._results = [];
}

start(source) {
Expand All @@ -24,22 +24,61 @@ module.exports = class Reporter {
}
resultCell = resultCell.replace(/\r/g, '');

this.results.push([
this._results.push([
name, resultCell
]);
}

end() {}
get joinHostDelimiter() { return ''; }

get results() {
this._results.sort((a,b) => a[0].localeCompare(b[0]));

const results = [];
if (this.options.coalesce) {
for (const [ resultString, hosts ] of Reporter.coalesce(this._results)) {
const joinedHosts = hosts.join(this.joinHostDelimiter).trim();
results.push([joinedHosts, resultString]);
}
} else {
results.push(...this._results);
}

return results;
}

printResults(results) { }

end() {
if (this.options.unanimous && this.isUnanimous) {
process.exit(0);
// don't print anything
} else {
this.printResults(this.prettyResults(this.results));

if (this.options.unanimous) {
process.exit(1);
}
}
}

prettyResults(results) {
if (this.options.markdown) {
return results.map(([host, resultString]) =>
[host, `<pre>${resultString}</pre>`.replace(/\n/g, '<br>')]);
}
return results;
}

get isUnanimous() {
if (this.results.length === 1) {
if (this._results.length === 1) {
return true;
}
// Get the result string of the first result entry
let first = this.results[0][1];
let first = this._results[0][1];

// Compare the first result string to all result strings
for (let [ host, result ] of this.results) {
for (let [ host, result ] of this._results) {
if (result !== first) {
return false;
}
Expand All @@ -62,4 +101,5 @@ module.exports = class Reporter {
console.log(chalk.blue('## Source'));
console.log(`${source}\n`);
}

}
20 changes: 7 additions & 13 deletions lib/host-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const os = require('os');
const path = require('path');

const chalk = require('chalk');
const Table = require('cli-table')

const Config = require('../lib/config')
const { buildTable } = require('../lib/table')
const esh = require('eshost');

const head = [
Expand All @@ -18,20 +18,18 @@ const head = [
'tags',
];

const colWidths = head.map(h => h.length + 2);

exports.list = ({configPath, hosts}) => {
const table = new Table({
colWidths, head
});
exports.list = ({configPath, hosts}, { markdown }) => {
const table = [
head.map(n => chalk.red(n))
];

const names = Object.keys(hosts);
let output;

if (!names.length) {
output = 'No configured hosts';
} else {
output = names.reduce((accum, name) => {
output = buildTable(names.sort().reduce((accum, name) => {
const {
type,
path = '',
Expand All @@ -40,14 +38,10 @@ exports.list = ({configPath, hosts}) => {
} = hosts[name];

const row = [ name, type, path, args, tags ];
const {colWidths: widths} = accum.options;

// Update the stored colWidths for each cell, saving the largest/longest
accum.options.colWidths = Array.from(widths, (width, index) => Math.max(width, String(row[index]).length + 2));

accum.push(row);
return accum;
}, table).sort().toString();
}, table), markdown);
}

console.log(output);
Expand Down
31 changes: 6 additions & 25 deletions lib/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,12 @@ const Reporter = require('../Reporter.js');
const chalk = require('chalk');

module.exports = class DefaultReporter extends Reporter {
end() {
if (this.options.unanimous && this.isUnanimous) {
process.exit(0);
// don't print anything
} else {
this.results.sort((a,b) => a[0].localeCompare(b[0]));
if (this.options.coalesce) {
for (const [ resultString, hosts ] of Reporter.coalesce(this.results)) {
const joinedHosts = hosts.join(", ").trim();
printHostResult(joinedHosts, resultString);
}

if (this.options.unanimous) {
process.exit(1);
}
} else {
this.results.forEach(row => {
printHostResult(row[0], row[1]);
});
}
printResults(results) {
for (const [name, result] of results) {
console.log(chalk.blue(`#### ${name}`));
if (this.options.markdown)
console.log("");
console.log(`${result}\n`);
}
}
}

function printHostResult(name, result) {
console.log(chalk.blue(`#### ${name}`));
console.log(`${result}\n`);
}
28 changes: 5 additions & 23 deletions lib/reporters/table.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
const Reporter = require('../Reporter.js');
const chalk = require('chalk');
const Table = require('cli-table');

module.exports = class TableReporter extends Reporter {
end() {
if (this.options.unanimous && this.isUnanimous) {
process.exit(0);
// don't print anything
} else {
this.results.sort((a,b) => a[0].localeCompare(b[0]));

const table = new Table();
if (this.options.coalesce) {
for (const [ resultString, hosts ] of Reporter.coalesce(this.results)) {
const joinedHosts = hosts.join("\n").trim();
table.push([joinedHosts, resultString]);
}
} else {
table.push(...this.results);
}
const { buildTable } = require('../table');

console.log(table.toString());
module.exports = class TableReporter extends Reporter {
printResults(results) {
let table = [['Engine', 'Results'], ...results];

if (this.options.unanimous) {
process.exit(1);
}
}
console.log(buildTable(table, this.options.markdown));
}
}
34 changes: 34 additions & 0 deletions lib/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const { table } = require('table')
const chalk = require('chalk');

function buildTable(content, markdown=false) {
if (!markdown) {
content[0] = content[0].map((n) => chalk.red(n));
return table(content);
} else {
return table(content, {
border: {
topBody: '',
topJoin: '',
topLeft: '',
topRight: '',
bottomBody: '',
bottomJoin: '',
bottomLeft: '',
bottomRight: '',
bodyLeft: '|',
bodyRight: '|',
bodyJoin: '|',
joinBody: '-',
joinLeft: '|',
joinRight: '|',
joinJoin: '|'
},
drawHorizontalLine: (index) => index === 1
});
}
}

module.exports = { buildTable }
Loading