Skip to content

Commit 90e4de7

Browse files
committed
feat: add makeTable method
1 parent 8c156c4 commit 90e4de7

File tree

3 files changed

+69
-51
lines changed

3 files changed

+69
-51
lines changed

src/ux/table.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import { TableOptions } from '@oclif/table';
8+
import { env } from '@salesforce/kit';
89

910
type Column<T extends Record<string, unknown>> = {
1011
extended: boolean;
@@ -56,3 +57,48 @@ export function convertToNewTableAPI<T extends Record<string, unknown>>(
5657

5758
return { data: d, title: options?.title, borderStyle: 'headers-only-with-underline', columns: cols };
5859
}
60+
61+
export function getDefaults<T extends Record<string, unknown>>(options: TableOptions<T>): Partial<TableOptions<T>> {
62+
const borderStyles = [
63+
'all',
64+
'headers-only-with-outline',
65+
'headers-only-with-underline',
66+
'headers-only',
67+
'horizontal-with-outline',
68+
'horizontal',
69+
'none',
70+
'outline',
71+
'vertical-with-outline',
72+
'vertical',
73+
];
74+
75+
const defaultStyle = 'vertical-with-outline';
76+
const determineBorderStyle = (): TableOptions<T>['borderStyle'] => {
77+
const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle);
78+
if (borderStyles.includes(envVar)) {
79+
return envVar as TableOptions<T>['borderStyle'];
80+
}
81+
82+
return defaultStyle;
83+
};
84+
85+
const overflowOptions = ['wrap', 'truncate', 'truncate-middle', 'truncate-start', 'truncate-end'];
86+
const determineOverflow = (): TableOptions<T>['overflow'] => {
87+
const envVar = env.getString('SF_TABLE_OVERFLOW');
88+
if (envVar && overflowOptions.includes(envVar)) {
89+
return envVar as TableOptions<T>['overflow'];
90+
}
91+
92+
return options.overflow;
93+
};
94+
95+
return {
96+
borderStyle: determineBorderStyle(),
97+
noStyle: env.getBoolean('SF_NO_TABLE_STYLE', false),
98+
headerOptions: {
99+
...options.headerOptions,
100+
formatter: 'capitalCase',
101+
},
102+
overflow: determineOverflow(),
103+
};
104+
}

src/ux/ux.ts

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import ansis from 'ansis';
99
import { ux } from '@oclif/core';
1010
import { AnyJson } from '@salesforce/ts-types';
1111
import terminalLink from 'terminal-link';
12-
import { printTable, TableOptions } from '@oclif/table';
13-
import { env } from '@salesforce/kit';
12+
import { makeTable, printTable, TableOptions } from '@oclif/table';
1413
import { UxBase } from './base.js';
1514
import { Spinner } from './spinner.js';
1615
import styledObject from './styledObject.js';
16+
import { getDefaults } from './table.js';
1717

1818
/**
1919
* UX methods for plugins. Automatically suppress console output if outputEnabled is set to false.
@@ -76,54 +76,28 @@ export class Ux extends UxBase {
7676
* @param options Table properties
7777
*/
7878
public table<T extends Record<string, unknown>>(options: TableOptions<T>): void {
79-
const borderStyles = [
80-
'all',
81-
'headers-only-with-outline',
82-
'headers-only-with-underline',
83-
'headers-only',
84-
'horizontal-with-outline',
85-
'horizontal',
86-
'none',
87-
'outline',
88-
'vertical-with-outline',
89-
'vertical',
90-
];
91-
92-
const defaultStyle = 'vertical-with-outline';
93-
const determineBorderStyle = (): TableOptions<T>['borderStyle'] => {
94-
const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle);
95-
if (borderStyles.includes(envVar)) {
96-
return envVar as TableOptions<T>['borderStyle'];
97-
}
98-
99-
return defaultStyle;
100-
};
101-
102-
const overflowOptions = ['wrap', 'truncate', 'truncate-middle', 'truncate-start', 'truncate-end'];
103-
const determineOverflow = (): TableOptions<T>['overflow'] => {
104-
const envVar = env.getString('SF_TABLE_OVERFLOW');
105-
if (envVar && overflowOptions.includes(envVar)) {
106-
return envVar as TableOptions<T>['overflow'];
107-
}
108-
109-
return options.overflow;
110-
};
111-
11279
this.maybeNoop(() =>
11380
printTable({
11481
...options,
115-
// Don't allow anyone to override these properties
116-
borderStyle: determineBorderStyle(),
117-
noStyle: env.getBoolean('SF_NO_TABLE_STYLE', false),
118-
headerOptions: {
119-
...options.headerOptions,
120-
formatter: 'capitalCase',
121-
},
122-
overflow: determineOverflow(),
82+
...getDefaults<T>(options),
12383
})
12484
);
12585
}
12686

87+
/**
88+
* Return a string rendering of a table.
89+
*
90+
* @param options Table properties
91+
* @returns string rendering of a table
92+
*/
93+
// eslint-disable-next-line class-methods-use-this
94+
public makeTable<T extends Record<string, unknown>>(options: TableOptions<T>): string {
95+
return makeTable({
96+
...options,
97+
...getDefaults<T>(options),
98+
});
99+
}
100+
127101
/**
128102
* Display a url to the console. This will be automatically suppressed if output is disabled.
129103
*

test/unit/ux/ux.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ describe('Ux', () => {
3131
const ux = new Ux();
3232
ux.table({ data: [{ key: 'foo', value: 'bar' }], title: 'Title' });
3333
});
34-
expect(stdout.replaceAll(' \n', '\n').trimEnd()).to.equal(`Title
35-
Key Value
36-
------------
37-
foo bar`);
34+
expect(stdout).to.include('Title');
35+
expect(stdout).to.match(/Key.+Value/);
36+
expect(stdout).to.match(/foo.+bar/);
3837
});
3938

4039
it('should not log anything if output is not enabled', async () => {
@@ -53,10 +52,9 @@ describe('Ux', () => {
5352
const opts = convertToNewTableAPI([{ key: 'foo', value: 'bar' }], { key: {}, value: {} }, { title: 'Title' });
5453
ux.table(opts);
5554
});
56-
expect(stdout.replaceAll(' \n', '\n').trimEnd()).to.equal(`Title
57-
Key Value
58-
------------
59-
foo bar`);
55+
expect(stdout).to.include('Title');
56+
expect(stdout).to.match(/Key.+Value/);
57+
expect(stdout).to.match(/foo.+bar/);
6058
});
6159

6260
it('should not log anything if output is not enabled', async () => {

0 commit comments

Comments
 (0)