@tooee/renderers 0.2.3 → 0.3.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/dist/code-blocks.d.ts +6 -6
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js +52 -42
- package/dist/code-blocks.js.map +1 -1
- package/dist/{CodeView.d.ts → code-view.d.ts} +3 -3
- package/dist/code-view.d.ts.map +1 -0
- package/dist/{CodeView.js → code-view.js} +5 -5
- package/dist/code-view.js.map +1 -0
- package/dist/{CommandPalette.d.ts → command-palette.d.ts} +2 -2
- package/dist/command-palette.d.ts.map +1 -0
- package/dist/{CommandPalette.js → command-palette.js} +27 -24
- package/dist/command-palette.js.map +1 -0
- package/dist/{ContextMenu.d.ts → context-menu.d.ts} +2 -2
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/{ContextMenu.js → context-menu.js} +24 -14
- package/dist/context-menu.js.map +1 -0
- package/dist/{DecorationLayer.d.ts → decoration-layer.d.ts} +2 -2
- package/dist/decoration-layer.d.ts.map +1 -0
- package/dist/decoration-layer.js +2 -0
- package/dist/decoration-layer.js.map +1 -0
- package/dist/{DocumentBindings.d.ts → document-bindings.d.ts} +4 -4
- package/dist/document-bindings.d.ts.map +1 -0
- package/dist/document-bindings.js +2 -0
- package/dist/document-bindings.js.map +1 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/markdown-blocks.d.ts +9 -13
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +98 -79
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/{MarkdownView.d.ts → markdown-view.d.ts} +20 -6
- package/dist/markdown-view.d.ts.map +1 -0
- package/dist/markdown-view.js +455 -0
- package/dist/markdown-view.js.map +1 -0
- package/dist/mermaid.d.ts +5 -5
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +33 -25
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts +5 -5
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js +64 -44
- package/dist/parsers.js.map +1 -1
- package/dist/{RowDocumentRenderable.d.ts → row-document-renderable.d.ts} +15 -11
- package/dist/row-document-renderable.d.ts.map +1 -0
- package/dist/{RowDocumentRenderable.js → row-document-renderable.js} +110 -62
- package/dist/row-document-renderable.js.map +1 -0
- package/dist/row-document.d.ts +1 -1
- package/dist/row-document.d.ts.map +1 -1
- package/dist/row-document.js +1 -1
- package/dist/row-document.js.map +1 -1
- package/dist/source.d.ts +6 -6
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +35 -26
- package/dist/source.js.map +1 -1
- package/dist/table-types.d.ts.map +1 -1
- package/dist/{Table.d.ts → table.d.ts} +6 -6
- package/dist/table.d.ts.map +1 -0
- package/dist/{Table.js → table.js} +65 -35
- package/dist/table.js.map +1 -0
- package/dist/text-table.d.ts.map +1 -1
- package/dist/text-table.js.map +1 -1
- package/dist/use-gutter-palette.d.ts +3 -0
- package/dist/use-gutter-palette.d.ts.map +1 -0
- package/dist/{useGutterPalette.js → use-gutter-palette.js} +4 -4
- package/dist/use-gutter-palette.js.map +1 -0
- package/package.json +5 -5
- package/src/code-blocks.tsx +122 -106
- package/src/{CodeView.tsx → code-view.tsx} +19 -14
- package/src/command-palette.tsx +163 -0
- package/src/context-menu.tsx +157 -0
- package/src/decoration-layer.ts +11 -0
- package/src/document-bindings.ts +16 -0
- package/src/index.ts +24 -23
- package/src/markdown-blocks.ts +178 -138
- package/src/markdown-view.tsx +853 -0
- package/src/mermaid.ts +94 -71
- package/src/parsers.ts +141 -103
- package/src/row-document-renderable.ts +672 -0
- package/src/row-document.ts +4 -4
- package/src/source.ts +92 -72
- package/src/table-types.ts +4 -4
- package/src/table.tsx +346 -0
- package/src/text-table.ts +4 -4
- package/src/use-gutter-palette.ts +15 -0
- package/dist/CodeView.d.ts.map +0 -1
- package/dist/CodeView.js.map +0 -1
- package/dist/CommandPalette.d.ts.map +0 -1
- package/dist/CommandPalette.js.map +0 -1
- package/dist/ContextMenu.d.ts.map +0 -1
- package/dist/ContextMenu.js.map +0 -1
- package/dist/DecorationLayer.d.ts.map +0 -1
- package/dist/DecorationLayer.js +0 -2
- package/dist/DecorationLayer.js.map +0 -1
- package/dist/DocumentBindings.d.ts.map +0 -1
- package/dist/DocumentBindings.js +0 -2
- package/dist/DocumentBindings.js.map +0 -1
- package/dist/MarkdownView.d.ts.map +0 -1
- package/dist/MarkdownView.js +0 -275
- package/dist/MarkdownView.js.map +0 -1
- package/dist/RowDocumentRenderable.d.ts.map +0 -1
- package/dist/RowDocumentRenderable.js.map +0 -1
- package/dist/Table.d.ts.map +0 -1
- package/dist/Table.js.map +0 -1
- package/dist/useGutterPalette.d.ts +0 -3
- package/dist/useGutterPalette.d.ts.map +0 -1
- package/dist/useGutterPalette.js.map +0 -1
- package/src/CommandPalette.tsx +0 -147
- package/src/ContextMenu.tsx +0 -132
- package/src/DecorationLayer.ts +0 -11
- package/src/DocumentBindings.ts +0 -16
- package/src/MarkdownView.tsx +0 -541
- package/src/RowDocumentRenderable.ts +0 -593
- package/src/Table.tsx +0 -300
- package/src/useGutterPalette.ts +0 -15
package/src/Table.tsx
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import { useTerminalDimensions } from "@opentui/react"
|
|
2
|
-
import { useTheme } from "@tooee/themes"
|
|
3
|
-
import { useMemo, useRef } from "react"
|
|
4
|
-
import type { DocumentBindings } from "./DocumentBindings.js"
|
|
5
|
-
import type { ColumnDef, TableRow } from "./table-types.js"
|
|
6
|
-
import {
|
|
7
|
-
DEFAULT_SIGN_COLUMN_WIDTH,
|
|
8
|
-
computeRowDocumentGutterWidth,
|
|
9
|
-
type RowDocumentRenderable,
|
|
10
|
-
} from "./RowDocumentRenderable.js"
|
|
11
|
-
import { useGutterPalette } from "./useGutterPalette.js"
|
|
12
|
-
import "./row-document.js"
|
|
13
|
-
|
|
14
|
-
export interface TableProps {
|
|
15
|
-
columns: ColumnDef[]
|
|
16
|
-
rows: TableRow[]
|
|
17
|
-
/** Maximum width for the table. If not provided, uses terminal width. */
|
|
18
|
-
maxWidth?: number
|
|
19
|
-
/** Minimum width for any column (default: 4) */
|
|
20
|
-
minColumnWidth?: number
|
|
21
|
-
/** Maximum width for any column (default: 50) */
|
|
22
|
-
maxColumnWidth?: number
|
|
23
|
-
/** Number of rows to sample for width calculation (default: 100) */
|
|
24
|
-
sampleSize?: number
|
|
25
|
-
/** Show line numbers in the gutter (default: true) */
|
|
26
|
-
showLineNumbers?: boolean
|
|
27
|
-
/**
|
|
28
|
-
* Binds the data rows to a document controller: its ref, the decoration
|
|
29
|
-
* layers to paint, and the mouse handler. Rows are data-row indices; the
|
|
30
|
-
* fixed header sits outside the row document. Omit it to render a static,
|
|
31
|
-
* non-interactive table.
|
|
32
|
-
*/
|
|
33
|
-
document?: DocumentBindings
|
|
34
|
-
/** Column width mode: "content" sizes to content (default), "fill" expands to fill available width */
|
|
35
|
-
columnWidthMode?: "content" | "fill"
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const PADDING = 1
|
|
39
|
-
const MARGIN = 1 // horizontal margin on each side of the table
|
|
40
|
-
const DEFAULT_MIN_COL_WIDTH = 4
|
|
41
|
-
const DEFAULT_MAX_COL_WIDTH = 80
|
|
42
|
-
const DEFAULT_SAMPLE_SIZE = 100
|
|
43
|
-
|
|
44
|
-
function isNumeric(value: string): boolean {
|
|
45
|
-
return /^\s*-?[\d,]+\.?\d*\s*$/.test(value)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
interface ColumnWidthOptions {
|
|
49
|
-
minColumnWidth: number
|
|
50
|
-
maxColumnWidth: number
|
|
51
|
-
sampleSize: number
|
|
52
|
-
columnWidthMode?: "content" | "fill"
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function sampleRows(rows: string[][], sampleSize: number): string[][] {
|
|
56
|
-
if (rows.length <= sampleSize) return rows
|
|
57
|
-
// Sample evenly distributed rows for representative widths
|
|
58
|
-
const step = rows.length / sampleSize
|
|
59
|
-
const sampled: string[][] = []
|
|
60
|
-
for (let i = 0; i < sampleSize; i++) {
|
|
61
|
-
sampled.push(rows[Math.floor(i * step)])
|
|
62
|
-
}
|
|
63
|
-
return sampled
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function computeColumnWidths(
|
|
67
|
-
headers: string[],
|
|
68
|
-
rows: string[][],
|
|
69
|
-
maxWidth: number,
|
|
70
|
-
options: ColumnWidthOptions,
|
|
71
|
-
): number[] {
|
|
72
|
-
const { minColumnWidth, maxColumnWidth, sampleSize } = options
|
|
73
|
-
const colCount = headers.length
|
|
74
|
-
|
|
75
|
-
// Sample rows for performance on large tables
|
|
76
|
-
const sampledRows = sampleRows(rows, sampleSize)
|
|
77
|
-
|
|
78
|
-
// Calculate natural width for each column (header + content + padding)
|
|
79
|
-
// Use Bun.stringWidth for correct display width with CJK/emoji
|
|
80
|
-
const naturalWidths = headers.map((header, col) => {
|
|
81
|
-
const headerLen = Bun.stringWidth(header)
|
|
82
|
-
const maxRowLen = sampledRows.reduce(
|
|
83
|
-
(max, row) => Math.max(max, Bun.stringWidth(row[col] ?? "")),
|
|
84
|
-
0,
|
|
85
|
-
)
|
|
86
|
-
const contentWidth = Math.max(headerLen, maxRowLen)
|
|
87
|
-
// Apply min/max constraints before adding padding
|
|
88
|
-
const constrainedWidth = Math.min(maxColumnWidth, Math.max(minColumnWidth, contentWidth))
|
|
89
|
-
return constrainedWidth + PADDING * 2
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
// No border overhead -- flexbox rows don't have border characters
|
|
93
|
-
const totalNatural = naturalWidths.reduce((a, b) => a + b, 0)
|
|
94
|
-
|
|
95
|
-
// If everything fits, use natural widths (or distribute extra space in fill mode)
|
|
96
|
-
if (totalNatural <= maxWidth) {
|
|
97
|
-
if (options.columnWidthMode === "fill" && totalNatural < maxWidth) {
|
|
98
|
-
const extra = maxWidth - totalNatural
|
|
99
|
-
const perCol = Math.floor(extra / colCount)
|
|
100
|
-
const remainder = extra - perCol * colCount
|
|
101
|
-
return naturalWidths.map((w, i) => w + perCol + (i < remainder ? 1 : 0))
|
|
102
|
-
}
|
|
103
|
-
return naturalWidths
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const available = maxWidth
|
|
107
|
-
const minColWidthWithPadding = minColumnWidth + PADDING * 2
|
|
108
|
-
|
|
109
|
-
// Extreme case: not even minimum widths fit
|
|
110
|
-
if (maxWidth <= colCount * minColWidthWithPadding) {
|
|
111
|
-
return naturalWidths.map(() =>
|
|
112
|
-
Math.max(minColWidthWithPadding, Math.floor(available / colCount)),
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Give compact columns their natural width, distribute rest proportionally
|
|
117
|
-
const compact: boolean[] = naturalWidths.map((w) => w <= 20)
|
|
118
|
-
const compactTotal = naturalWidths.reduce((sum, w, i) => sum + (compact[i] ? w : 0), 0)
|
|
119
|
-
const remaining = available - compactTotal
|
|
120
|
-
const longTotal = naturalWidths.reduce((sum, w, i) => sum + (compact[i] ? 0 : w), 0)
|
|
121
|
-
|
|
122
|
-
if (longTotal === 0 || remaining <= 0) {
|
|
123
|
-
// All compact or no space left -- distribute evenly
|
|
124
|
-
const total = naturalWidths.reduce((a, b) => a + b, 0)
|
|
125
|
-
return naturalWidths.map((w) =>
|
|
126
|
-
Math.max(minColWidthWithPadding, Math.floor((w / total) * available)),
|
|
127
|
-
)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return naturalWidths.map((w, i) => {
|
|
131
|
-
if (compact[i]) return w
|
|
132
|
-
return Math.max(minColWidthWithPadding, Math.floor((w / longTotal) * remaining))
|
|
133
|
-
})
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function formatCellValue(value: unknown): string {
|
|
137
|
-
if (value == null) return ""
|
|
138
|
-
if (typeof value === "string") return value
|
|
139
|
-
if (typeof value === "number" || typeof value === "boolean") return String(value)
|
|
140
|
-
if (value instanceof Date) return value.toISOString()
|
|
141
|
-
try {
|
|
142
|
-
return JSON.stringify(value)
|
|
143
|
-
} catch {
|
|
144
|
-
return String(value)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export function Table({
|
|
149
|
-
columns,
|
|
150
|
-
rows,
|
|
151
|
-
maxWidth,
|
|
152
|
-
minColumnWidth = DEFAULT_MIN_COL_WIDTH,
|
|
153
|
-
maxColumnWidth = DEFAULT_MAX_COL_WIDTH,
|
|
154
|
-
sampleSize = DEFAULT_SAMPLE_SIZE,
|
|
155
|
-
showLineNumbers = true,
|
|
156
|
-
document,
|
|
157
|
-
columnWidthMode = "content",
|
|
158
|
-
}: TableProps) {
|
|
159
|
-
const { theme } = useTheme()
|
|
160
|
-
const palette = useGutterPalette()
|
|
161
|
-
const { width: terminalWidth } = useTerminalDimensions()
|
|
162
|
-
const internalDocRef = useRef<RowDocumentRenderable | null>(null)
|
|
163
|
-
const rowDocumentRef = document?.ref ?? internalDocRef
|
|
164
|
-
|
|
165
|
-
// Compute available content width: start with total space, subtract margins and gutter
|
|
166
|
-
const gutterWidth = useMemo(
|
|
167
|
-
() =>
|
|
168
|
-
computeRowDocumentGutterWidth({
|
|
169
|
-
showLineNumbers,
|
|
170
|
-
rowCount: rows.length,
|
|
171
|
-
signColumnWidth: DEFAULT_SIGN_COLUMN_WIDTH,
|
|
172
|
-
}),
|
|
173
|
-
[showLineNumbers, rows.length],
|
|
174
|
-
)
|
|
175
|
-
const effectiveMaxWidth = Math.max(0, (maxWidth ?? terminalWidth) - MARGIN * 2 - gutterWidth)
|
|
176
|
-
|
|
177
|
-
const headers = useMemo(() => columns.map((column) => column.header ?? column.key), [columns])
|
|
178
|
-
const normalizedRows = useMemo(
|
|
179
|
-
() => rows.map((row) => columns.map((column) => formatCellValue(row[column.key]))),
|
|
180
|
-
[columns, rows],
|
|
181
|
-
)
|
|
182
|
-
|
|
183
|
-
const colWidths = useMemo(
|
|
184
|
-
() =>
|
|
185
|
-
computeColumnWidths(headers, normalizedRows, effectiveMaxWidth, {
|
|
186
|
-
minColumnWidth,
|
|
187
|
-
maxColumnWidth,
|
|
188
|
-
sampleSize,
|
|
189
|
-
columnWidthMode,
|
|
190
|
-
}),
|
|
191
|
-
[
|
|
192
|
-
headers,
|
|
193
|
-
normalizedRows,
|
|
194
|
-
effectiveMaxWidth,
|
|
195
|
-
minColumnWidth,
|
|
196
|
-
maxColumnWidth,
|
|
197
|
-
sampleSize,
|
|
198
|
-
columnWidthMode,
|
|
199
|
-
],
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
// Detect right-aligned columns: explicit align prop or auto-detect numeric
|
|
203
|
-
const alignments = useMemo(
|
|
204
|
-
() =>
|
|
205
|
-
columns.map((column, colIdx) => {
|
|
206
|
-
if (column.align === "right") return true
|
|
207
|
-
if (column.align === "left") return false
|
|
208
|
-
const sampleValues = normalizedRows.slice(0, 10).map((row) => row[colIdx] ?? "")
|
|
209
|
-
const numericCount = sampleValues.filter(isNumeric).length
|
|
210
|
-
return numericCount > sampleValues.length / 2
|
|
211
|
-
}),
|
|
212
|
-
[columns, normalizedRows],
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
const rowElements = useMemo(
|
|
216
|
-
() =>
|
|
217
|
-
normalizedRows.map((row, i) => (
|
|
218
|
-
<box key={i} style={{ flexDirection: "row" }}>
|
|
219
|
-
{row.map((cell, j) => {
|
|
220
|
-
const contentWidth = colWidths[j] - PADDING * 2
|
|
221
|
-
const cellWidth = Bun.stringWidth(cell)
|
|
222
|
-
const displayCell =
|
|
223
|
-
alignments[j] && cellWidth <= contentWidth
|
|
224
|
-
? " ".repeat(contentWidth - cellWidth) + cell
|
|
225
|
-
: cell
|
|
226
|
-
return (
|
|
227
|
-
<text
|
|
228
|
-
key={j}
|
|
229
|
-
content={displayCell}
|
|
230
|
-
wrapMode="word"
|
|
231
|
-
style={{
|
|
232
|
-
width: colWidths[j],
|
|
233
|
-
paddingLeft: PADDING,
|
|
234
|
-
paddingRight: PADDING,
|
|
235
|
-
}}
|
|
236
|
-
fg={theme.text}
|
|
237
|
-
/>
|
|
238
|
-
)
|
|
239
|
-
})}
|
|
240
|
-
</box>
|
|
241
|
-
)),
|
|
242
|
-
[normalizedRows, colWidths, alignments, theme.text],
|
|
243
|
-
)
|
|
244
|
-
|
|
245
|
-
return (
|
|
246
|
-
<box
|
|
247
|
-
style={{
|
|
248
|
-
flexDirection: "column",
|
|
249
|
-
flexGrow: 1,
|
|
250
|
-
marginLeft: MARGIN,
|
|
251
|
-
marginRight: MARGIN,
|
|
252
|
-
marginBottom: MARGIN,
|
|
253
|
-
}}
|
|
254
|
-
>
|
|
255
|
-
{/* Fixed header row — outside row-document so it stays visible */}
|
|
256
|
-
<box style={{ flexDirection: "row", flexShrink: 0, paddingLeft: gutterWidth }}>
|
|
257
|
-
{headers.map((h, i) => (
|
|
258
|
-
<text
|
|
259
|
-
key={i}
|
|
260
|
-
content={h}
|
|
261
|
-
style={{ width: colWidths[i], paddingLeft: PADDING, paddingRight: PADDING }}
|
|
262
|
-
fg={theme.primary}
|
|
263
|
-
/>
|
|
264
|
-
))}
|
|
265
|
-
</box>
|
|
266
|
-
|
|
267
|
-
{/* Fixed header underline */}
|
|
268
|
-
<box style={{ flexDirection: "row", flexShrink: 0, paddingLeft: gutterWidth }}>
|
|
269
|
-
{colWidths.map((w, i) => (
|
|
270
|
-
<text
|
|
271
|
-
key={i}
|
|
272
|
-
content={"\u2500".repeat(w - PADDING * 2)}
|
|
273
|
-
style={{ width: w, paddingLeft: PADDING, paddingRight: PADDING }}
|
|
274
|
-
fg={theme.border}
|
|
275
|
-
/>
|
|
276
|
-
))}
|
|
277
|
-
</box>
|
|
278
|
-
|
|
279
|
-
{/* Scrollable data rows */}
|
|
280
|
-
<row-document
|
|
281
|
-
ref={rowDocumentRef}
|
|
282
|
-
mode="multi"
|
|
283
|
-
rowChildOffset={0}
|
|
284
|
-
showGutter={true}
|
|
285
|
-
showLineNumbers={showLineNumbers}
|
|
286
|
-
signColumnWidth={DEFAULT_SIGN_COLUMN_WIDTH}
|
|
287
|
-
palette={palette}
|
|
288
|
-
decorations={document?.decorations}
|
|
289
|
-
style={{ flexGrow: 1 }}
|
|
290
|
-
onMouseDown={document?.onMouseDown}
|
|
291
|
-
>
|
|
292
|
-
{rowElements}
|
|
293
|
-
</row-document>
|
|
294
|
-
</box>
|
|
295
|
-
)
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Exported for testing and MarkdownView
|
|
299
|
-
export { computeColumnWidths, isNumeric, sampleRows }
|
|
300
|
-
export type { ColumnWidthOptions }
|
package/src/useGutterPalette.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react"
|
|
2
|
-
import { useTheme } from "@tooee/themes"
|
|
3
|
-
import type { RowDocumentPalette } from "./RowDocumentRenderable.js"
|
|
4
|
-
|
|
5
|
-
export function useGutterPalette(): RowDocumentPalette {
|
|
6
|
-
const { theme } = useTheme()
|
|
7
|
-
|
|
8
|
-
return useMemo(
|
|
9
|
-
() => ({
|
|
10
|
-
gutterFg: theme.textMuted,
|
|
11
|
-
gutterBg: theme.backgroundElement,
|
|
12
|
-
}),
|
|
13
|
-
[theme.textMuted, theme.backgroundElement],
|
|
14
|
-
)
|
|
15
|
-
}
|