From 7f01b06456b08e52590a50249f57ac88e6bdc27d Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Tue, 31 Aug 2021 00:53:19 +0530 Subject: [PATCH] Improved columnify for cli output --- cli/index.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/cli/index.ts b/cli/index.ts index 99bbaa8684ef..9e4091e3e263 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -8,7 +8,7 @@ import pify from 'pify'; import args from 'args'; import chalk from 'chalk'; import open from 'open'; -import columnify from 'columnify'; +import _columnify from 'columnify'; import got from 'got'; import ora from 'ora'; import * as api from './api'; @@ -32,6 +32,22 @@ const checkConfig = () => { 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( 'install', 'Install a plugin', @@ -92,12 +108,6 @@ const lsRemote = (pattern?: string) => { entries.map(({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')}`); process.exit(1); } else { - let msg = columnify(entries); + const msg = columnify(entries); spinner.succeed(); - msg = msg.substring(msg.indexOf('\n') + 1); // remove header console.log(msg); } }) @@ -138,10 +147,8 @@ args.command( commandPromise = lsRemote() .then((entries) => { - let msg = columnify(entries); - + const msg = columnify(entries); spinner.succeed(); - msg = msg.substring(msg.indexOf('\n') + 1); // remove header console.log(msg); }) .catch((err) => {