Improved columnify for cli output

This commit is contained in:
Labhansh Agrawal 2021-08-31 00:53:19 +05:30
parent cbc5e05c8c
commit 7f01b06456

View file

@ -8,7 +8,7 @@ import pify from 'pify';
import args from 'args'; import args from 'args';
import chalk from 'chalk'; import chalk from 'chalk';
import open from 'open'; import open from 'open';
import columnify from 'columnify'; import _columnify from 'columnify';
import got from 'got'; import got from 'got';
import ora from 'ora'; import ora from 'ora';
import * as api from './api'; import * as api from './api';
@ -32,6 +32,22 @@ const checkConfig = () => {
process.exit(1); process.exit(1);
}; };
const columnify = (data: {name: string; description: string}[]) => {
const maxNameLength = Math.max(...data.map((entry) => entry.name.length), 0);
const descriptionWidth = process.stdout.columns - maxNameLength - 1;
return _columnify(data, {
showHeaders: false,
config: {
description: {
maxWidth: descriptionWidth
},
name: {
dataTransform: (nameValue) => chalk.green(nameValue)
}
}
}).replace(/\s+$/gm, ''); // remove padding from the end of all lines
};
args.command( args.command(
'install', 'install',
'Install a plugin', 'Install a plugin',
@ -92,12 +108,6 @@ const lsRemote = (pattern?: string) => {
entries.map(({name, description}) => { entries.map(({name, description}) => {
return {name, description}; return {name, description};
}) })
)
.then((entries) =>
entries.map((entry) => {
entry.name = chalk.green(entry.name);
return entry;
})
); );
}; };
@ -116,9 +126,8 @@ args.command(
console.error(`${chalk.red('Try')} ${chalk.green('hyper ls-remote')}`); console.error(`${chalk.red('Try')} ${chalk.green('hyper ls-remote')}`);
process.exit(1); process.exit(1);
} else { } else {
let msg = columnify(entries); const msg = columnify(entries);
spinner.succeed(); spinner.succeed();
msg = msg.substring(msg.indexOf('\n') + 1); // remove header
console.log(msg); console.log(msg);
} }
}) })
@ -138,10 +147,8 @@ args.command(
commandPromise = lsRemote() commandPromise = lsRemote()
.then((entries) => { .then((entries) => {
let msg = columnify(entries); const msg = columnify(entries);
spinner.succeed(); spinner.succeed();
msg = msg.substring(msg.indexOf('\n') + 1); // remove header
console.log(msg); console.log(msg);
}) })
.catch((err) => { .catch((err) => {