starlight-table 1.0.0
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/README.md +118 -0
- package/package.json +17 -0
- package/starlight-table.mjs +94 -0
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# starlight-table
|
|
2
|
+
|
|
3
|
+
**Description**
|
|
4
|
+
|
|
5
|
+
starlight-table is a flexible terminal table and list builder for Starlight.
|
|
6
|
+
It does not force any predefined structure. Developers fully control columns,
|
|
7
|
+
rows, and layout.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Why starlight-table?
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
- No predefined schema
|
|
16
|
+
- Builder-style API
|
|
17
|
+
- Add rows dynamically
|
|
18
|
+
- Works for products, reports, logs, and data views
|
|
19
|
+
- Outputs clean ASCII tables
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Creating a Table
|
|
23
|
+
|
|
24
|
+
Use `createTable()` to start building a table.
|
|
25
|
+
|
|
26
|
+
`
|
|
27
|
+
import { createTable } from "starlight-table"
|
|
28
|
+
|
|
29
|
+
let table = createTable()
|
|
30
|
+
`
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Setting Headers
|
|
36
|
+
|
|
37
|
+
Headers are optional. You can define any number of columns.
|
|
38
|
+
|
|
39
|
+
`
|
|
40
|
+
table.setHeaders(["Product", "Price", "Stock"])
|
|
41
|
+
`
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Adding Rows
|
|
47
|
+
|
|
48
|
+
Rows can be added one by one or in batches.
|
|
49
|
+
|
|
50
|
+
`
|
|
51
|
+
table.addRow(["Keyboard", "$50", 10])
|
|
52
|
+
table.addRow(["Mouse", "$25", 20])
|
|
53
|
+
`
|
|
54
|
+
|
|
55
|
+
You can also add multiple rows at once.
|
|
56
|
+
|
|
57
|
+
`
|
|
58
|
+
table.addRows([
|
|
59
|
+
["Monitor", "$300", 5],
|
|
60
|
+
["USB Cable", "$5", 100]
|
|
61
|
+
])
|
|
62
|
+
`
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
## Rendering the Table
|
|
68
|
+
|
|
69
|
+
Once your data is ready, render the table as a string.
|
|
70
|
+
|
|
71
|
+
`
|
|
72
|
+
const output = table.render()
|
|
73
|
+
`
|
|
74
|
+
|
|
75
|
+
This output can be printed using `sldeploy`.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Creating Lists
|
|
81
|
+
|
|
82
|
+
starlight-table also supports simple lists.
|
|
83
|
+
|
|
84
|
+
`
|
|
85
|
+
import { createList } from "starlight-table"
|
|
86
|
+
|
|
87
|
+
const list = createList([
|
|
88
|
+
"Flexible",
|
|
89
|
+
"Readable",
|
|
90
|
+
"Developer-controlled"
|
|
91
|
+
])
|
|
92
|
+
`
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
## Common Use Cases
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
- Product listings
|
|
101
|
+
- CLI dashboards
|
|
102
|
+
- Debug output
|
|
103
|
+
- Reports
|
|
104
|
+
- Log summaries
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
## Design Philosophy
|
|
108
|
+
|
|
109
|
+
starlight-table is designed to:
|
|
110
|
+
|
|
111
|
+
- Give developers full control
|
|
112
|
+
- Remain simple and predictable
|
|
113
|
+
- Integrate naturally with Starlight
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
**There are no limits on rows or columns.**
|
|
117
|
+
|
|
118
|
+
You decide how your table looks.
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "starlight-table",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Flexible table and list builder for Starlight",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "starlight-table.mjs",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"starlight",
|
|
9
|
+
"table",
|
|
10
|
+
"cli",
|
|
11
|
+
"ascii",
|
|
12
|
+
"builder",
|
|
13
|
+
"terminal"
|
|
14
|
+
],
|
|
15
|
+
"author": "Macedon",
|
|
16
|
+
"license": "MIT"
|
|
17
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starlight Table Builder
|
|
3
|
+
* Fully configurable ASCII table generator
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
function pad(text, width) {
|
|
7
|
+
const str = String(text)
|
|
8
|
+
return str + " ".repeat(Math.max(0, width - str.length))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function separator(widths) {
|
|
12
|
+
return "+" + widths.map(w => "-".repeat(w + 2)).join("+") + "+"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function row(cells, widths) {
|
|
16
|
+
return "| " + cells.map((c, i) => pad(c, widths[i])).join(" | ") + " |"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class Table {
|
|
20
|
+
constructor() {
|
|
21
|
+
this.headers = []
|
|
22
|
+
this.rows = []
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setHeaders(headers) {
|
|
26
|
+
this.headers = headers.map(h => String(h))
|
|
27
|
+
return this
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
addRow(row) {
|
|
31
|
+
this.rows.push(row.map(v => v === null || v === undefined ? "" : String(v)))
|
|
32
|
+
return this
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
addRows(rows) {
|
|
36
|
+
for (const r of rows) {
|
|
37
|
+
this.addRow(r)
|
|
38
|
+
}
|
|
39
|
+
return this
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
columnWidths() {
|
|
43
|
+
const all = []
|
|
44
|
+
if (this.headers.length) all.push(this.headers)
|
|
45
|
+
all.push(...this.rows)
|
|
46
|
+
|
|
47
|
+
const cols = Math.max(...all.map(r => r.length))
|
|
48
|
+
const widths = []
|
|
49
|
+
|
|
50
|
+
for (let i = 0; i < cols; i++) {
|
|
51
|
+
widths[i] = Math.max(
|
|
52
|
+
...all.map(r => (r[i] ? r[i].length : 0))
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return widths
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
render() {
|
|
60
|
+
if (!this.headers.length && !this.rows.length) {
|
|
61
|
+
return ""
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const widths = this.columnWidths()
|
|
65
|
+
const lines = []
|
|
66
|
+
|
|
67
|
+
lines.push(separator(widths))
|
|
68
|
+
|
|
69
|
+
if (this.headers.length) {
|
|
70
|
+
lines.push(row(this.headers, widths))
|
|
71
|
+
lines.push(separator(widths))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const r of this.rows) {
|
|
75
|
+
lines.push(row(r, widths))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
lines.push(separator(widths))
|
|
79
|
+
return lines.join("\n")
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function createTable() {
|
|
84
|
+
return new Table()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function createList(items, bullet = "-") {
|
|
88
|
+
return items.map(i => `${bullet} ${i}`).join("\n")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export {
|
|
92
|
+
createTable,
|
|
93
|
+
createList
|
|
94
|
+
}
|