@exodus/stasis 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/table.js ADDED
@@ -0,0 +1,63 @@
1
+ // A report's tables, as `stasis audit` and `stasis validate` print them, and their colours where the
2
+ // stream they go to takes colours.
3
+ import { styleText } from 'node:util'
4
+
5
+ // Strip line breaks (LF/CR/CRLF, U+2028/U+2029) from a cell (an advisory's title, say) so they can't
6
+ // break the box layout.
7
+ const cell = (v) => String(v ?? '').replace(/[\r\n\u2028\u2029]+/gu, ' ')
8
+
9
+ // Split a value into the physical lines a cell occupies. Non-multiline columns
10
+ // collapse every line break to a space via `cell` (one line). A column named in
11
+ // `multiline` (the `--why` reason column) keeps intentional `\n` breaks -- one
12
+ // physical line per entry -- while still flattening stray CR/separators in each.
13
+ const cellLines = (v, multiline) =>
14
+ multiline ? String(v ?? '').split('\n').map((s) => cell(s)) : [cell(v)]
15
+
16
+ // `rows` as a boxed table of `columns`, its header their names. `paint(text, column, row)`, where
17
+ // given, styles the text of each cell of a row (with colour, say), its padding after, unstyled.
18
+ export function formatTable(rows, columns, { multiline = [], paint } = {}) {
19
+ if (rows.length === 0) return ''
20
+ const ml = new Set(multiline)
21
+ const linesOf = (obj) => columns.map((c) => cellLines(obj[c], ml.has(c)))
22
+ const header = columns.map((c) => [c])
23
+ const body = rows.map((r) => linesOf(r))
24
+ const widths = columns.map((c, i) =>
25
+ Math.max(c.length, ...body.map((cells) => Math.max(0, ...cells[i].map((l) => l.length))))
26
+ )
27
+ const line = (l, m, r, fill) => l + widths.map((w) => fill.repeat(w + 2)).join(m) + r
28
+ // A row spans as many physical lines as its tallest cell; shorter cells pad
29
+ // out with blanks so the borders stay aligned.
30
+ const render = (cells, row) => {
31
+ const height = Math.max(...cells.map((c) => c.length))
32
+ const out = []
33
+ for (let i = 0; i < height; i++) {
34
+ out.push('│ ' + cells.map((c, j) => {
35
+ const text = c[i] ?? ''
36
+ return (paint && row && text !== '' ? paint(text, columns[j], row) : text) + ' '.repeat(widths[j] - text.length)
37
+ }).join(' │ ') + ' │')
38
+ }
39
+ return out.join('\n')
40
+ }
41
+ return [
42
+ line('┌', '┬', '┐', '─'),
43
+ render(header),
44
+ line('├', '┼', '┤', '─'),
45
+ ...body.map((cells, at) => render(cells, rows[at])),
46
+ line('└', '┴', '┘', '─'),
47
+ ].join('\n')
48
+ }
49
+
50
+ // Whether `stream` takes colours, as @exodus/test tells it: CI's logs do, though no terminal; a
51
+ // terminal that has them does (a TTY's hasColors); a pipe, or a file, doesn't.
52
+ export function takesColors(stream) {
53
+ return Boolean(process.env.CI) || Boolean(stream?.hasColors?.())
54
+ }
55
+
56
+ // Dimmed text's format: CI's logs show gray, not faint.
57
+ export const DIM = process.env.CI ? 'gray' : 'dim'
58
+
59
+ // `paint(format, text)`: `text` in `format` (util.styleText's: a colour, `bold`, or a list of them)
60
+ // where `colors` (else as `stream` takes them, takesColors), else as it is.
61
+ export function painter(stream, colors = takesColors(stream)) {
62
+ return (format, text) => (colors && format && text !== '' ? styleText(format, text, { validateStream: false }) : text)
63
+ }