Skip to content

Commit

Permalink
feat: allow specifying custom color map
Browse files Browse the repository at this point in the history
  • Loading branch information
ejose19 committed Aug 11, 2021
1 parent 9301b05 commit c2e1120
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import Table from './src/console-table-printer';
import { printSimpleTable as printTable } from './src/internalTable/internal-table-printer';

import { COLOR, ALIGNMENT } from './src/models/external-table';
import { ColorMap, DEFAULT_COLOR_MAP } from './src/utils/colored-console-line';

export { Table, printTable, COLOR, ALIGNMENT };
export { Table, printTable, COLOR, ALIGNMENT, ColorMap, DEFAULT_COLOR_MAP };
28 changes: 21 additions & 7 deletions src/internalTable/internal-table-printer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Row } from '../models/common';
import { Column, TableStyleDetails } from '../models/internal-table';
import ColoredConsoleLine from '../utils/colored-console-line';
import ColoredConsoleLine, { ColorMap } from '../utils/colored-console-line';
import { textWithPadding } from '../utils/string-utils';
import {
DEFAULT_COLUMN_LEN,
Expand All @@ -26,9 +26,10 @@ const renderOneLine = (
currentLineIndex: number,
widthLimitedColumnsArray: { [key: string]: string[] },
isHeader: boolean | undefined,
row: Row
row: Row,
colorMap: ColorMap
): string => {
const line = new ColoredConsoleLine();
const line = new ColoredConsoleLine(colorMap);
line.addCharsWithColor(DEFAULT_ROW_FONT_COLOR, tableStyle.vertical);
columns.forEach((column) => {
const thisLineHasText =
Expand Down Expand Up @@ -58,6 +59,7 @@ const renderWidthLimitedLines = (
tableStyle: TableStyleDetails,
columns: Column[],
row: Row,
colorMap: ColorMap,
isHeader?: boolean
): string[] => {
// { col1: ['How', 'Is', 'Going'], col2: ['I am', 'Tom'], }
Expand All @@ -80,7 +82,8 @@ const renderWidthLimitedLines = (
currentLineIndex,
widthLimitedColumnsArray,
isHeader,
row
row,
colorMap
);

ret.push(singleLine);
Expand All @@ -93,7 +96,12 @@ const renderWidthLimitedLines = (
const renderRow = (table: TableInternal, row: Row): string[] => {
let ret: string[] = [];
ret = ret.concat(
renderWidthLimitedLines(table.tableStyle, table.columns, row)
renderWidthLimitedLines(
table.tableStyle,
table.columns,
row,
table.colorMap
)
);
return ret;
};
Expand Down Expand Up @@ -123,7 +131,7 @@ const renderTableTitle = (table: TableInternal): string[] => {
DEFAULT_HEADER_ALIGNMENT,
getTableWidth()
);
const styledText = new ColoredConsoleLine();
const styledText = new ColoredConsoleLine(table.colorMap);
styledText.addCharsWithColor(DEFAULT_HEADER_FONT_COLOR, titleWithPadding);
// The analysis Result
ret.push(styledText.renderConsole());
Expand All @@ -149,7 +157,13 @@ const renderTableHeaders = (table: TableInternal): string[] => {
// ║ index ║ text ║ value ║
const row = createHeaderAsRow(createRow, table.columns);
ret = ret.concat(
renderWidthLimitedLines(table.tableStyle, table.columns, row, true)
renderWidthLimitedLines(
table.tableStyle,
table.columns,
row,
table.colorMap,
true
)
);

// ╟═══════╬═══════════════════════════════════════╬════════╢
Expand Down
5 changes: 5 additions & 0 deletions src/internalTable/internal-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RowSortFunction,
} from '../models/external-table';
import { Column, TableStyleDetails } from '../models/internal-table';
import { ColorMap, DEFAULT_COLOR_MAP } from '../utils/colored-console-line';
import {
DEFAULT_TABLE_STYLE,
DEFAULT_ROW_ALIGNMENT,
Expand Down Expand Up @@ -46,6 +47,8 @@ class TableInternal {

rowSeparator: boolean;

colorMap: ColorMap;

initSimple(columns: string[]) {
this.columns = columns.map((column) => ({
name: column,
Expand All @@ -65,6 +68,7 @@ class TableInternal {
this.columns =
options?.columns?.map(rawColumnToInternalColumn) || this.columns;
this.rowSeparator = options?.rowSeparator || this.rowSeparator;
this.colorMap = options?.colorMap || this.colorMap;
}

constructor(options?: ComplexOptions | string[]) {
Expand All @@ -79,6 +83,7 @@ class TableInternal {
this.disabledColumns = [];
this.computedColumns = [];
this.rowSeparator = DEFAULT_ROW_SEPARATOR;
this.colorMap = DEFAULT_COLOR_MAP;

if (options instanceof Array) {
this.initSimple(options);
Expand Down
2 changes: 2 additions & 0 deletions src/models/external-table.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ColorMap } from '../utils/colored-console-line';
import { ALIGNMENT, COLOR } from './common';
import { TableStyleDetails } from './internal-table';

Expand Down Expand Up @@ -30,4 +31,5 @@ export interface ComplexOptions {
disabledColumns?: string[];
computedColumns?: ComputedColumn[];
rowSeparator?: boolean;
colorMap?: ColorMap;
}
1 change: 1 addition & 0 deletions src/models/internal-table.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ColorMap } from '../utils/colored-console-line';
import { ALIGNMENT, COLOR } from './common';

/*
Expand Down
20 changes: 13 additions & 7 deletions src/utils/colored-console-line.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { COLOR } from '../models/common';

const COLOR_MAP: {
export type ColorMap = {
[key in COLOR]?: string;
} = {
};

export const DEFAULT_COLOR_MAP: ColorMap = {
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
Expand All @@ -15,18 +17,22 @@ const COLOR_MAP: {
reset: '\x1b[0m',
};

export const colorString = (color: COLOR, text: string) =>
`${color && COLOR_MAP[color]}${text}${COLOR_MAP.reset}`;

export default class ColoredConsoleLine {
text: string;

constructor() {
colorMap: ColorMap;

constructor(colorMap = DEFAULT_COLOR_MAP) {
this.text = '';
this.colorMap = colorMap;
}

addCharsWithColor(color: COLOR, text: string) {
this.text += colorString(color, text);
const colorAnsi = this.colorMap[color];
this.text +=
colorAnsi !== undefined
? `${colorAnsi}${text}${this.colorMap.reset}`
: text;
}

renderConsole(): string {
Expand Down

0 comments on commit c2e1120

Please sign in to comment.