@zyro-sec/merman 0.0.0-stage → 1.0.3
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/package.json +29 -4
- package/script/layout-audit.ts +98 -0
- package/src/core/canvas.ts +228 -0
- package/src/core/color/style.ts +56 -0
- package/src/core/drawing.ts +301 -0
- package/src/core/geometry.ts +347 -0
- package/src/core/mermaid.ts +38 -0
- package/src/core/render-grid.ts +51 -0
- package/src/core/spatial.ts +243 -0
- package/src/core/text-lines.ts +32 -0
- package/src/core/text.ts +44 -0
- package/src/detect.ts +17 -0
- package/src/diagnostics.ts +16 -0
- package/src/flowchart/drawing.ts +340 -0
- package/src/flowchart/labels.ts +144 -0
- package/src/flowchart/layout.ts +1427 -0
- package/src/flowchart/options.ts +12 -0
- package/src/flowchart/parser.ts +467 -0
- package/src/flowchart/render.ts +10 -0
- package/src/flowchart/routing.ts +1542 -0
- package/src/flowchart/style.ts +79 -0
- package/src/flowchart/types.ts +65 -0
- package/src/gantt/diagram.ts +8 -0
- package/src/gantt/drawing.ts +191 -0
- package/src/gantt/parser.ts +161 -0
- package/src/gantt/render-grid.ts +17 -0
- package/src/gantt/style.ts +36 -0
- package/src/gantt/types.ts +44 -0
- package/src/gitgraph/diagram.ts +8 -0
- package/src/gitgraph/drawing.ts +234 -0
- package/src/gitgraph/parser.ts +219 -0
- package/src/gitgraph/render-grid.ts +17 -0
- package/src/gitgraph/style.ts +37 -0
- package/src/gitgraph/types.ts +36 -0
- package/src/markdown.ts +336 -0
- package/src/palette.ts +75 -0
- package/src/plugin.ts +15 -0
- package/src/sequence/diagram.ts +8 -0
- package/src/sequence/drawing.ts +287 -0
- package/src/sequence/options.ts +8 -0
- package/src/sequence/parser.ts +282 -0
- package/src/sequence/placement.ts +684 -0
- package/src/sequence/render-grid.ts +22 -0
- package/src/sequence/style.ts +81 -0
- package/src/sequence/types.ts +74 -0
- package/src/state/diagram.ts +8 -0
- package/src/state/drawing.ts +360 -0
- package/src/state/endpoint.ts +15 -0
- package/src/state/layout.ts +1134 -0
- package/src/state/note.ts +59 -0
- package/src/state/options.ts +11 -0
- package/src/state/parser.ts +224 -0
- package/src/state/render-grid.ts +22 -0
- package/src/state/routing.ts +1522 -0
- package/src/state/search.ts +103 -0
- package/src/state/style.ts +52 -0
- package/src/state/types.ts +63 -0
- package/src/state/visible-model.ts +77 -0
- package/src/test/diagram.ts +32 -0
- package/src/test/layout-audit/fixtures.ts +538 -0
- package/src/test/layout-audit/harness.ts +522 -0
- package/src/timeline/diagram.ts +8 -0
- package/src/timeline/drawing.ts +109 -0
- package/src/timeline/parser.ts +119 -0
- package/src/timeline/render-grid.ts +17 -0
- package/src/timeline/style.ts +36 -0
- package/src/timeline/types.ts +31 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
2
3
|
"name": "@zyro-sec/merman",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
4
|
+
"version": "1.0.3",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./markdown": "./src/markdown.ts",
|
|
8
|
+
"./palette": "./src/palette.ts",
|
|
9
|
+
"./plugin": "./src/plugin.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"audit:layouts": "bun run script/layout-audit.ts",
|
|
13
|
+
"test": "bun test --timeout 30000 --only-failures",
|
|
14
|
+
"typecheck": "bun check --no-script",
|
|
15
|
+
"typecheck:tsc": "tsc --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@zyro-sec/plugin": "^1.0.3",
|
|
19
|
+
"@opentui/core": "0.9.9",
|
|
20
|
+
"entities": "7.0.1",
|
|
21
|
+
"string-width": "7.2.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@tsconfig/bun": "1.0.9",
|
|
25
|
+
"@types/bun": "1.4.3",
|
|
26
|
+
"@typescript/native": "npm:typescript@7.1.0-beta"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { mkdir } from "node:fs/promises"
|
|
2
|
+
import { dirname, resolve } from "node:path"
|
|
3
|
+
import {
|
|
4
|
+
auditAllFixtures,
|
|
5
|
+
summarizeAudits,
|
|
6
|
+
worstAudits,
|
|
7
|
+
type LayoutAudit,
|
|
8
|
+
type LayoutMetrics,
|
|
9
|
+
} from "../src/test/layout-audit/harness.js"
|
|
10
|
+
|
|
11
|
+
const outputPath = resolve(import.meta.dir, "../../../tmp/merman-layout-audit.md")
|
|
12
|
+
const startedAt = performance.now()
|
|
13
|
+
const audits = auditAllFixtures()
|
|
14
|
+
const elapsedMs = performance.now() - startedAt
|
|
15
|
+
const summary = summarizeAudits(audits)
|
|
16
|
+
|
|
17
|
+
function label(audit: LayoutAudit): string {
|
|
18
|
+
return `${audit.fixture.id} @${audit.viewport}`
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function metricTable(items: readonly LayoutAudit[]): string {
|
|
22
|
+
return [
|
|
23
|
+
"| Fixture | Viewport | Size | Area | Route length | Bends | Crossings | Shared cells | Overflow |",
|
|
24
|
+
"| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
|
|
25
|
+
...items.map(
|
|
26
|
+
(audit) =>
|
|
27
|
+
`| \`${audit.fixture.id}\` | ${audit.viewport} | ${audit.metrics.width}x${audit.metrics.height} | ${audit.metrics.area} | ${audit.metrics.routeLength} | ${audit.metrics.bends} | ${audit.metrics.crossings} | ${audit.metrics.sharedRouteCells} | ${audit.metrics.overflow} |`,
|
|
28
|
+
),
|
|
29
|
+
].join("\n")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function worstSection(metric: keyof LayoutMetrics): string {
|
|
33
|
+
const worst = worstAudits(audits, metric)
|
|
34
|
+
return [`### ${metric}`, "", metricTable(worst)].join("\n")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function fixtureSection(audit: LayoutAudit): string {
|
|
38
|
+
return [
|
|
39
|
+
`<details${audit.fixture.curated || audit.violations.length > 0 ? " open" : ""}>`,
|
|
40
|
+
`<summary><code>${label(audit)}</code> · ${audit.metrics.width}x${audit.metrics.height} · area ${audit.metrics.area} · bends ${audit.metrics.bends} · crossings ${audit.metrics.crossings} · overflow ${audit.metrics.overflow}</summary>`,
|
|
41
|
+
"",
|
|
42
|
+
...(audit.violations.length > 0 ? ["Violations:", "", ...audit.violations.map((item) => `- ${item}`), ""] : []),
|
|
43
|
+
"Source:",
|
|
44
|
+
"",
|
|
45
|
+
"```mermaid",
|
|
46
|
+
audit.fixture.source,
|
|
47
|
+
"```",
|
|
48
|
+
"",
|
|
49
|
+
"Rendered output:",
|
|
50
|
+
"",
|
|
51
|
+
"```text",
|
|
52
|
+
audit.output,
|
|
53
|
+
"```",
|
|
54
|
+
"",
|
|
55
|
+
"</details>",
|
|
56
|
+
].join("\n")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const grouped = Map.groupBy(audits, (audit) => `${audit.fixture.kind}/${audit.fixture.family}`)
|
|
60
|
+
const violations = audits.flatMap((audit) => audit.violations.map((violation) => `${label(audit)}: ${violation}`))
|
|
61
|
+
const markdown = [
|
|
62
|
+
"# Merman Layout Audit",
|
|
63
|
+
"",
|
|
64
|
+
`Generated from ${new Set(audits.map((audit) => audit.fixture.id)).size} sources and ${audits.length} layout runs.`,
|
|
65
|
+
"",
|
|
66
|
+
`Structural violations: **${violations.length}**`,
|
|
67
|
+
"",
|
|
68
|
+
"## Aggregate Metrics",
|
|
69
|
+
"",
|
|
70
|
+
"```json",
|
|
71
|
+
JSON.stringify(summary, null, 2),
|
|
72
|
+
"```",
|
|
73
|
+
"",
|
|
74
|
+
"## Worst Offenders",
|
|
75
|
+
"",
|
|
76
|
+
...(["area", "bends", "crossings", "sharedRouteCells", "overflow"] as const).flatMap((metric) => [
|
|
77
|
+
worstSection(metric),
|
|
78
|
+
"",
|
|
79
|
+
]),
|
|
80
|
+
"## Fixtures",
|
|
81
|
+
"",
|
|
82
|
+
...[...grouped.entries()].flatMap(([family, items]) => [
|
|
83
|
+
`### ${family}`,
|
|
84
|
+
"",
|
|
85
|
+
metricTable(items),
|
|
86
|
+
"",
|
|
87
|
+
...items.flatMap((audit) => [fixtureSection(audit), ""]),
|
|
88
|
+
]),
|
|
89
|
+
].join("\n")
|
|
90
|
+
|
|
91
|
+
await mkdir(dirname(outputPath), { recursive: true })
|
|
92
|
+
await Bun.write(outputPath, markdown)
|
|
93
|
+
|
|
94
|
+
console.log(`Wrote ${audits.length} layout runs to ${outputPath} in ${elapsedMs.toFixed(0)}ms`)
|
|
95
|
+
if (violations.length > 0) {
|
|
96
|
+
console.error(violations.join("\n"))
|
|
97
|
+
process.exitCode = 1
|
|
98
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import stringWidth from "string-width"
|
|
2
|
+
import { diagramTextGraphemes } from "./text.js"
|
|
3
|
+
|
|
4
|
+
export type DiagramCanvasCell<Style extends string, Metadata extends object = object> = {
|
|
5
|
+
char: string
|
|
6
|
+
style?: Style
|
|
7
|
+
} & Partial<Metadata>
|
|
8
|
+
|
|
9
|
+
export interface DiagramCanvasRun<Style extends string, Metadata extends object = object> {
|
|
10
|
+
text: string
|
|
11
|
+
style: Style | undefined
|
|
12
|
+
cell: DiagramCanvasCell<Style, Metadata>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface DiagramCanvasOptions<Style extends string, Metadata extends object = object> {
|
|
16
|
+
measure?: (text: string) => number
|
|
17
|
+
mergeCell?: (
|
|
18
|
+
existing: DiagramCanvasCell<Style, Metadata>,
|
|
19
|
+
incoming: DiagramCanvasCell<Style, Metadata>,
|
|
20
|
+
) => DiagramCanvasCell<Style, Metadata>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface DiagramCanvasTextOptions {
|
|
24
|
+
trimTop?: boolean
|
|
25
|
+
trimBottom?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface DiagramCanvasTextSize {
|
|
29
|
+
width: number
|
|
30
|
+
height: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type DiagramCanvasTextMetadata<Metadata extends object> =
|
|
34
|
+
| Partial<Metadata>
|
|
35
|
+
| ((x: number, y: number) => Partial<Metadata>)
|
|
36
|
+
|
|
37
|
+
export interface DiagramCanvasRunOptions<Style extends string, Metadata extends object = object> {
|
|
38
|
+
key?: (cell: DiagramCanvasCell<Style, Metadata>) => readonly unknown[]
|
|
39
|
+
trimTop?: boolean
|
|
40
|
+
trimBottom?: boolean
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const MAX_DIAGRAM_CELLS = 250_000
|
|
44
|
+
|
|
45
|
+
export class DiagramCanvasSizeError extends Error {
|
|
46
|
+
constructor(
|
|
47
|
+
readonly width: number,
|
|
48
|
+
readonly height: number,
|
|
49
|
+
) {
|
|
50
|
+
const invalid = !Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 0 || height < 0
|
|
51
|
+
super(
|
|
52
|
+
invalid
|
|
53
|
+
? `Diagram canvas dimensions must be non-negative safe integers, received ${width}x${height}`
|
|
54
|
+
: `Diagram canvas ${width}x${height} exceeds the ${MAX_DIAGRAM_CELLS.toLocaleString()} cell limit`,
|
|
55
|
+
)
|
|
56
|
+
this.name = "DiagramCanvasSizeError"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function createEmptyCell<Style extends string, Metadata extends object>(): DiagramCanvasCell<Style, Metadata> {
|
|
61
|
+
return { char: " " } as DiagramCanvasCell<Style, Metadata>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function sameKey(left: readonly unknown[] | undefined, right: readonly unknown[]): boolean {
|
|
65
|
+
return Boolean(left && left.length === right.length && left.every((value, index) => Object.is(value, right[index])))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class DiagramCanvas<Style extends string, Metadata extends object = object> {
|
|
69
|
+
private readonly cells: Array<Array<DiagramCanvasCell<Style, Metadata>>>
|
|
70
|
+
private readonly measure: (text: string) => number
|
|
71
|
+
private readonly mergeCell?: DiagramCanvasOptions<Style, Metadata>["mergeCell"]
|
|
72
|
+
private readonly rowEnds: Uint32Array
|
|
73
|
+
|
|
74
|
+
constructor(
|
|
75
|
+
readonly width: number,
|
|
76
|
+
readonly height: number,
|
|
77
|
+
options: DiagramCanvasOptions<Style, Metadata> = {},
|
|
78
|
+
) {
|
|
79
|
+
if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 0 || height < 0) {
|
|
80
|
+
throw new DiagramCanvasSizeError(width, height)
|
|
81
|
+
}
|
|
82
|
+
if (width * height > MAX_DIAGRAM_CELLS) throw new DiagramCanvasSizeError(width, height)
|
|
83
|
+
this.measure = options.measure ?? stringWidth
|
|
84
|
+
this.mergeCell = options.mergeCell
|
|
85
|
+
this.cells = Array.from({ length: height }, () => Array.from({ length: width }, () => createEmptyCell()))
|
|
86
|
+
this.rowEnds = new Uint32Array(height)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get rows(): ReadonlyArray<ReadonlyArray<Readonly<DiagramCanvasCell<Style, Metadata>>>> {
|
|
90
|
+
return this.cells
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private rowText(row: Array<DiagramCanvasCell<Style, Metadata>>, rowEnd: number): string {
|
|
94
|
+
return row
|
|
95
|
+
.slice(0, rowEnd)
|
|
96
|
+
.map((cell) => cell.char)
|
|
97
|
+
.join("")
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private textRowRange(trimTop: boolean, trimBottom: boolean): { start: number; end: number } {
|
|
101
|
+
let start = 0
|
|
102
|
+
let end = this.cells.length
|
|
103
|
+
if (trimTop) while (start < end && this.rowEnds[start] === 0) start += 1
|
|
104
|
+
if (trimBottom) while (end > start && this.rowEnds[end - 1] === 0) end -= 1
|
|
105
|
+
return { start, end }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setCell(x: number, y: number, char: string, style?: Style, metadata?: Partial<Metadata>): void {
|
|
109
|
+
this.writeCell(x, y, char, style, metadata, true)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
replaceCell(x: number, y: number, char: string, style?: Style, metadata?: Partial<Metadata>): void {
|
|
113
|
+
this.writeCell(x, y, char, style, metadata, false)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private writeCell(
|
|
117
|
+
x: number,
|
|
118
|
+
y: number,
|
|
119
|
+
char: string,
|
|
120
|
+
style: Style | undefined,
|
|
121
|
+
metadata: Partial<Metadata> | undefined,
|
|
122
|
+
merge: boolean,
|
|
123
|
+
): void {
|
|
124
|
+
if (y < 0 || y >= this.cells.length || x < 0 || x >= this.cells[y]!.length) return
|
|
125
|
+
const incoming = { char, style, ...metadata } as DiagramCanvasCell<Style, Metadata>
|
|
126
|
+
const cell = merge ? (this.mergeCell?.(this.cells[y]![x]!, incoming) ?? incoming) : incoming
|
|
127
|
+
this.cells[y]![x] = cell
|
|
128
|
+
if (cell.char !== " ") {
|
|
129
|
+
this.rowEnds[y] = Math.max(this.rowEnds[y]!, x + 1)
|
|
130
|
+
} else if (this.rowEnds[y] === x + 1) {
|
|
131
|
+
let end = x
|
|
132
|
+
while (end > 0 && this.cells[y]![end - 1]?.char === " ") end -= 1
|
|
133
|
+
this.rowEnds[y] = end
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getCell(x: number, y: number): Readonly<DiagramCanvasCell<Style, Metadata>> | undefined {
|
|
138
|
+
return this.cells[y]?.[x]
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
setText(x: number, y: number, text: string, style?: Style, metadata?: DiagramCanvasTextMetadata<Metadata>): void {
|
|
142
|
+
const metadataAt = (cellX: number) => (typeof metadata === "function" ? metadata(cellX, y) : metadata)
|
|
143
|
+
if (this.measure === stringWidth && /^[\x20-\x7e]*$/.test(text)) {
|
|
144
|
+
for (let index = 0; index < text.length; index++) {
|
|
145
|
+
this.setCell(x + index, y, text[index]!, style, metadataAt(x + index))
|
|
146
|
+
}
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let offset = 0
|
|
151
|
+
for (const grapheme of diagramTextGraphemes(text)) {
|
|
152
|
+
const width = Math.max(1, this.measure(grapheme))
|
|
153
|
+
if (x + offset < 0 || x + offset + width > this.width) {
|
|
154
|
+
offset += width
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
this.setCell(x + offset, y, grapheme, style, metadataAt(x + offset))
|
|
158
|
+
for (let continuation = 1; continuation < width; continuation++) {
|
|
159
|
+
this.setCell(x + offset + continuation, y, "", style, metadataAt(x + offset + continuation))
|
|
160
|
+
}
|
|
161
|
+
offset += width
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
toString(options: DiagramCanvasTextOptions = {}): string {
|
|
166
|
+
const lines: string[] = []
|
|
167
|
+
const rows = this.textRowRange(options.trimTop ?? false, options.trimBottom ?? false)
|
|
168
|
+
for (let rowIndex = rows.start; rowIndex < rows.end; rowIndex++) {
|
|
169
|
+
lines.push(this.rowText(this.cells[rowIndex]!, this.rowEnds[rowIndex]!))
|
|
170
|
+
}
|
|
171
|
+
return lines.join("\n")
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
getTextSize(options: DiagramCanvasTextOptions = {}): DiagramCanvasTextSize {
|
|
175
|
+
const rows = this.textRowRange(options.trimTop ?? false, options.trimBottom ?? false)
|
|
176
|
+
let width = 0
|
|
177
|
+
for (let rowIndex = rows.start; rowIndex < rows.end; rowIndex++) {
|
|
178
|
+
const row = this.cells[rowIndex]!
|
|
179
|
+
const rowEnd = this.rowEnds[rowIndex]!
|
|
180
|
+
if (rowEnd > 0) width = Math.max(width, this.measure(this.rowText(row, rowEnd)))
|
|
181
|
+
}
|
|
182
|
+
return { width, height: rows.end - rows.start }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
getTextHeight(options: DiagramCanvasTextOptions = {}): number {
|
|
186
|
+
const rows = this.textRowRange(options.trimTop ?? false, options.trimBottom ?? false)
|
|
187
|
+
return rows.end - rows.start
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
forEachRun(
|
|
191
|
+
onRun: (run: DiagramCanvasRun<Style, Metadata>) => void,
|
|
192
|
+
onLineEnd: () => void,
|
|
193
|
+
options: DiagramCanvasRunOptions<Style, Metadata> = {},
|
|
194
|
+
): void {
|
|
195
|
+
const key = options.key
|
|
196
|
+
const rows = this.textRowRange(options.trimTop ?? false, options.trimBottom ?? false)
|
|
197
|
+
|
|
198
|
+
for (let rowIndex = rows.start; rowIndex < rows.end; rowIndex++) {
|
|
199
|
+
const row = this.cells[rowIndex]!
|
|
200
|
+
let rowEnd = this.rowEnds[rowIndex]!
|
|
201
|
+
while (rowEnd < row.length && row[rowEnd]?.style !== undefined) rowEnd += 1
|
|
202
|
+
|
|
203
|
+
let currentCell: DiagramCanvasCell<Style, Metadata> | undefined
|
|
204
|
+
let currentKey: readonly unknown[] | undefined
|
|
205
|
+
let currentText = ""
|
|
206
|
+
const flush = () => {
|
|
207
|
+
if (!currentText || !currentCell) return
|
|
208
|
+
onRun({ text: currentText, style: currentCell.style, cell: currentCell })
|
|
209
|
+
currentText = ""
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
for (let x = 0; x < rowEnd; x++) {
|
|
213
|
+
const cell = row[x]!
|
|
214
|
+
const nextKey = key?.(cell)
|
|
215
|
+
const sameRun = currentCell && (key ? sameKey(currentKey, nextKey!) : currentCell.style === cell.style)
|
|
216
|
+
if (!sameRun) {
|
|
217
|
+
flush()
|
|
218
|
+
currentCell = cell
|
|
219
|
+
currentKey = nextKey
|
|
220
|
+
}
|
|
221
|
+
currentText += cell.char
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
flush()
|
|
225
|
+
if (rowIndex < rows.end - 1) onLineEnd()
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { RGBA } from "@opentui/core"
|
|
2
|
+
|
|
3
|
+
export type DiagramRgb = readonly [number, number, number]
|
|
4
|
+
export type DiagramFadeStep = 1 | 2 | 3 | 4 | 5
|
|
5
|
+
|
|
6
|
+
export const DIAGRAM_FADE_STEPS = [1, 2, 3, 4, 5] as const satisfies readonly DiagramFadeStep[]
|
|
7
|
+
|
|
8
|
+
export function numberedStyleKeys<Prefix extends string, Step extends number>(
|
|
9
|
+
prefix: Prefix,
|
|
10
|
+
steps: readonly Step[],
|
|
11
|
+
): Array<`${Prefix}${Step}`> {
|
|
12
|
+
return steps.map((step) => `${prefix}${step}` as `${Prefix}${Step}`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function rgba(rgb: DiagramRgb): RGBA {
|
|
16
|
+
return RGBA.fromInts(rgb[0], rgb[1], rgb[2], 255)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function blendColor(from: RGBA, to: RGBA, amount: number): RGBA
|
|
20
|
+
export function blendColor(from: RGBA | undefined, to: RGBA | undefined, amount: number): RGBA | undefined
|
|
21
|
+
export function blendColor(from: RGBA | undefined, to: RGBA | undefined, amount: number): RGBA | undefined {
|
|
22
|
+
if (!from && !to) return undefined
|
|
23
|
+
if (!from) return to
|
|
24
|
+
if (!to) return from
|
|
25
|
+
|
|
26
|
+
const [fromR, fromG, fromB, fromA] = from.toInts()
|
|
27
|
+
const [toR, toG, toB, toA] = to.toInts()
|
|
28
|
+
const mix = (left: number, right: number) => left + (right - left) * amount
|
|
29
|
+
|
|
30
|
+
return RGBA.fromInts(mix(fromR, toR), mix(fromG, toG), mix(fromB, toB), mix(fromA, toA))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function colorsEqual(left?: RGBA, right?: RGBA): boolean {
|
|
34
|
+
if (!left || !right) return left === right
|
|
35
|
+
return left.equals(right)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createColorRampTheme<Style extends string>(
|
|
39
|
+
styles: readonly Style[],
|
|
40
|
+
from: RGBA,
|
|
41
|
+
to: RGBA,
|
|
42
|
+
): Record<Style, RGBA>
|
|
43
|
+
export function createColorRampTheme<Style extends string>(
|
|
44
|
+
styles: readonly Style[],
|
|
45
|
+
from: RGBA | undefined,
|
|
46
|
+
to: RGBA | undefined,
|
|
47
|
+
): Record<Style, RGBA | undefined>
|
|
48
|
+
export function createColorRampTheme<Style extends string>(
|
|
49
|
+
styles: readonly Style[],
|
|
50
|
+
from: RGBA | undefined,
|
|
51
|
+
to: RGBA | undefined,
|
|
52
|
+
): Record<Style, RGBA | undefined> {
|
|
53
|
+
return Object.fromEntries(
|
|
54
|
+
styles.map((style, index) => [style, blendColor(from, to, (index + 1) / (styles.length + 1))]),
|
|
55
|
+
) as Record<Style, RGBA | undefined>
|
|
56
|
+
}
|