Skip to content

Commit

Permalink
feat: add justified columns example
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 8, 2024
1 parent b87e13c commit b9d42b5
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/justified_columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()

0 comments on commit b9d42b5

Please sign in to comment.