Skip to content

Commit aa63615

Browse files
committed
chore: code review
1 parent 704b2cf commit aa63615

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/ux/table.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function termwidth(stream: NodeJS.WriteStream): number {
2424
return 80;
2525
}
2626

27-
const width = stream.getWindowSize()[0];
27+
const [width] = stream.getWindowSize();
2828
if (width < 1) {
2929
return 80;
3030
}
@@ -36,7 +36,10 @@ function termwidth(stream: NodeJS.WriteStream): number {
3636
return width;
3737
}
3838

39-
const stdtermwidth = Number.parseInt(process.env.OCLIF_COLUMNS!, 10) || termwidth(process.stdout);
39+
const stdtermwidth =
40+
typeof process.env.OCLIF_COLUMNS === 'string'
41+
? Number.parseInt(process.env.OCLIF_COLUMNS, 10)
42+
: termwidth(process.stdout);
4043

4144
class Table<T extends Record<string, unknown>> {
4245
private columns: Array<Column<T> & { key: string; maxWidth?: number; width?: number }>;
@@ -47,8 +50,7 @@ class Table<T extends Record<string, unknown>> {
4750

4851
public constructor(data: T[], columns: Columns<T>, options: Options = {}) {
4952
// assign columns
50-
this.columns = Object.keys(columns).map((key: string) => {
51-
const col = columns[key];
53+
this.columns = Object.entries(columns).map(([key, col]) => {
5254
const extended = col.extended ?? false;
5355
// turn null and undefined into empty strings by default
5456
const get = col.get ?? ((row: Record<string, unknown>): unknown => row[key] ?? '');
@@ -148,7 +150,7 @@ class Table<T extends Record<string, unknown>> {
148150
// terminal width
149151
const maxWidth = stdtermwidth - 2;
150152
// truncation logic
151-
const shouldShorten = (): void => {
153+
const maybeShorten = (): void => {
152154
// don't shorten if full mode
153155
if (options['no-truncate'] ?? (!process.stdout.isTTY && !process.env.CLI_UX_SKIP_TTY_CHECK)) return;
154156

@@ -187,7 +189,7 @@ class Table<T extends Record<string, unknown>> {
187189
}
188190
};
189191

190-
shouldShorten();
192+
maybeShorten();
191193

192194
// print table title
193195
if (options.title) {

0 commit comments

Comments
 (0)