Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow specifying custom color map #394

Merged
merged 3 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
export { Table, printTable, renderTable, COLOR, ALIGNMENT, ColorMap, DEFAULT_COLOR_MAP };
Copy link
Owner

@ayonious ayonious Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ColorMap does not need to be exported here, is it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe DEFAULT_COLOR_MAP also is not needed?

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;

if (options.rows !== undefined) {
this.addRows(options.rows);
Expand All @@ -83,6 +87,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, Dictionary } from './common';
import { TableStyleDetails } from './internal-table';

Expand Down Expand Up @@ -31,4 +32,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