@zyro-sec/merman 1.0.2 → 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.
Files changed (91) hide show
  1. package/package.json +2 -2
  2. package/script/layout-audit.ts +98 -98
  3. package/src/core/canvas.ts +228 -228
  4. package/src/core/color/style.ts +56 -56
  5. package/src/core/drawing.ts +301 -301
  6. package/src/core/geometry.ts +347 -347
  7. package/src/core/mermaid.ts +38 -38
  8. package/src/core/render-grid.ts +51 -51
  9. package/src/core/spatial.ts +243 -243
  10. package/src/core/text-lines.ts +32 -32
  11. package/src/core/text.ts +44 -44
  12. package/src/detect.ts +17 -17
  13. package/src/diagnostics.ts +16 -16
  14. package/src/flowchart/drawing.ts +340 -340
  15. package/src/flowchart/labels.ts +144 -144
  16. package/src/flowchart/layout.ts +1427 -1427
  17. package/src/flowchart/options.ts +12 -12
  18. package/src/flowchart/parser.ts +467 -467
  19. package/src/flowchart/render.ts +10 -10
  20. package/src/flowchart/routing.ts +1542 -1542
  21. package/src/flowchart/style.ts +79 -79
  22. package/src/flowchart/types.ts +65 -65
  23. package/src/gantt/diagram.ts +8 -8
  24. package/src/gantt/drawing.ts +191 -191
  25. package/src/gantt/parser.ts +161 -161
  26. package/src/gantt/render-grid.ts +17 -17
  27. package/src/gantt/style.ts +36 -36
  28. package/src/gantt/types.ts +44 -44
  29. package/src/gitgraph/diagram.ts +8 -8
  30. package/src/gitgraph/drawing.ts +234 -234
  31. package/src/gitgraph/parser.ts +219 -219
  32. package/src/gitgraph/render-grid.ts +17 -17
  33. package/src/gitgraph/style.ts +37 -37
  34. package/src/gitgraph/types.ts +36 -36
  35. package/src/markdown.ts +336 -336
  36. package/src/palette.ts +75 -75
  37. package/src/plugin.ts +15 -15
  38. package/src/sequence/diagram.ts +8 -8
  39. package/src/sequence/drawing.ts +287 -287
  40. package/src/sequence/options.ts +8 -8
  41. package/src/sequence/parser.ts +282 -282
  42. package/src/sequence/placement.ts +684 -684
  43. package/src/sequence/render-grid.ts +22 -22
  44. package/src/sequence/style.ts +81 -81
  45. package/src/sequence/types.ts +74 -74
  46. package/src/state/diagram.ts +8 -8
  47. package/src/state/drawing.ts +360 -360
  48. package/src/state/endpoint.ts +15 -15
  49. package/src/state/layout.ts +1134 -1134
  50. package/src/state/note.ts +59 -59
  51. package/src/state/options.ts +11 -11
  52. package/src/state/parser.ts +224 -224
  53. package/src/state/render-grid.ts +22 -22
  54. package/src/state/routing.ts +1522 -1522
  55. package/src/state/search.ts +103 -103
  56. package/src/state/style.ts +52 -52
  57. package/src/state/types.ts +63 -63
  58. package/src/state/visible-model.ts +77 -77
  59. package/src/test/diagram.ts +32 -32
  60. package/src/test/layout-audit/fixtures.ts +538 -538
  61. package/src/test/layout-audit/harness.ts +522 -522
  62. package/src/timeline/diagram.ts +8 -8
  63. package/src/timeline/drawing.ts +109 -109
  64. package/src/timeline/parser.ts +119 -119
  65. package/src/timeline/render-grid.ts +17 -17
  66. package/src/timeline/style.ts +36 -36
  67. package/src/timeline/types.ts +31 -31
  68. package/tsconfig.json +10 -10
  69. package/src/core/canvas.test.ts +0 -203
  70. package/src/core/color/style.test.ts +0 -24
  71. package/src/core/drawing.test.ts +0 -116
  72. package/src/core/geometry.test.ts +0 -112
  73. package/src/core/spatial.test.ts +0 -87
  74. package/src/core/text.test.ts +0 -33
  75. package/src/flowchart/flowchart.test.ts +0 -2800
  76. package/src/flowchart/labels.test.ts +0 -127
  77. package/src/flowchart/routing.test.ts +0 -368
  78. package/src/gantt/diagram.test.ts +0 -124
  79. package/src/gitgraph/diagram.test.ts +0 -183
  80. package/src/layout-audit.test.ts +0 -92
  81. package/src/palette.test.ts +0 -103
  82. package/src/sequence/diagram.test.ts +0 -757
  83. package/src/sequence/placement.test.ts +0 -219
  84. package/src/state/diagram.test.ts +0 -1386
  85. package/src/state/layout.test.ts +0 -314
  86. package/src/state/routing.test.ts +0 -693
  87. package/src/state/style.test.ts +0 -113
  88. package/src/state/visible-model.test.ts +0 -84
  89. package/src/test/diagnostics.test.ts +0 -129
  90. package/src/test/markdown.test.ts +0 -474
  91. package/src/timeline/diagram.test.ts +0 -188
@@ -1,228 +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
- }
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
+ }
@@ -1,56 +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
- }
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
+ }