table
table copied to clipboard
Add option to expand one column to fill console
In node I can get the width of the console from process.stdout.columns. So I can get the available rest width for one column with the following formula:
const fixedColumnsWidth = [ w1, w2, ..., wn-1];
const fixedColumnsCount = fixedColumnsWidth.length; // n - 1
const consoleWidth = process.stdout.columns; // Other environments could not have process
// calculate width of column n
const wn = consoleWidth
- fixedColumnsWidth.reduce((sum, width) => sum + width, 0) // get the sum of all fixed column widths
- (fixedColumnsCount + 1) * 3 // add 1 for the variable width column, each column has three extra chars
- 1; // subtract one char for the first vertical line
I have only to make sure that wn
does not get smaller than zero. So we should also define a minWidth
for this column. I am using this on my tables with the help of a little manual calculation and a ternary expression.