diff --git a/index.ts b/index.ts index 0f269454..6e98adcd 100644 --- a/index.ts +++ b/index.ts @@ -5,5 +5,6 @@ import { } 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, renderTable, COLOR, ALIGNMENT }; diff --git a/src/internalTable/internal-table-printer.ts b/src/internalTable/internal-table-printer.ts index ffdeb594..df56d120 100644 --- a/src/internalTable/internal-table-printer.ts +++ b/src/internalTable/internal-table-printer.ts @@ -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, @@ -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 = @@ -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'], } @@ -80,7 +82,8 @@ const renderWidthLimitedLines = ( currentLineIndex, widthLimitedColumnsArray, isHeader, - row + row, + colorMap ); ret.push(singleLine); @@ -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; }; @@ -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()); @@ -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 + ) ); // ╟═══════╬═══════════════════════════════════════╬════════╢ diff --git a/src/internalTable/internal-table.ts b/src/internalTable/internal-table.ts index a7b443fc..f43a5457 100644 --- a/src/internalTable/internal-table.ts +++ b/src/internalTable/internal-table.ts @@ -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, @@ -46,6 +47,8 @@ class TableInternal { rowSeparator: boolean; + colorMap: ColorMap; + initSimple(columns: string[]) { this.columns = columns.map((column) => ({ name: column, @@ -65,6 +68,11 @@ class TableInternal { this.columns = options?.columns?.map(rawColumnToInternalColumn) || this.columns; this.rowSeparator = options?.rowSeparator || this.rowSeparator; + + if(options?.colorMap) { + this.colorMap = { ...this.colorMap, ...options.colorMap }; + } + if (options.rows !== undefined) { this.addRows(options.rows); @@ -83,6 +91,7 @@ class TableInternal { this.disabledColumns = []; this.computedColumns = []; this.rowSeparator = DEFAULT_ROW_SEPARATOR; + this.colorMap = DEFAULT_COLOR_MAP; if (options instanceof Array) { this.initSimple(options); diff --git a/src/models/external-table.ts b/src/models/external-table.ts index 4c2df307..2c4ac4ef 100644 --- a/src/models/external-table.ts +++ b/src/models/external-table.ts @@ -1,3 +1,4 @@ +import { ColorMap } from '../utils/colored-console-line'; import { ALIGNMENT, COLOR, Dictionary } from './common'; import { TableStyleDetails } from './internal-table'; @@ -31,4 +32,5 @@ export interface ComplexOptions { disabledColumns?: string[]; computedColumns?: ComputedColumn[]; rowSeparator?: boolean; + colorMap?: ColorMap; } diff --git a/src/models/internal-table.ts b/src/models/internal-table.ts index 50d048a1..a5bdaab6 100644 --- a/src/models/internal-table.ts +++ b/src/models/internal-table.ts @@ -1,3 +1,4 @@ +import { ColorMap } from '../utils/colored-console-line'; import { ALIGNMENT, COLOR } from './common'; /* diff --git a/src/utils/colored-console-line.ts b/src/utils/colored-console-line.ts index 5d86a08d..99e30060 100644 --- a/src/utils/colored-console-line.ts +++ b/src/utils/colored-console-line.ts @@ -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', @@ -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 {