termiflow 0.1.0

Terminal-native Mermaid flowchart renderer — jq for diagrams
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Cycle edge routing and node geometry helpers.
//!
//! This module provides:
//! - `route_cycle_edge`: Routes cycle edges through the gutter (right for TD/BT, bottom for LR/RL)
//! - `center_x`, `center_y`: Calculate visual center coordinates of nodes

use crate::graph::Node;
use crate::spacing::SpacingConfig;
use crate::style::StyleChars;

use super::canvas::Canvas;
use super::semantic::CellOwnerKind;

const CYCLE_Z_INDEX: u8 = 5;

// ============================================================================
// Cycle Edge Routing
// ============================================================================

/// Route a cycle edge through the appropriate gutter.
///
/// For TD/BT: Routes through right gutter (horizontal → vertical → horizontal)
/// For LR/RL: Routes through bottom gutter (vertical → horizontal → vertical)
pub fn route_cycle_edge(
    from: &Node,
    to: &Node,
    canvas: &mut Canvas,
    style: &StyleChars,
    spacing: &SpacingConfig,
    direction: crate::graph::Direction,
    owner_id: Option<&str>,
) {
    use crate::graph::Direction;

    if !canvas.is_visible(from) || !canvas.is_visible(to) {
        return;
    }

    match direction {
        Direction::TD | Direction::TB | Direction::BT => {
            // Vertical layout: use right gutter
            let cycle_gutter = spacing.cycle_gutter;
            if canvas.width <= cycle_gutter {
                return;
            }

            let gutter_x = canvas.width - 2;
            let from_y = from.center_y();
            let to_y = to.center_y();

            // Horizontal line from source to gutter
            for x in (from.x + from.width)..gutter_x {
                set_cycle_edge_char(canvas, x, from_y, style.back_h, style, owner_id);
            }

            // Vertical line in gutter
            let (top, bottom) = if from_y < to_y {
                (from_y, to_y)
            } else {
                (to_y, from_y)
            };
            for y in top..=bottom {
                set_cycle_edge_char(canvas, gutter_x, y, style.back_v, style, owner_id);
            }

            // Horizontal line from gutter to target
            for x in (to.x + to.width)..gutter_x {
                set_cycle_edge_char(canvas, x, to_y, style.back_h, style, owner_id);
            }

            // Corners at source-to-gutter and gutter-to-target junctions
            // Path: horizontal right from source → vertical in gutter → horizontal left to target
            if from_y < to_y {
                // Downward: right-then-down at source, down-then-left at target
                set_cycle_char(canvas, gutter_x, from_y, style.corner_dr, owner_id); //                set_cycle_char(canvas, gutter_x, to_y, style.corner_ur, owner_id);
            //            } else {
                // Upward: right-then-up at source, up-then-left at target
                set_cycle_char(canvas, gutter_x, from_y, style.corner_ur, owner_id); //                set_cycle_char(canvas, gutter_x, to_y, style.corner_dr, owner_id);
                //            }

            // Arrow pointing into target - back-edge always enters from right gutter
            set_cycle_char(canvas, to.x + to.width, to_y, style.arrow_left, owner_id);
        }
        Direction::LR | Direction::RL => {
            // Horizontal layout: use bottom gutter for back-edge
            // Back-edge goes: down from source → horizontal in gutter → up to target

            // Self-loop detection: route around the right side of the node
            if from.id == to.id {
                let node_right = from.x + from.width;
                let node_bottom = from.bottom_y();
                let loop_offset = spacing.row_spacing.max(2);
                let gutter_y = node_bottom + loop_offset;

                if gutter_y >= canvas.height {
                    return;
                }

                let center_x = from.x + from.width / 2;
                let loop_right = node_right + 2;

                if loop_right >= canvas.width {
                    return;
                }

                // Place corners FIRST to prevent overlap resolution converting them to crosses.
                // Path: right from node -> down -> left -> up into node bottom.
                set_cycle_char(
                    canvas,
                    loop_right,
                    from.center_y(),
                    style.corner_dr,
                    owner_id,
                );
                set_cycle_char(canvas, loop_right, gutter_y, style.corner_ur, owner_id);
                set_cycle_char(canvas, center_x, gutter_y, style.corner_ul, owner_id);

                // Draw lines EXCLUDING corner positions
                // Horizontal bridge from the node exit to the right-side loop column.
                for x in node_right..loop_right {
                    set_cycle_edge_char(canvas, x, from.center_y(), style.back_h, style, owner_id);
                }
                // Vertical down from right of box (below the top corner)
                for y in (from.center_y() + 1)..gutter_y {
                    set_cycle_edge_char(canvas, loop_right, y, style.back_v, style, owner_id);
                }
                // Horizontal in gutter (between the two corners)
                for x in (center_x + 1)..loop_right {
                    set_cycle_edge_char(canvas, x, gutter_y, style.back_h, style, owner_id);
                }
                // Vertical up from gutter to box bottom (above the bottom corner)
                for y in (node_bottom + 1)..gutter_y {
                    set_cycle_edge_char(canvas, center_x, y, style.back_v, style, owner_id);
                }
                // Place the visible arrow on the outside entry row so the later
                // box redraw doesn't erase it.
                if node_bottom < canvas.height {
                    set_cycle_char(canvas, center_x, node_bottom, style.arrow_up, owner_id);
                }
                return;
            }

            // Calculate a gutter position below the nodes
            let nodes_bottom = from.bottom_y().max(to.bottom_y());
            let gutter_y = nodes_bottom + spacing.row_spacing.max(2); // Add spacing below nodes

            if gutter_y >= canvas.height {
                // Not enough space for gutter, skip
                return;
            }

            let from_x = from.x + from.width / 2;
            let to_x = to.x + to.width / 2;

            // Vertical line from source box bottom down to gutter
            for y in from.bottom_y()..=gutter_y {
                set_cycle_edge_char(canvas, from_x, y, style.back_v, style, owner_id);
            }

            // Horizontal line in gutter connecting the two vertical lines
            let (left, right) = if from_x < to_x {
                (from_x, to_x)
            } else {
                (to_x, from_x)
            };
            for x in left..=right {
                set_cycle_edge_char(canvas, x, gutter_y, style.back_h, style, owner_id);
            }

            // Vertical line from gutter up to target box bottom
            for y in to.bottom_y()..=gutter_y {
                set_cycle_edge_char(canvas, to_x, y, style.back_v, style, owner_id);
            }

            // Arrow pointing into target's bottom (where it enters from the gutter)
            // The back-edge enters from below, so we draw an up-arrow at the target's bottom
            let target_bottom_y = to.bottom_y();

            // Place the visible arrow on the outside entry row so the later box
            // redraw doesn't erase it.
            if target_bottom_y < canvas.height {
                set_cycle_char(canvas, to_x, target_bottom_y, style.arrow_up, owner_id);
            }
        }
    }
}

fn set_cycle_char(canvas: &mut Canvas, x: usize, y: usize, ch: char, owner_id: Option<&str>) {
    if let Some(owner_id) = owner_id {
        canvas.set_owned(x, y, ch, CellOwnerKind::CycleEdge, owner_id, CYCLE_Z_INDEX);
    } else {
        canvas.set(x, y, ch);
    }
}

fn set_cycle_edge_char(
    canvas: &mut Canvas,
    x: usize,
    y: usize,
    ch: char,
    style: &StyleChars,
    owner_id: Option<&str>,
) {
    if let Some(owner_id) = owner_id {
        canvas.set_edge_char_owned(
            x,
            y,
            ch,
            style,
            CellOwnerKind::CycleEdge,
            owner_id,
            CYCLE_Z_INDEX,
        );
    } else {
        canvas.set_edge_char(x, y, ch, style);
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Calculate the visual center x-coordinate of a node.
#[inline]
pub fn center_x(node: &Node) -> usize {
    node.center_x()
}

/// Calculate the visual center y-coordinate of a node.
#[inline]
pub fn center_y(node: &Node) -> usize {
    node.center_y()
}

#[cfg(test)]
mod tests {
    use super::super::canvas::Canvas;
    use super::*;
    use crate::graph::Direction;
    use crate::spacing::SpacingConfig;
    use crate::style::{BaseStyle, CompositeStyle};

    fn make_node(id: &str, x: usize, y: usize, width: usize) -> Node {
        Node {
            id: id.into(),
            label: id.into(),
            label_lines: Vec::new(),
            shape: crate::graph::NodeShape::Rectangle,
            click_target: None,
            x,
            y,
            width,
            height: crate::style::BOX_HEIGHT,
            rank: 0,
        }
    }

    fn unicode_chars() -> StyleChars {
        CompositeStyle::default().to_style_chars(BaseStyle::Unicode)
    }

    // ==========================================================================
    // Cycle Edge Routing Tests
    // ==========================================================================

    #[test]
    fn cycle_edge_routes_through_gutter_td() {
        let chars = unicode_chars();
        let mut canvas = Canvas::new(80, 40);

        let src = make_node("S", 10, 15, 7);
        let target = make_node("T", 10, 2, 7); // Target is ABOVE source (back-edge)

        let spacing = SpacingConfig::default_config();
        route_cycle_edge(
            &src,
            &target,
            &mut canvas,
            &chars,
            &spacing,
            Direction::TD,
            None,
        );

        // Back-edge uses right gutter
        let gutter_x = canvas.width - 2;

        // Corners should exist at junction points in gutter
        let src_mid_y = src.center_y();
        let target_mid_y = target.center_y();
        // src is below target (src_mid_y > target_mid_y), so this is an "upward cycle":
        // corner_ur (┘) at source (right-then-up), corner_dr (┐) at target (up-then-left)
        assert_eq!(canvas.get(gutter_x, src_mid_y), chars.corner_ur);
        assert_eq!(canvas.get(gutter_x, target_mid_y), chars.corner_dr);

        // Vertical line should exist between the corners
        let mid_y = (src_mid_y + target_mid_y) / 2;
        assert_eq!(canvas.get(gutter_x, mid_y), chars.back_v);

        // Arrow should point into target
        assert_eq!(
            canvas.get(target.x + target.width, target_mid_y),
            chars.arrow_left
        );
    }

    #[test]
    fn cycle_edge_invisible_nodes_is_noop() {
        let chars = unicode_chars();
        let mut canvas = Canvas::new(20, 20); // Small canvas

        // Nodes outside canvas bounds
        let src = make_node("S", 100, 100, 7);
        let target = make_node("T", 100, 50, 7);

        let spacing = SpacingConfig::default_config();
        route_cycle_edge(
            &src,
            &target,
            &mut canvas,
            &chars,
            &spacing,
            Direction::TD,
            None,
        );

        // Nothing should be drawn (gutter would be at x=18)
        assert_eq!(canvas.get(18, 10), ' ');
    }

    #[test]
    fn cycle_edge_lr_target_entry_uses_visible_arrow() {
        let chars = unicode_chars();
        let mut canvas = Canvas::new(80, 30);

        let target = make_node("A", 8, 2, 9);
        let source = make_node("B", 34, 2, 9);

        let spacing = SpacingConfig::default_config();
        route_cycle_edge(
            &source,
            &target,
            &mut canvas,
            &chars,
            &spacing,
            Direction::LR,
            None,
        );

        let target_entry_x = target.x + target.width / 2;
        let target_bottom_y = target.bottom_y();

        assert_eq!(canvas.get(target_entry_x, target_bottom_y), chars.arrow_up);
        assert_eq!(
            canvas.get(target_entry_x, target_bottom_y + 1),
            chars.back_v
        );
    }

    #[test]
    fn cycle_self_loop_lr_places_visible_arrow_below_box() {
        let chars = unicode_chars();
        let mut canvas = Canvas::new(40, 20);

        let node = make_node("Self", 8, 2, 8);

        let spacing = SpacingConfig::default_config();
        route_cycle_edge(
            &node,
            &node,
            &mut canvas,
            &chars,
            &spacing,
            Direction::LR,
            None,
        );

        let center_x = node.x + node.width / 2;
        let loop_right = node.x + node.width + 2;
        let gutter_y = node.bottom_y() + spacing.row_spacing.max(2);
        assert_eq!(
            canvas.get(node.x + node.width, node.center_y()),
            chars.back_h
        );
        assert_eq!(canvas.get(loop_right, node.center_y()), chars.corner_dr);
        assert_eq!(canvas.get(loop_right, gutter_y), chars.corner_ur);
        assert_eq!(canvas.get(center_x, gutter_y), chars.corner_ul);
        assert_eq!(canvas.get(center_x, node.bottom_y()), chars.arrow_up);
        assert_eq!(canvas.get(center_x, node.bottom_y() + 1), chars.back_v);
    }

    // ==========================================================================
    // Helper Function Tests
    // ==========================================================================

    #[test]
    fn center_x_calculates_correctly() {
        // Odd width: 7/2 = 3, so center is at x+3
        let node_odd = make_node("A", 10, 0, 7);
        assert_eq!(center_x(&node_odd), 13); // 10 + 3

        // Even width: 8/2 = 4, so center is at x+4
        let node_even = make_node("B", 10, 0, 8);
        assert_eq!(center_x(&node_even), 14); // 10 + 4

        // Width 1: 1/2 = 0, center is at x
        let node_min = make_node("C", 10, 0, 1);
        assert_eq!(center_x(&node_min), 10);
    }
}