Skip to content

Commit

Permalink
fix: correct header to data column alignment
Browse files Browse the repository at this point in the history
@W-9712423@
  • Loading branch information
peternhale committed Aug 10, 2021
1 parent 5470d20 commit a6d1baf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"pretty-quick": "^2.0.1",
"shelljs": "0.8.1",
"sinon": "^9.0.2",
"strip-ansi": "6.0.0",
"ts-node": "^8.10.2",
"typescript": "^4"
},
Expand Down
10 changes: 7 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Dictionary, Nullable, ensureString } from '@salesforce/ts-types';
export function generateTableChoices<T>(
columns: Dictionary<string>,
choices: Array<Dictionary<Nullable<string> | T>>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
padForCheckbox = true
): ChoiceBase[] {
const columnEntries = Object.entries(columns);
Expand All @@ -32,12 +33,15 @@ export function generateTableChoices<T>(
ensureString(option[key], `Type ${typeof option[key]} for ${key} in ${Object.keys(option).join(', ')}`)
.length
)
) + (padForCheckbox ? 3 : 0)
) + 1
);

const choicesOptions: ChoiceBase[] = [
// Pad an extra 2 to account for checkbox
new Separator(columnEntries.map(([, value], index) => value?.padEnd(columnLengths[index] + 2, ' ')).join('')),
new Separator(
`${padForCheckbox ? ' '.repeat(2) : ''}${columnEntries
.map(([, value], index) => value?.padEnd(columnLengths[index], ' '))
.join('')}`
),
];

for (const meta of choices) {
Expand Down
15 changes: 10 additions & 5 deletions test/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { expect } from 'chai';
import { Separator } from 'inquirer';
import stripAnsi = require('strip-ansi');
import { generateTableChoices } from '../../src/util';

describe('generateTableChoices', () => {
Expand All @@ -24,22 +25,26 @@ describe('generateTableChoices', () => {
},
{
name: 'my-app',
type: 'org',
type: 'a-long-org',
path: 'my-app',
value: 'my-app',
},
];
it('should generate a formatted table of choices', () => {
const tableChoices = generateTableChoices(columns, choices);
expect(tableChoices[0]).to.be.instanceof(Separator);
expect(tableChoices[1]).to.have.property('name').and.equal('force-app org force-app ');
expect(tableChoices[2]).to.have.property('name').and.equal('my-app org my-app ');
const separator = tableChoices[0] as typeof Separator;
expect(stripAnsi(separator.toString())).to.be.equal(' APP OR PACKAGE TYPE PATH ');
expect(tableChoices[1]).to.have.property('name').and.equal('force-app org force-app ');
expect(tableChoices[2]).to.have.property('name').and.equal('my-app a-long-org my-app ');
});

it('should generate a formatted table of choices without checkbox padding', () => {
const tableChoices = generateTableChoices(columns, choices, false);
expect(tableChoices[0]).to.be.instanceof(Separator);
expect(tableChoices[1]).to.have.property('name').and.equal('force-app org force-app');
expect(tableChoices[2]).to.have.property('name').and.equal('my-app org my-app ');
const separator = tableChoices[0] as typeof Separator;
expect(stripAnsi(separator.toString())).to.be.equal('APP OR PACKAGE TYPE PATH ');
expect(tableChoices[1]).to.have.property('name').and.equal('force-app org force-app ');
expect(tableChoices[2]).to.have.property('name').and.equal('my-app a-long-org my-app ');
});
});
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4614,6 +4614,13 @@ string_decoder@^1.1.1:
dependencies:
safe-buffer "~5.2.0"

strip-ansi@6.0.0, strip-ansi@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz#0b1571dd7669ccd4f3e06e14ef1eed26225ae532"
integrity sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==
dependencies:
ansi-regex "^5.0.0"

strip-ansi@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz#a8479022eb1ac368a871389b635262c505ee368f"
Expand All @@ -4628,13 +4635,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz#0b1571dd7669ccd4f3e06e14ef1eed26225ae532"
integrity sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==
dependencies:
ansi-regex "^5.0.0"

strip-bom@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e"
Expand Down

0 comments on commit a6d1baf

Please sign in to comment.