Skip to content

Commit 38bbb32

Browse files
committed
feat: add support for specifying fixed column width(s)
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent a130400 commit 38bbb32

File tree

11 files changed

+451
-111
lines changed

11 files changed

+451
-111
lines changed

lib/node_modules/@stdlib/plot/table/unicode/README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ The constructor accepts the following `options`:
6969

7070
- **columnSeparator**: column separator character(s). Default: `'│'`.
7171

72+
- **columnWidth**: column width(s) in units of grapheme clusters (i.e., visual characters). When `null`, column widths are determined according to cell content. When set to a specific width, a column is rendered with the specified width, unless overridden by a `maxColumnWidth` option which is less than the specified width. Default: `null`.
73+
7274
- **cornerTopLeft**: top-left table corner grapheme cluster (i.e., visual character). Default: `'┌'`.
7375

7476
- **cornerTopRight**: top-right table corner grapheme cluster (i.e., visual character). Default: `'┌'`.
@@ -123,7 +125,7 @@ The constructor accepts the following `options`:
123125

124126
- **margins**: table margins in the order `[top, right, bottom, left]`. This is a shorthand option instead of specifying margins individually. Default: `[ 0, 0, 0, 0 ]`.
125127

126-
- **maxColumnWidth**: maximum cell width(s) in units of grapheme clusters (i.e., visual characters). Default: `+infinity`.
128+
- **maxColumnWidth**: maximum cell width(s) in units of grapheme clusters (i.e., visual characters). If less than a column's specified width, this option overrides the `columnWidth` option. Default: `+infinity`.
127129

128130
- **maxWidth**: maximum table width in units of grapheme clusters (i.e., visual characters). Default: `+infinity`.
129131

@@ -327,6 +329,34 @@ var v = table.columnSeparator;
327329
// returns '|'
328330
```
329331

332+
<a name="prop-column-width"></a>
333+
334+
#### UnicodeTable.prototype.columnWidth
335+
336+
Column width in units of grapheme clusters (i.e., visual characters).
337+
338+
```javascript
339+
var table = new UnicodeTable();
340+
341+
// Set:
342+
table.columnWidth = 10;
343+
344+
// Get:
345+
var v = table.columnWidth;
346+
// returns 10
347+
348+
// Set:
349+
table.columnWidth = [ 10, 15 ];
350+
351+
// Get:
352+
v = table.columnWidth;
353+
// returns [ 10, 15 ]
354+
```
355+
356+
When `null`, column widths are determined according to cell content. When set to a specific width, a column is rendered with the specified width, unless overridden by a [`maxColumnWidth`](#prop-max-column-width) which is less than the specified width.
357+
358+
To specify a column width for each column, provide an array of widths.
359+
330360
<a name="prop-corner-top-left"></a>
331361

332362
#### UnicodeTable.prototype.cornerTopLeft
@@ -735,11 +765,11 @@ var v = table.margins;
735765
// returns [ 2, 2, 2, 2 ]
736766
```
737767

738-
<a name="prop-max-cell-width"></a>
768+
<a name="prop-max-column-width"></a>
739769

740770
#### UnicodeTable.prototype.maxColumnWidth
741771

742-
Maximum cell width in units of grapheme clusters (i.e., visual characters). If cell data is longer than the maximum cell width, the rendered cell data is truncated. To specify the maximum cell width for each column of cells, provide an array of maximum cell widths.
772+
Maximum column width in units of grapheme clusters (i.e., visual characters). If column data is longer than the maximum column width, the rendered column data is truncated. To specify the maximum column width for each column, provide an array of maximum widths.
743773

744774
```javascript
745775
var table = new UnicodeTable();
@@ -756,8 +786,11 @@ table.maxColumnWidth = [ 10, 15 ];
756786

757787
// Get:
758788
v = table.maxColumnWidth;
789+
// returns [ 10, 15 ]
759790
```
760791

792+
If set to a value which is less than a column's specified width, the maximum column width takes precedence over [`columnWidth`](#prop-column-width).
793+
761794
<a name="prop-max-width"></a>
762795

763796
#### UnicodeTable.prototype.maxWidth

lib/node_modules/@stdlib/plot/table/unicode/benchmark/benchmark.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ bench( pkg+'::instantiation,data,options', function benchmark( b ) {
125125
'borderLeft': '7890',
126126
'borders': [ '1', '23', '456', '7890' ],
127127
'bufferSize': 10,
128+
'columnWidth': null,
128129
'columnSeparator': 'None',
129130
'cornerTopLeft': '*',
130131
'cornerTopRight': '*',
@@ -437,6 +438,62 @@ bench( pkg+'::set,get:columnSeparator', function benchmark( b ) {
437438
b.end();
438439
});
439440

441+
bench( pkg+'::set,get:columnWidth:value=number', function benchmark( b ) {
442+
var values;
443+
var v;
444+
var i;
445+
446+
values = [
447+
10,
448+
20,
449+
30,
450+
40
451+
];
452+
v = new UnicodeTable();
453+
454+
b.tic();
455+
for ( i = 0; i < b.iterations; i++ ) {
456+
v.columnWidth = values[ i % values.length ];
457+
if ( !isNumber( v.columnWidth ) ) {
458+
b.fail( 'should return a number' );
459+
}
460+
}
461+
b.toc();
462+
if ( !isNumber( v.columnWidth ) ) {
463+
b.fail( 'should return a number' );
464+
}
465+
b.pass( 'benchmark finished' );
466+
b.end();
467+
});
468+
469+
bench( pkg+'::set,get:columnWidth:value=array', function benchmark( b ) {
470+
var values;
471+
var v;
472+
var i;
473+
474+
values = [
475+
[ 1, 2, 3 ],
476+
[ 4, 5, 6 ],
477+
[ 7, null, 9 ],
478+
[ null, null, null ]
479+
];
480+
v = new UnicodeTable();
481+
482+
b.tic();
483+
for ( i = 0; i < b.iterations; i++ ) {
484+
v.columnWidth = values[ i % values.length ];
485+
if ( !isArray( v.columnWidth ) ) {
486+
b.fail( 'should return an array' );
487+
}
488+
}
489+
b.toc();
490+
if ( !isArray( v.columnWidth ) ) {
491+
b.fail( 'should return an array' );
492+
}
493+
b.pass( 'benchmark finished' );
494+
b.end();
495+
});
496+
440497
bench( pkg+'::set,get:cornerTopLeft', function benchmark( b ) {
441498
var values;
442499
var v;

lib/node_modules/@stdlib/plot/table/unicode/docs/repl.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
options.columnSeparator: string (optional)
4848
Column separator character(s). Default: '│'.
4949

50+
options.columnWidth: Array<integer|null>|integer|null (optional)
51+
Column width(s) in units of grapheme clusters (i.e., visual
52+
characters). When `null`, column widths are determined according to cell
53+
content. When set to a specific width, a column is rendered with the
54+
specified width, unless overridden by a `maxColumnWidth` option which is
55+
less than the specified width. Default: null.
56+
5057
options.cornerTopLeft: string (optional)
5158
Top-left table corner grapheme cluster (i.e., visual character).
5259
Default: '┌'.
@@ -135,9 +142,10 @@
135142
Table margins in the order [top, right, bottom, left]. Default:
136143
[ 0, 0, 0, 0 ].
137144

138-
options.maxColumnWidth: Array<integer>|integer|null (optional)
145+
options.maxColumnWidth: Array<integer|null>|integer|null (optional)
139146
Maximum column width(s) in units of grapheme clusters (i.e., visual
140-
characters). Default: +infinity.
147+
characters). If less than a column's specified width, this option
148+
overrides the `columnWidth` option. Default: +infinity.
141149

142150
options.maxWidth: integer|null (optional)
143151
Maximum table width in units of grapheme clusters (i.e., visual
@@ -194,6 +202,9 @@
194202
table.columnSeparator
195203
Column separator character(s).
196204

205+
table.columnWidth
206+
Column width(s).
207+
197208
table.cornerTopLeft
198209
Top-left table corner grapheme cluster (i.e., visual character).
199210

lib/node_modules/@stdlib/plot/table/unicode/examples/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var table = new UnicodeTable( data, {
3636
'rowSeparator': '-',
3737
'columnSeparator': '#$%',
3838
'maxWidth': 200,
39+
'columnWidth': 15,
3940
'marginLeft': 5,
4041
'marginRight': 5,
4142
'align': 'right',
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var zeros = require( '@stdlib/array/base/zeros' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Resolves individual column widths based on table data and specified paddings.
30+
*
31+
* @private
32+
* @param {NonNegativeInteger} nrows - number of table rows
33+
* @param {NonNegativeInteger} ncols - number of table columns
34+
* @param {ndarray} data - table data
35+
* @param {(ndarray|null)} headers - table headers
36+
* @param {NonNegativeIntegerArray} leftPadding - left padding
37+
* @param {NonNegativeIntegerArray} rightPadding - right padding
38+
* @returns {NonNegativeIntegerArray} column widths
39+
*/
40+
function columnWidths( nrows, ncols, data, headers, leftPadding, rightPadding ) { // eslint-disable-line max-len
41+
var lpad;
42+
var rpad;
43+
var out;
44+
var len;
45+
var M;
46+
var N;
47+
var v;
48+
var i;
49+
var j;
50+
51+
M = leftPadding.length;
52+
N = rightPadding.length;
53+
54+
out = zeros( ncols );
55+
if ( headers ) {
56+
for ( j = 0; j < ncols; j++ ) {
57+
lpad = leftPadding[ j%M ];
58+
rpad = rightPadding[ j%N ];
59+
v = headers.get( 0, j );
60+
len = lpad + v.ngraphemes + rpad;
61+
if ( len > out[ j ] ) {
62+
out[ j ] = len;
63+
}
64+
}
65+
}
66+
for ( i = 0; i < nrows; i++ ) {
67+
for ( j = 0; j < ncols; j++ ) {
68+
lpad = leftPadding[ j%M ];
69+
rpad = rightPadding[ j%N ];
70+
v = data.get( i, j );
71+
len = lpad + v.ngraphemes + rpad;
72+
if ( len > out[ j ] ) {
73+
out[ j ] = len;
74+
}
75+
}
76+
}
77+
return out;
78+
}
79+
80+
81+
// EXPORTS //
82+
83+
module.exports = columnWidths;

lib/node_modules/@stdlib/plot/table/unicode/lib/defaults.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ function defaults() {
7171
// Buffer size:
7272
out.bufferSize = FLOAT64_MAX;
7373

74+
// Column separator:
75+
out.columnSeparator = {
76+
'value': '|',
77+
'graphemes': [ '|' ],
78+
'ngraphemes': 1
79+
};
80+
81+
// Column widths:
82+
out.columnWidth = null; // by default, automatically size based on content
83+
7484
// Corner grapheme clusters:
7585
out.corners = [
7686
{
@@ -99,13 +109,6 @@ function defaults() {
99109
}
100110
];
101111

102-
// Column separator:
103-
out.columnSeparator = {
104-
'value': '|',
105-
'graphemes': [ '|' ],
106-
'ngraphemes': 1
107-
};
108-
109112
// Cell format string:
110113
out.format = [ '%s' ];
111114

0 commit comments

Comments
 (0)