@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,301 +1,301 @@
1
- import { BorderChars, type BorderCharacters } from "@opentui/core"
2
- import {
3
- directionBetween,
4
- walkOrthogonalSegment,
5
- type DiagramBounds,
6
- type DiagramDirection,
7
- type DiagramPoint,
8
- } from "./geometry.js"
9
-
10
- export type DiagramLineCornerStyle = "square" | "rounded"
11
- export type DiagramLineStyle = "single" | "heavy" | "dashed"
12
- export type DiagramArrowHeadStyle = "filled" | "line"
13
-
14
- export interface DiagramDiamondCharacters {
15
- topLeft: string
16
- topRight: string
17
- upperLeft: string
18
- upperLeftJoin: string
19
- upperRightJoin: string
20
- upperRight: string
21
- vertical: string
22
- lowerLeft: string
23
- lowerLeftJoin: string
24
- lowerRightJoin: string
25
- lowerRight: string
26
- bottomLeft: string
27
- bottomRight: string
28
- horizontal: string
29
- }
30
-
31
- export const DIAGRAM_ARROW_HEADS = new Set(["▶", "◀", "▼", "▲", "→", "←", "↓", "↑"])
32
- const HEAVY_LINE_GLYPHS = new Set(Object.values(BorderChars.heavy))
33
-
34
- export const DIAGRAM_DIAMOND_CHARS = {
35
- topLeft: "╭",
36
- topRight: "╮",
37
- upperLeft: "╭",
38
- upperLeftJoin: "╯",
39
- upperRightJoin: "╰",
40
- upperRight: "╮",
41
- vertical: "│",
42
- lowerLeft: "╰",
43
- lowerLeftJoin: "╮",
44
- lowerRightJoin: "╭",
45
- lowerRight: "╯",
46
- bottomLeft: "╰",
47
- bottomRight: "╯",
48
- horizontal: "─",
49
- } as const satisfies DiagramDiamondCharacters
50
-
51
- export function diagramDiamondCharactersFromBorder(chars: BorderCharacters): DiagramDiamondCharacters {
52
- return {
53
- topLeft: chars.topLeft,
54
- topRight: chars.topRight,
55
- upperLeft: chars.topLeft,
56
- upperLeftJoin: chars.bottomRight,
57
- upperRightJoin: chars.bottomLeft,
58
- upperRight: chars.topRight,
59
- vertical: chars.vertical,
60
- lowerLeft: chars.bottomLeft,
61
- lowerLeftJoin: chars.topRight,
62
- lowerRightJoin: chars.topLeft,
63
- lowerRight: chars.bottomRight,
64
- bottomLeft: chars.bottomLeft,
65
- bottomRight: chars.bottomRight,
66
- horizontal: chars.horizontal,
67
- }
68
- }
69
-
70
- function lineDirections(char: string): readonly DiagramDirection[] | undefined {
71
- switch (char) {
72
- case "─":
73
- return ["left", "right"]
74
- case "│":
75
- return ["up", "down"]
76
- case "━":
77
- return ["left", "right"]
78
- case "┃":
79
- return ["up", "down"]
80
- case "┌":
81
- case "╭":
82
- case "┏":
83
- return ["right", "down"]
84
- case "┐":
85
- case "╮":
86
- case "┓":
87
- return ["left", "down"]
88
- case "└":
89
- case "╰":
90
- case "┗":
91
- return ["up", "right"]
92
- case "┘":
93
- case "╯":
94
- case "┛":
95
- return ["up", "left"]
96
- case "├":
97
- case "┣":
98
- return ["up", "down", "right"]
99
- case "┤":
100
- case "┫":
101
- return ["up", "down", "left"]
102
- case "┬":
103
- case "┳":
104
- return ["left", "right", "down"]
105
- case "┴":
106
- case "┻":
107
- return ["left", "right", "up"]
108
- case "┼":
109
- case "╋":
110
- return ["up", "down", "left", "right"]
111
- default:
112
- return undefined
113
- }
114
- }
115
-
116
- export function diagramLineGlyph(
117
- directions: ReadonlySet<DiagramDirection>,
118
- cornerStyle: DiagramLineCornerStyle = "square",
119
- lineStyle: DiagramLineStyle = "single",
120
- ): string {
121
- const up = directions.has("up")
122
- const down = directions.has("down")
123
- const left = directions.has("left")
124
- const right = directions.has("right")
125
- if (lineStyle === "heavy") {
126
- const chars = BorderChars.heavy
127
- if (up && down && left && right) return chars.cross
128
- if (up && down && right) return chars.leftT
129
- if (up && down && left) return chars.rightT
130
- if (left && right && down) return chars.topT
131
- if (left && right && up) return chars.bottomT
132
- if (up && right) return chars.bottomLeft
133
- if (up && left) return chars.bottomRight
134
- if (down && right) return chars.topLeft
135
- if (down && left) return chars.topRight
136
- if (up || down) return chars.vertical
137
- return chars.horizontal
138
- }
139
- if (up && down && left && right) return "┼"
140
- if (up && down && right) return "├"
141
- if (up && down && left) return "┤"
142
- if (left && right && down) return "┬"
143
- if (left && right && up) return "┴"
144
- if (up && right) return cornerStyle === "rounded" ? "╰" : "└"
145
- if (up && left) return cornerStyle === "rounded" ? "╯" : "┘"
146
- if (down && right) return cornerStyle === "rounded" ? "╭" : "┌"
147
- if (down && left) return cornerStyle === "rounded" ? "╮" : "┐"
148
- if (up || down) return "│"
149
- return "─"
150
- }
151
-
152
- function isHeavyLineGlyph(char: string): boolean {
153
- return HEAVY_LINE_GLYPHS.has(char)
154
- }
155
-
156
- function segmentGlyph(direction: DiagramDirection, lineStyle: DiagramLineStyle | undefined): string {
157
- const directions = new Set<DiagramDirection>(
158
- direction === "left" || direction === "right" ? ["left", "right"] : ["up", "down"],
159
- )
160
- return diagramLineGlyph(directions, "square", lineStyle === "heavy" ? "heavy" : "single")
161
- }
162
-
163
- export function mergeDiagramLineGlyph(
164
- existing: string,
165
- incoming: string,
166
- cornerStyle: DiagramLineCornerStyle = "square",
167
- ): string | undefined {
168
- const existingDirections = lineDirections(existing)
169
- const incomingDirections = lineDirections(incoming)
170
- if (!existingDirections || !incomingDirections) return undefined
171
-
172
- return diagramLineGlyph(
173
- new Set([...existingDirections, ...incomingDirections]),
174
- cornerStyle,
175
- isHeavyLineGlyph(existing) && isHeavyLineGlyph(incoming) ? "heavy" : "single",
176
- )
177
- }
178
-
179
- export function diagramArrowHead(direction: DiagramDirection, style: DiagramArrowHeadStyle = "filled"): string {
180
- if (style === "line") {
181
- if (direction === "right") return "→"
182
- if (direction === "left") return "←"
183
- if (direction === "up") return "↑"
184
- return "↓"
185
- }
186
-
187
- if (direction === "right") return "▶"
188
- if (direction === "left") return "◀"
189
- if (direction === "up") return "▲"
190
- return "▼"
191
- }
192
-
193
- export function diagramArrowHeadBetween(
194
- from: DiagramPoint,
195
- to: DiagramPoint,
196
- style: DiagramArrowHeadStyle = "filled",
197
- ): string {
198
- const direction = directionBetween(from, to)
199
- return direction ? diagramArrowHead(direction, style) : diagramArrowHead("right", style)
200
- }
201
-
202
- export function drawDiagramFrame(
203
- bounds: DiagramBounds,
204
- chars: BorderCharacters,
205
- setCell: (x: number, y: number, char: string) => void,
206
- ): void {
207
- setCell(bounds.left, bounds.top, chars.topLeft)
208
- setCell(bounds.left + bounds.width - 1, bounds.top, chars.topRight)
209
- setCell(bounds.left, bounds.top + bounds.height - 1, chars.bottomLeft)
210
- setCell(bounds.left + bounds.width - 1, bounds.top + bounds.height - 1, chars.bottomRight)
211
- for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) {
212
- setCell(x, bounds.top, chars.horizontal)
213
- setCell(x, bounds.top + bounds.height - 1, chars.horizontal)
214
- }
215
- for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
216
- setCell(bounds.left, y, chars.vertical)
217
- setCell(bounds.left + bounds.width - 1, y, chars.vertical)
218
- }
219
- }
220
-
221
- export function fillDiagramFrameInterior(bounds: DiagramBounds, setCell: (x: number, y: number) => void): void {
222
- for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
223
- for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) setCell(x, y)
224
- }
225
- }
226
-
227
- export function drawDiagramDiamond(
228
- bounds: DiagramBounds,
229
- setCell: (x: number, y: number, char: string) => void,
230
- chars: DiagramDiamondCharacters = DIAGRAM_DIAMOND_CHARS,
231
- ): void {
232
- const left = bounds.left
233
- const right = bounds.left + bounds.width - 1
234
- const top = bounds.top
235
- const bottom = bounds.top + bounds.height - 1
236
- const capInset = Math.min(2, Math.max(1, Math.floor((bounds.width - 1) / 2)))
237
- const capLeft = left + capInset
238
- const capRight = right - capInset
239
-
240
- setCell(capLeft, top, chars.topLeft)
241
- for (let x = capLeft + 1; x < capRight; x++) setCell(x, top, chars.horizontal)
242
- setCell(capRight, top, chars.topRight)
243
-
244
- setCell(left, top + 1, chars.upperLeft)
245
- for (let x = left + 1; x < capLeft; x++) setCell(x, top + 1, chars.horizontal)
246
- setCell(capLeft, top + 1, chars.upperLeftJoin)
247
- setCell(capRight, top + 1, chars.upperRightJoin)
248
- for (let x = capRight + 1; x < right; x++) setCell(x, top + 1, chars.horizontal)
249
- setCell(right, top + 1, chars.upperRight)
250
-
251
- for (let y = top + 2; y < bottom - 1; y++) {
252
- setCell(left, y, chars.vertical)
253
- setCell(right, y, chars.vertical)
254
- }
255
-
256
- setCell(left, bottom - 1, chars.lowerLeft)
257
- for (let x = left + 1; x < capLeft; x++) setCell(x, bottom - 1, chars.horizontal)
258
- setCell(capLeft, bottom - 1, chars.lowerLeftJoin)
259
- setCell(capRight, bottom - 1, chars.lowerRightJoin)
260
- for (let x = capRight + 1; x < right; x++) setCell(x, bottom - 1, chars.horizontal)
261
- setCell(right, bottom - 1, chars.lowerRight)
262
-
263
- setCell(capLeft, bottom, chars.bottomLeft)
264
- for (let x = capLeft + 1; x < capRight; x++) setCell(x, bottom, chars.horizontal)
265
- setCell(capRight, bottom, chars.bottomRight)
266
- }
267
-
268
- export function drawOrthogonalPath(
269
- points: readonly DiagramPoint[],
270
- setCell: (x: number, y: number, char: string) => void,
271
- options: { cornerStyle?: DiagramLineCornerStyle; lineStyle?: DiagramLineStyle } = {},
272
- ): void {
273
- for (let index = 1; index < points.length; index++) {
274
- const from = points[index - 1]!
275
- const to = points[index]!
276
- const direction = directionBetween(from, to)
277
- if (!direction) continue
278
- const glyph = segmentGlyph(direction, options.lineStyle)
279
- let step = index === 1 ? 0 : 1
280
- walkOrthogonalSegment(from, to, index === 1, (point) => {
281
- if (options.lineStyle !== "dashed" || step % 2 === 0) setCell(point.x, point.y, glyph)
282
- step += 1
283
- })
284
- }
285
-
286
- for (let index = 1; index < points.length - 1; index++) {
287
- const previous = points[index - 1]!
288
- const current = points[index]!
289
- const next = points[index + 1]!
290
- const fromDirection = directionBetween(current, previous)
291
- const toDirection = directionBetween(current, next)
292
- const directions = new Set<DiagramDirection>()
293
- if (fromDirection) directions.add(fromDirection)
294
- if (toDirection) directions.add(toDirection)
295
- setCell(
296
- current.x,
297
- current.y,
298
- diagramLineGlyph(directions, options.cornerStyle, options.lineStyle === "heavy" ? "heavy" : "single"),
299
- )
300
- }
301
- }
1
+ import { BorderChars, type BorderCharacters } from "@opentui/core"
2
+ import {
3
+ directionBetween,
4
+ walkOrthogonalSegment,
5
+ type DiagramBounds,
6
+ type DiagramDirection,
7
+ type DiagramPoint,
8
+ } from "./geometry.js"
9
+
10
+ export type DiagramLineCornerStyle = "square" | "rounded"
11
+ export type DiagramLineStyle = "single" | "heavy" | "dashed"
12
+ export type DiagramArrowHeadStyle = "filled" | "line"
13
+
14
+ export interface DiagramDiamondCharacters {
15
+ topLeft: string
16
+ topRight: string
17
+ upperLeft: string
18
+ upperLeftJoin: string
19
+ upperRightJoin: string
20
+ upperRight: string
21
+ vertical: string
22
+ lowerLeft: string
23
+ lowerLeftJoin: string
24
+ lowerRightJoin: string
25
+ lowerRight: string
26
+ bottomLeft: string
27
+ bottomRight: string
28
+ horizontal: string
29
+ }
30
+
31
+ export const DIAGRAM_ARROW_HEADS = new Set(["▶", "◀", "▼", "▲", "→", "←", "↓", "↑"])
32
+ const HEAVY_LINE_GLYPHS = new Set(Object.values(BorderChars.heavy))
33
+
34
+ export const DIAGRAM_DIAMOND_CHARS = {
35
+ topLeft: "╭",
36
+ topRight: "╮",
37
+ upperLeft: "╭",
38
+ upperLeftJoin: "╯",
39
+ upperRightJoin: "╰",
40
+ upperRight: "╮",
41
+ vertical: "│",
42
+ lowerLeft: "╰",
43
+ lowerLeftJoin: "╮",
44
+ lowerRightJoin: "╭",
45
+ lowerRight: "╯",
46
+ bottomLeft: "╰",
47
+ bottomRight: "╯",
48
+ horizontal: "─",
49
+ } as const satisfies DiagramDiamondCharacters
50
+
51
+ export function diagramDiamondCharactersFromBorder(chars: BorderCharacters): DiagramDiamondCharacters {
52
+ return {
53
+ topLeft: chars.topLeft,
54
+ topRight: chars.topRight,
55
+ upperLeft: chars.topLeft,
56
+ upperLeftJoin: chars.bottomRight,
57
+ upperRightJoin: chars.bottomLeft,
58
+ upperRight: chars.topRight,
59
+ vertical: chars.vertical,
60
+ lowerLeft: chars.bottomLeft,
61
+ lowerLeftJoin: chars.topRight,
62
+ lowerRightJoin: chars.topLeft,
63
+ lowerRight: chars.bottomRight,
64
+ bottomLeft: chars.bottomLeft,
65
+ bottomRight: chars.bottomRight,
66
+ horizontal: chars.horizontal,
67
+ }
68
+ }
69
+
70
+ function lineDirections(char: string): readonly DiagramDirection[] | undefined {
71
+ switch (char) {
72
+ case "─":
73
+ return ["left", "right"]
74
+ case "│":
75
+ return ["up", "down"]
76
+ case "━":
77
+ return ["left", "right"]
78
+ case "┃":
79
+ return ["up", "down"]
80
+ case "┌":
81
+ case "╭":
82
+ case "┏":
83
+ return ["right", "down"]
84
+ case "┐":
85
+ case "╮":
86
+ case "┓":
87
+ return ["left", "down"]
88
+ case "└":
89
+ case "╰":
90
+ case "┗":
91
+ return ["up", "right"]
92
+ case "┘":
93
+ case "╯":
94
+ case "┛":
95
+ return ["up", "left"]
96
+ case "├":
97
+ case "┣":
98
+ return ["up", "down", "right"]
99
+ case "┤":
100
+ case "┫":
101
+ return ["up", "down", "left"]
102
+ case "┬":
103
+ case "┳":
104
+ return ["left", "right", "down"]
105
+ case "┴":
106
+ case "┻":
107
+ return ["left", "right", "up"]
108
+ case "┼":
109
+ case "╋":
110
+ return ["up", "down", "left", "right"]
111
+ default:
112
+ return undefined
113
+ }
114
+ }
115
+
116
+ export function diagramLineGlyph(
117
+ directions: ReadonlySet<DiagramDirection>,
118
+ cornerStyle: DiagramLineCornerStyle = "square",
119
+ lineStyle: DiagramLineStyle = "single",
120
+ ): string {
121
+ const up = directions.has("up")
122
+ const down = directions.has("down")
123
+ const left = directions.has("left")
124
+ const right = directions.has("right")
125
+ if (lineStyle === "heavy") {
126
+ const chars = BorderChars.heavy
127
+ if (up && down && left && right) return chars.cross
128
+ if (up && down && right) return chars.leftT
129
+ if (up && down && left) return chars.rightT
130
+ if (left && right && down) return chars.topT
131
+ if (left && right && up) return chars.bottomT
132
+ if (up && right) return chars.bottomLeft
133
+ if (up && left) return chars.bottomRight
134
+ if (down && right) return chars.topLeft
135
+ if (down && left) return chars.topRight
136
+ if (up || down) return chars.vertical
137
+ return chars.horizontal
138
+ }
139
+ if (up && down && left && right) return "┼"
140
+ if (up && down && right) return "├"
141
+ if (up && down && left) return "┤"
142
+ if (left && right && down) return "┬"
143
+ if (left && right && up) return "┴"
144
+ if (up && right) return cornerStyle === "rounded" ? "╰" : "└"
145
+ if (up && left) return cornerStyle === "rounded" ? "╯" : "┘"
146
+ if (down && right) return cornerStyle === "rounded" ? "╭" : "┌"
147
+ if (down && left) return cornerStyle === "rounded" ? "╮" : "┐"
148
+ if (up || down) return "│"
149
+ return "─"
150
+ }
151
+
152
+ function isHeavyLineGlyph(char: string): boolean {
153
+ return HEAVY_LINE_GLYPHS.has(char)
154
+ }
155
+
156
+ function segmentGlyph(direction: DiagramDirection, lineStyle: DiagramLineStyle | undefined): string {
157
+ const directions = new Set<DiagramDirection>(
158
+ direction === "left" || direction === "right" ? ["left", "right"] : ["up", "down"],
159
+ )
160
+ return diagramLineGlyph(directions, "square", lineStyle === "heavy" ? "heavy" : "single")
161
+ }
162
+
163
+ export function mergeDiagramLineGlyph(
164
+ existing: string,
165
+ incoming: string,
166
+ cornerStyle: DiagramLineCornerStyle = "square",
167
+ ): string | undefined {
168
+ const existingDirections = lineDirections(existing)
169
+ const incomingDirections = lineDirections(incoming)
170
+ if (!existingDirections || !incomingDirections) return undefined
171
+
172
+ return diagramLineGlyph(
173
+ new Set([...existingDirections, ...incomingDirections]),
174
+ cornerStyle,
175
+ isHeavyLineGlyph(existing) && isHeavyLineGlyph(incoming) ? "heavy" : "single",
176
+ )
177
+ }
178
+
179
+ export function diagramArrowHead(direction: DiagramDirection, style: DiagramArrowHeadStyle = "filled"): string {
180
+ if (style === "line") {
181
+ if (direction === "right") return "→"
182
+ if (direction === "left") return "←"
183
+ if (direction === "up") return "↑"
184
+ return "↓"
185
+ }
186
+
187
+ if (direction === "right") return "▶"
188
+ if (direction === "left") return "◀"
189
+ if (direction === "up") return "▲"
190
+ return "▼"
191
+ }
192
+
193
+ export function diagramArrowHeadBetween(
194
+ from: DiagramPoint,
195
+ to: DiagramPoint,
196
+ style: DiagramArrowHeadStyle = "filled",
197
+ ): string {
198
+ const direction = directionBetween(from, to)
199
+ return direction ? diagramArrowHead(direction, style) : diagramArrowHead("right", style)
200
+ }
201
+
202
+ export function drawDiagramFrame(
203
+ bounds: DiagramBounds,
204
+ chars: BorderCharacters,
205
+ setCell: (x: number, y: number, char: string) => void,
206
+ ): void {
207
+ setCell(bounds.left, bounds.top, chars.topLeft)
208
+ setCell(bounds.left + bounds.width - 1, bounds.top, chars.topRight)
209
+ setCell(bounds.left, bounds.top + bounds.height - 1, chars.bottomLeft)
210
+ setCell(bounds.left + bounds.width - 1, bounds.top + bounds.height - 1, chars.bottomRight)
211
+ for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) {
212
+ setCell(x, bounds.top, chars.horizontal)
213
+ setCell(x, bounds.top + bounds.height - 1, chars.horizontal)
214
+ }
215
+ for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
216
+ setCell(bounds.left, y, chars.vertical)
217
+ setCell(bounds.left + bounds.width - 1, y, chars.vertical)
218
+ }
219
+ }
220
+
221
+ export function fillDiagramFrameInterior(bounds: DiagramBounds, setCell: (x: number, y: number) => void): void {
222
+ for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
223
+ for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) setCell(x, y)
224
+ }
225
+ }
226
+
227
+ export function drawDiagramDiamond(
228
+ bounds: DiagramBounds,
229
+ setCell: (x: number, y: number, char: string) => void,
230
+ chars: DiagramDiamondCharacters = DIAGRAM_DIAMOND_CHARS,
231
+ ): void {
232
+ const left = bounds.left
233
+ const right = bounds.left + bounds.width - 1
234
+ const top = bounds.top
235
+ const bottom = bounds.top + bounds.height - 1
236
+ const capInset = Math.min(2, Math.max(1, Math.floor((bounds.width - 1) / 2)))
237
+ const capLeft = left + capInset
238
+ const capRight = right - capInset
239
+
240
+ setCell(capLeft, top, chars.topLeft)
241
+ for (let x = capLeft + 1; x < capRight; x++) setCell(x, top, chars.horizontal)
242
+ setCell(capRight, top, chars.topRight)
243
+
244
+ setCell(left, top + 1, chars.upperLeft)
245
+ for (let x = left + 1; x < capLeft; x++) setCell(x, top + 1, chars.horizontal)
246
+ setCell(capLeft, top + 1, chars.upperLeftJoin)
247
+ setCell(capRight, top + 1, chars.upperRightJoin)
248
+ for (let x = capRight + 1; x < right; x++) setCell(x, top + 1, chars.horizontal)
249
+ setCell(right, top + 1, chars.upperRight)
250
+
251
+ for (let y = top + 2; y < bottom - 1; y++) {
252
+ setCell(left, y, chars.vertical)
253
+ setCell(right, y, chars.vertical)
254
+ }
255
+
256
+ setCell(left, bottom - 1, chars.lowerLeft)
257
+ for (let x = left + 1; x < capLeft; x++) setCell(x, bottom - 1, chars.horizontal)
258
+ setCell(capLeft, bottom - 1, chars.lowerLeftJoin)
259
+ setCell(capRight, bottom - 1, chars.lowerRightJoin)
260
+ for (let x = capRight + 1; x < right; x++) setCell(x, bottom - 1, chars.horizontal)
261
+ setCell(right, bottom - 1, chars.lowerRight)
262
+
263
+ setCell(capLeft, bottom, chars.bottomLeft)
264
+ for (let x = capLeft + 1; x < capRight; x++) setCell(x, bottom, chars.horizontal)
265
+ setCell(capRight, bottom, chars.bottomRight)
266
+ }
267
+
268
+ export function drawOrthogonalPath(
269
+ points: readonly DiagramPoint[],
270
+ setCell: (x: number, y: number, char: string) => void,
271
+ options: { cornerStyle?: DiagramLineCornerStyle; lineStyle?: DiagramLineStyle } = {},
272
+ ): void {
273
+ for (let index = 1; index < points.length; index++) {
274
+ const from = points[index - 1]!
275
+ const to = points[index]!
276
+ const direction = directionBetween(from, to)
277
+ if (!direction) continue
278
+ const glyph = segmentGlyph(direction, options.lineStyle)
279
+ let step = index === 1 ? 0 : 1
280
+ walkOrthogonalSegment(from, to, index === 1, (point) => {
281
+ if (options.lineStyle !== "dashed" || step % 2 === 0) setCell(point.x, point.y, glyph)
282
+ step += 1
283
+ })
284
+ }
285
+
286
+ for (let index = 1; index < points.length - 1; index++) {
287
+ const previous = points[index - 1]!
288
+ const current = points[index]!
289
+ const next = points[index + 1]!
290
+ const fromDirection = directionBetween(current, previous)
291
+ const toDirection = directionBetween(current, next)
292
+ const directions = new Set<DiagramDirection>()
293
+ if (fromDirection) directions.add(fromDirection)
294
+ if (toDirection) directions.add(toDirection)
295
+ setCell(
296
+ current.x,
297
+ current.y,
298
+ diagramLineGlyph(directions, options.cornerStyle, options.lineStyle === "heavy" ? "heavy" : "single"),
299
+ )
300
+ }
301
+ }