/
justified_columns.ts
49 lines (43 loc) · 1.15 KB
/
justified_columns.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
// import stringWidth from 'string-width'
import { TERMINAL_SIZE, justify, wrap } from '../src/helpers.js'
const metrics = [
{
title: 'First contentful paint',
timing: '2.7s',
},
{
title: 'Largest contentful pain',
timing: '3.2s',
},
{
title: 'Speed index',
timing: '2.7s',
},
{
title: 'Initial server response was short',
timing: 'Root document took 20ms',
},
]
function renderList() {
const titles = metrics.map(({ title }) => title)
const timings = metrics.map(({ timing }) => timing)
/**
* Uncomment the following line if you do not want
* to trim the timing column.
*/
// const largestTiming = Math.max(...timings.map((timing) => stringWidth(timing)))
const largestTiming = 10
const justifiedTitles = justify(titles, {
maxWidth: TERMINAL_SIZE - largestTiming,
align: 'left',
})
const justifiedTimings = wrap(justify(timings, { maxWidth: largestTiming }), {
startColumn: TERMINAL_SIZE - largestTiming,
endColumn: TERMINAL_SIZE,
trimStart: true,
})
justifiedTitles.forEach((title, index) => {
console.log(`${title}${justifiedTimings[index]}`)
})
}
renderList()