Skip to content
Permalink
Browse files

feat(cli): improve table rendering

  • Loading branch information...
RangerRick committed Jun 11, 2018
1 parent 51426b2 commit da92bdb09fe1f33160f8fead5669fe7024aa420a
Showing with 32 additions and 8 deletions.
  1. +1 −1 package.json
  2. +18 −7 src/CLI.ts
  3. +13 −0 yarn.lock
@@ -79,7 +79,7 @@
},
"dependencies": {
"axios": "^0.16.1",
"cli-table": "^0.3.1",
"cli-table2": "^0.2.0",
"commander": "^2.9.0",
"fs": "^0.0.1-security",
"ip-address": "^5.8.8",
@@ -16,7 +16,7 @@ const CLI = () => {
const catCLI = new Category('cli', catRoot);

// tslint:disable
const Table = require('cli-table');
const Table = require('cli-table2');
const colors = require('colors');
const fs = require('fs');
const path = require('path');
@@ -27,8 +27,9 @@ const CLI = () => {
const defaultConfigFile = path.join(homedir, '.opennms-cli.config.json');

const tableFormat = {
head: [],
/* tslint:disable:object-literal-sort-keys */
head: [],
colWidths: [],
chars: {
'middle': ' ',
'top': '',
@@ -50,6 +51,7 @@ const CLI = () => {
'padding-left': 0,
'padding-right': 0,
},
wordWrap: true,
};

const readConfig = () => {
@@ -186,17 +188,20 @@ const CLI = () => {
}
};

const getMaxWidth = (data, prop, max) => {
const filtered = data.map((d) => ('' + d[prop]).length);
const m = Math.max(...filtered);
return Math.min(m, max);
};

const logMessageLength = 50;
const formatAlarms = (alarms) => {
return alarms.map((alarm) => {
const severityLabel = ((alarm.severity && alarm.severity.label) ? alarm.severity.label : '');

let logMessage = '';
if (alarm.logMessage) {
logMessage = alarm.logMessage.replace('[\r\n].*$').trim();
if (logMessage.length > logMessageLength) {
logMessage = logMessage.slice(0, logMessageLength).trim() + '';
}
logMessage = alarm.logMessage.replace('[\r\n].*$', '').replace('<p>', '').replace('</p>', '').trim();
}

return {
@@ -233,9 +238,15 @@ const CLI = () => {

return dao.find(filter).then((alarms) => {
const format = Object.assign({}, tableFormat);
const formatted = formatAlarms(alarms);

format.head = [ 'ID', 'Severity', 'Node', 'Count', 'Time', 'Log' ];
format.colWidths = [3, 8, getMaxWidth(formatted, 'node', 30), 5, 17];
const existingWidths = format.colWidths.reduce((acc, val) => acc + val);
const spacers = (format.colWidths.length * 2);
const remainder = process.stdout.columns - existingWidths - spacers;
format.colWidths.push(remainder);
const t = new Table(format);
const formatted = formatAlarms(alarms);
for (const alarm of formatted) {
t.push([alarm.id, alarm.severity, alarm.node, alarm.count, alarm.time, alarm.log]);
}
@@ -1625,6 +1625,15 @@ cli-spinners@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-0.1.2.tgz#bb764d88e185fb9e1e6a2a1f19772318f605e31c"

cli-table2@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/cli-table2/-/cli-table2-0.2.0.tgz#2d1ef7f218a0e786e214540562d4bd177fe32d97"
dependencies:
lodash "^3.10.1"
string-width "^1.0.1"
optionalDependencies:
colors "^1.1.2"

cli-table@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/cli-table/-/cli-table-0.3.1.tgz#f53b05266a8b1a0b934b3d0821e6e2dc5914ae23"
@@ -4561,6 +4570,10 @@ lodash@4.17.4, lodash@^4.13.1, lodash@^4.14.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

lodash@^3.10.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.11.1, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

0 comments on commit da92bdb

Please sign in to comment.
You can’t perform that action at this time.