Skip to content

Commit

Permalink
feat: Allow for color: false to wipe the colorMap. (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmarshall authored Nov 6, 2023
1 parent 4876a92 commit 9405d83
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/internalTable/internal-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class TableInternal {
this.rowSeparator = options?.rowSeparator || this.rowSeparator;
this.charLength = options?.charLength || this.charLength;

if (options?.colorMap) {
if (options?.shouldDisableColors) {
this.colorMap = {};
} else if (options?.colorMap) {
this.colorMap = { ...this.colorMap, ...options.colorMap };
}

Expand Down
1 change: 1 addition & 0 deletions src/models/external-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ComplexOptions {
disabledColumns?: string[];
computedColumns?: ComputedColumn[];
rowSeparator?: boolean;
shouldDisableColors? : boolean;
colorMap?: ColorMap;
charLength?: CharLengthDict;
}
10 changes: 10 additions & 0 deletions test/__snapshots__/customizedColor.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ exports[`Example: Print a simple Table with Custom column colors Custom column c
β”‚ 4  β”‚  This row is green β”‚  10.212 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
`;

exports[`Example: Print a simple Table with Custom column colors Disabling color works 1`] = `
"β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ red_left_align_index β”‚ right_align_text β”‚ green_value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2 β”‚ This is my defined Green β”‚ 10.212 β”‚
β”‚ 3 β”‚ This row is blue as well β”‚ 10.212 β”‚
β”‚ 4 β”‚ This row is green β”‚ 10.212 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
`;
42 changes: 42 additions & 0 deletions test/customizedColor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,46 @@ describe('Example: Print a simple Table with Custom column colors', () => {
p.printTable();
expect(p.render()).toMatchSnapshot();
});

it('Disabling color works', () => {
// Create a table
const p = new Table({
columns: [
{ name: 'red_left_align_index', alignment: 'left' },
{ name: 'right_align_text', alignment: 'right' },
{ name: 'green_value' },
],
shouldDisableColors: true,
});

// add rows with custom color
p.addRow(
{
red_left_align_index: 2,
right_align_text: 'This is my defined Green',
green_value: 10.212,
},
{ color: 'custom_green' } // custom color
);
p.addRow(
{
red_left_align_index: 3,
right_align_text: 'This row is blue as well',
green_value: 10.212,
},
{ color: 'blue' }
);
p.addRow(
{
red_left_align_index: 4,
right_align_text: 'This row is green',
green_value: 10.212,
},
{ color: 'green' }
);

// print
p.printTable();
expect(p.render()).toMatchSnapshot();
});
});

0 comments on commit 9405d83

Please sign in to comment.