Skip to content

Commit a43146b

Browse files
committed
Merge branch 'develop' of github.com:privatenumber/terminal-columns
2 parents 0600f5e + 677ab5f commit a43146b

File tree

6 files changed

+45
-19
lines changed

6 files changed

+45
-19
lines changed

.github/demo.gif

4.45 KB
Loading

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
Readable tables for the terminal.
44

55
<p align="center">
6-
<img src=".github/demo.gif" width="400">
6+
<img src=".github/demo.gif" width="450">
77
<br>
88
<em>Tables can be automatically responsive!</em>
99
</p>
1010

1111
### Features
12-
- Wrap content to fit the column width
13-
- Supports column widths `auto`, `content-width`, viewport percents & static values
14-
- Supports horizontal & vertical padding
15-
- Allow rows to overflow into multiple rows
12+
- Content wrapped to fit column width
13+
- Column widths `auto`, `content-width`, viewport percents & static values
14+
- Align left & right
15+
- Horizontal & vertical padding
16+
- Rows can overflow into multiple rows
1617
- Easy to make responsive tables
1718

1819
[Try it out online](https://stackblitz.com/edit/terminal-columns-demo?devtoolsheight=50&file=examples/responsive-table.js&view=editor)
@@ -116,6 +117,20 @@ terminalColumns(
116117
)
117118
```
118119

120+
### Right align text
121+
You can align the content of the column by setting `align: 'right'`.
122+
123+
```ts
124+
terminalColumns(
125+
tableData,
126+
[
127+
{
128+
align: 'right'
129+
}
130+
]
131+
)
132+
```
133+
119134
### Responsive table by terminal width
120135
You can make the table responsive by passing in a function that computes the column width allocation based on the detected viewport width.
121136

@@ -220,6 +235,7 @@ type ColumnMeta = {
220235
paddingLeft?: number
221236
paddingTop?: number
222237
paddingBottom?: number
238+
align?: 'left' | 'right'
223239
}
224240
```
225241
@@ -266,4 +282,9 @@ Type: `number`
266282
267283
How many new lines to the bottom the column should have
268284
285+
##### align
286+
Type: `'left' | 'right'`
287+
288+
Default: `'left'`
269289
290+
Whether to align the text to the left or right.

examples/lorem-ipsum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const renderTable = (stdoutColumns: number) => {
2323
stdoutColumns,
2424
columns: [
2525
{
26+
align: 'right',
2627
paddingRight: 4,
2728
paddingBottom: 1,
2829
},

src/types.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ export type Row = string[];
22

33
export type ColumnWidth = number | 'content-width' | 'auto' | string;
44

5+
type Alignment = 'left' | 'right';
6+
57
export type ColumnMeta<Width = ColumnWidth> ={
68
width?: Width;
7-
// TODO: align
8-
// align?: string;
9+
align?: Alignment;
910
paddingRight?: number;
1011
paddingLeft?: number;
1112
paddingTop?: number;
1213
paddingBottom?: number;
1314
};
1415

15-
export type InternalColumnMeta<Width = ColumnWidth> = {
16-
width: Width;
16+
export type InternalColumnMeta<Width = ColumnWidth> = Required<ColumnMeta<Width>> & {
1717
autoOverflow?: number;
1818
contentWidth: number;
19-
paddingRight: number;
20-
paddingLeft: number;
21-
paddingTop: number;
22-
paddingBottom: number;
2319
paddingLeftString: string;
2420
paddingRightString: string;
2521
horizontalPadding: number;

src/utils/compute-column-widths.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const isPercentPattern = /^\d+%$/;
88

99
const defaultColumnMetas: InternalColumnMeta = {
1010
width: 'auto',
11+
align: 'left',
1112
contentWidth: 0,
1213
paddingLeft: 0,
1314
paddingRight: 0,

src/utils/render-row.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ export function renderRow(
5252
const rowLine = subRowWithData
5353
.map((column) => {
5454
const cellLine = column.lines[i] ?? '';
55+
const lineFiller = ' '.repeat(column.width - stringWidth(cellLine));
56+
let text = column.paddingLeftString;
5557

56-
return (
57-
column.paddingLeftString
58-
+ cellLine
59-
+ ' '.repeat(column.width - stringWidth(cellLine))
60-
+ column.paddingRightString
61-
);
58+
if (column.align === 'right') {
59+
text += lineFiller;
60+
}
61+
62+
text += cellLine;
63+
64+
if (column.align === 'left') {
65+
text += lineFiller;
66+
}
67+
68+
return text + column.paddingRightString;
6269
})
6370
.join('');
6471

0 commit comments

Comments
 (0)