-
Notifications
You must be signed in to change notification settings - Fork 22
/
internal-table-printer.ts
199 lines (174 loc) Β· 6.24 KB
/
internal-table-printer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Column, Row } from '../models/internal-table';
import ColoredConsoleLine from '../utils/colored-console-line';
import { textWithPadding, limitWidth } from '../utils/string-utils';
import {
defaultHeaderAlignment,
defaultHeaderFontColor,
defaultRowAlignment,
defaultRowFontColor,
TABLE_STYLE_DETAILS,
} from '../utils/table-constants';
import {
cellText,
createHeaderAsRow,
createRow,
renderTableHorizontalBorders,
getWidthLimitedColumnsArray,
} from '../utils/table-helpers';
import { TableInternal } from './internal-table';
import { preProcessColumns, preProcessRows } from './table-pre-processors';
// β Index β β β
const renderOneLine = (
tableStyle: TABLE_STYLE_DETAILS,
columns: Column[],
currentLineIndex: number,
widthLimitedColumnsArray: { [key: string]: string[] },
isHeader: boolean | undefined,
row: Row
): string => {
const line = new ColoredConsoleLine();
line.addCharsWithColor(defaultRowFontColor, tableStyle.vertical);
columns.forEach((column) => {
const thisLineHasText =
currentLineIndex < widthLimitedColumnsArray[column.name].length;
const textForThisLine: string = thisLineHasText
? cellText(widthLimitedColumnsArray[column.name][currentLineIndex])
: '';
line.addCharsWithColor(defaultRowFontColor, ' ');
line.addCharsWithColor(
(isHeader && defaultHeaderFontColor) || column.color || row.color,
textWithPadding(
textForThisLine,
column.alignment || defaultRowAlignment,
column.maxLen || 20
)
);
line.addCharsWithColor(defaultRowFontColor, ` ${tableStyle.vertical}`);
});
return line.renderConsole();
};
// β Bold β text β value β
// β Index β β β
const renderWidthLimitedLines = (
tableStyle: TABLE_STYLE_DETAILS,
columns: Column[],
row: Row,
isHeader?: boolean
): string[] => {
// { col1: ['How', 'Is', 'Going'], col2: ['I am', 'Tom'], }
const widthLimitedColumnsArray = getWidthLimitedColumnsArray(columns, row);
const totalLines = Object.values(widthLimitedColumnsArray).reduce(
(a, b) => Math.max(a, b.length),
0
);
const ret = [];
for (
let currentLineIndex = 0;
currentLineIndex < totalLines;
currentLineIndex += 1
) {
const singleLine = renderOneLine(
tableStyle,
columns,
currentLineIndex,
widthLimitedColumnsArray,
isHeader,
row
);
ret.push(singleLine);
}
return ret;
};
// β 1 β I would like some red wine please β 10.212 β
const renderRow = (table: TableInternal, row: Row): string[] => {
let ret: string[] = [];
ret = ret.concat(
renderWidthLimitedLines(table.tableStyle, table.columns, row)
);
return ret;
};
/*
The analysis Result
βββββββββ¦ββββββββββββββββββββββββββββββββββββββββ¦βββββββββ
*/
const renderTableTitle = (table: TableInternal): string[] => {
const ret: string[] = [];
if (table.title === undefined) {
return ret;
}
const getTableWidth = () => {
const reducer = (accumulator: number, currentValue: number) =>
// β cell β, 2 spaces + cellTextSize + one border on the left
accumulator + currentValue + 2 + 1;
return table.columns.map((m) => m.maxLen || 20).reduce(reducer, 1);
};
const titleWithPadding = textWithPadding(
table.title as string,
defaultHeaderAlignment,
getTableWidth()
);
const styledText = new ColoredConsoleLine();
styledText.addCharsWithColor(defaultHeaderFontColor, titleWithPadding);
// The analysis Result
ret.push(styledText.renderConsole());
return ret;
};
/*
βββββββββ¦ββββββββββββββββββββββββββββββββββββββββ¦βββββββββ
β index β text β value β
βββββββββ¬ββββββββββββββββββββββββββββββββββββββββ¬βββββββββ’
*/
const renderTableHeaders = (table: TableInternal): string[] => {
let ret: string[] = [];
// βββββββββ¦ββββββββββββββββββββββββββββββββββββββββ¦βββββββββ
ret.push(
renderTableHorizontalBorders(
table.tableStyle.headerTop,
table.columns.map((m) => m.maxLen || 20)
)
);
// β index β text β value β
const row = createHeaderAsRow(createRow, table.columns);
ret = ret.concat(
renderWidthLimitedLines(table.tableStyle, table.columns, row, true)
);
// βββββββββ¬ββββββββββββββββββββββββββββββββββββββββ¬βββββββββ’
ret.push(
renderTableHorizontalBorders(
table.tableStyle.headerBottom,
table.columns.map((m) => m.maxLen || 20)
)
);
return ret;
};
const renderTableEnding = (table: TableInternal): string[] => {
const ret: string[] = [];
// βββββββββ©ββββββββββββββββββββββββββββββββββββββββ©βββββββββ
ret.push(
renderTableHorizontalBorders(
table.tableStyle.tableBottom,
table.columns.map((m) => m.maxLen || 20)
)
);
return ret;
};
export const renderTable = (table: TableInternal): string => {
preProcessColumns(table); // enable / disable cols, find maxLn of each col/ computed Columns
preProcessRows(table); // sort and filter
const ret: string[] = [];
renderTableTitle(table).forEach((row) => ret.push(row));
renderTableHeaders(table).forEach((row) => ret.push(row));
table.rows.forEach((row) => {
renderRow(table, row).forEach((row_) => ret.push(row_));
});
renderTableEnding(table).forEach((row) => ret.push(row));
return ret.join('\n');
};
export const renderSimpleTable = (rows: any[]) => {
const table = new TableInternal();
table.addRows(rows);
return renderTable(table);
};
export const printSimpleTable = (rows: any[]) => {
console.log(renderSimpleTable(rows));
};