From a6d1bafd098443dcb4381f03fd7283594d263c8d Mon Sep 17 00:00:00 2001 From: Peter Hale Date: Tue, 10 Aug 2021 08:34:03 -0600 Subject: [PATCH] fix: correct header to data column alignment @W-9712423@ --- package.json | 1 + src/util.ts | 10 +++++++--- test/unit/util.test.ts | 15 ++++++++++----- yarn.lock | 14 +++++++------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 8929d446..8030ee31 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/util.ts b/src/util.ts index 9e71d089..58938914 100644 --- a/src/util.ts +++ b/src/util.ts @@ -20,6 +20,7 @@ import { Dictionary, Nullable, ensureString } from '@salesforce/ts-types'; export function generateTableChoices( columns: Dictionary, choices: Array | T>>, + // eslint-disable-next-line @typescript-eslint/no-unused-vars padForCheckbox = true ): ChoiceBase[] { const columnEntries = Object.entries(columns); @@ -32,12 +33,15 @@ export function generateTableChoices( 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) { diff --git a/test/unit/util.test.ts b/test/unit/util.test.ts index b311af94..61292ba5 100644 --- a/test/unit/util.test.ts +++ b/test/unit/util.test.ts @@ -7,6 +7,7 @@ import { expect } from 'chai'; import { Separator } from 'inquirer'; +import stripAnsi = require('strip-ansi'); import { generateTableChoices } from '../../src/util'; describe('generateTableChoices', () => { @@ -24,7 +25,7 @@ describe('generateTableChoices', () => { }, { name: 'my-app', - type: 'org', + type: 'a-long-org', path: 'my-app', value: 'my-app', }, @@ -32,14 +33,18 @@ describe('generateTableChoices', () => { 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 '); }); }); diff --git a/yarn.lock b/yarn.lock index ed863fd6..d71ef520 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"