Skip to content

Commit

Permalink
Improved columnify for cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
LabhanshAgrawal committed Aug 30, 2021
1 parent cbc5e05 commit 7f01b06
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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',
Expand Down Expand Up @@ -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;
})
);
};

Expand All @@ -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);
}
})
Expand All @@ -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) => {
Expand Down

0 comments on commit 7f01b06

Please sign in to comment.