gen 0.1.23

A sequence graph and version control system.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use std::{collections::HashMap, io, time::Instant};

use crossterm::{
    event::{self, Event, KeyCode},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use gen_diff::{
    graph::{DiffGenGraph, DiffGenGraphRef, DiffGraphNode},
    operations::{BlockGroupDiff, OperationDiff},
};
use gen_models::db::GraphConnection;
use gen_tui::{LineStyle, graph_controller::GraphController, plotter::PathStyle};
use ratatui::{
    Terminal,
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    widgets::{Block, Borders, List, ListItem, Paragraph},
};

use crate::{
    config::get_theme_color,
    views::{
        gen_graph_widget::{
            GenGraphNodeSizer, create_gen_graph_controller, create_gen_graph_widget,
        },
        helpers::{install_tui_panic_hook, style_text},
    },
};

struct DiffComponent {
    title: String,
    collection: String,
    sample: String,
    block_group: String,
    part_label: Option<String>,
    graph: gen_graph::GenGraph,
    highlight_nodes: Vec<gen_graph::GraphNode>,
    highlight_edges: Vec<(gen_graph::GraphNode, gen_graph::GraphNode)>,
    highlight_color: Color,
    change_label: &'static str,
}

struct ListEntry {
    label: String,
    component_index: Option<usize>,
    db_path: String,
    is_header: bool,
}

/// Apply diff highlights (nodes and edges) to a graph controller.
///
/// This is the only direct GraphController mutation beyond setup — it configures
/// per-node and per-edge highlights for disconnected diff regions that can't use
/// set_path_highlight.
fn apply_diff_highlights(
    controller: &mut GraphController<&gen_graph::GenGraph, GenGraphNodeSizer>,
    component: &DiffComponent,
) {
    let style = PathStyle::new(component.highlight_color)
        .with_line_style(LineStyle::Bold)
        .with_merge_glyphs(true);
    for &node in &component.highlight_nodes {
        controller.set_node_highlight(node, style);
    }
    for &(src, tgt) in &component.highlight_edges {
        controller.set_edge_highlight((src, tgt), style);
    }
}

/// This splits a graph into its connected components. It's needed because a change may happen in the middle of a graph where no
/// start/end nodes are present and the viewer crashes without splitting it.
fn split_connected_components(graph: &DiffGenGraph) -> Vec<DiffGenGraph> {
    use std::collections::HashSet;

    use petgraph::Direction;

    let mut visited: HashSet<DiffGraphNode> = HashSet::new();
    let mut components = vec![];

    for node in graph.nodes() {
        if visited.contains(&node) {
            continue;
        }
        let mut stack = vec![node];
        let mut component_nodes: HashSet<DiffGraphNode> = HashSet::new();
        while let Some(current) = stack.pop() {
            if !visited.insert(current) {
                continue;
            }
            component_nodes.insert(current);
            for neighbor in graph
                .neighbors_directed(current, Direction::Outgoing)
                .chain(graph.neighbors_directed(current, Direction::Incoming))
            {
                if !visited.contains(&neighbor) {
                    stack.push(neighbor);
                }
            }
        }

        let mut subgraph = DiffGenGraph::new();
        for n in &component_nodes {
            subgraph.add_node(*n);
        }
        for (src, dest, edges) in graph.all_edges() {
            if component_nodes.contains(&src) && component_nodes.contains(&dest) {
                subgraph.add_edge(src, dest, edges.clone());
            }
        }
        components.push(subgraph);
    }

    components
}

fn block_group_label(diff: &BlockGroupDiff) -> String {
    if let Some(bg) = &diff.block_group {
        format!(
            "{collection} {sample} {name}",
            collection = bg.collection_name,
            sample = bg
                .sample_name
                .clone()
                .unwrap_or_else(|| "Reference".to_string()),
            name = bg.name
        )
    } else {
        format!("BlockGroup {}", diff.id)
    }
}

pub fn view_diff(
    conn: &GraphConnection,
    diffs: &HashMap<String, OperationDiff>,
) -> Result<(), io::Error> {
    let mut components: Vec<DiffComponent> = vec![];
    let mut components_by_db: HashMap<String, Vec<usize>> = HashMap::new();
    let mut db_order = diffs.keys().cloned().collect::<Vec<_>>();
    db_order.sort();

    for db_path in &db_order {
        if let Some(diff) = diffs.get(db_path)
            && let Some(db_diff) = diff.dbs.get(db_path)
        {
            for component in collect_components(&db_diff.added_block_groups, "Add") {
                let entry = components_by_db.entry(db_path.clone()).or_default();
                entry.push(components.len());
                components.push(component);
            }
            for component in collect_components(&db_diff.removed_block_groups, "Remove") {
                let entry = components_by_db.entry(db_path.clone()).or_default();
                entry.push(components.len());
                components.push(component);
            }
        }
    }

    if components.is_empty() {
        println!("No differences to display.");
        return Ok(());
    }

    install_tui_panic_hook();

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut selected = 0usize;
    let mut expanded_db = db_order.first().cloned();
    let mut current_component = 0usize;

    // Panel focus: two-mode system matching operations view
    let mut panel_focus = "diff_list"; // "diff_list" or "graph_view"
    let mut panel_activated = true; // Start with diff list activated

    // Three-state border styles matching operations view
    let focused_style = Style::default()
        .fg(Color::Blue)
        .add_modifier(Modifier::BOLD);
    let selected_style = Style::default()
        .fg(Color::White)
        .add_modifier(Modifier::BOLD);
    let unfocused_style = Style::default().fg(Color::Gray);

    // Setup: create controller and apply highlights for initial component
    let mut graph_controller = create_gen_graph_controller(&components[current_component].graph);
    apply_diff_highlights(&mut graph_controller, &components[current_component]);

    let mut last_frame_time = Instant::now();

    let result = (|| -> Result<(), io::Error> {
        loop {
            let entries = build_entries(&db_order, &components, &components_by_db, &expanded_db);
            if entries.is_empty() {
                break;
            }
            if selected >= entries.len() {
                selected = 0;
            }
            let desired_component = resolve_selected_component(
                &entries,
                selected,
                &components_by_db,
                expanded_db.as_ref(),
            )
            .unwrap_or(0);
            if desired_component != current_component {
                current_component = desired_component;
                // Setup: create new controller and apply highlights on component switch
                graph_controller =
                    create_gen_graph_controller(&components[current_component].graph);
                apply_diff_highlights(&mut graph_controller, &components[current_component]);
            }

            // Calculate frame delta for smooth animations
            let now = Instant::now();
            let frame_delta = now.duration_since(last_frame_time);
            last_frame_time = now;

            terminal.draw(|f| {
                let outer = Layout::default()
                    .direction(Direction::Vertical)
                    .constraints([Constraint::Min(1), Constraint::Length(1)])
                    .split(f.area());

                let main = Layout::default()
                    .direction(Direction::Horizontal)
                    .constraints([Constraint::Length(45), Constraint::Min(1)])
                    .split(outer[0]);

                // Left panel: diff list
                let list_items: Vec<ListItem> = entries
                    .iter()
                    .enumerate()
                    .map(|(i, entry)| {
                        let style = if i == selected {
                            Style::default()
                                .fg(Color::Cyan)
                                .add_modifier(Modifier::BOLD)
                        } else {
                            Style::default()
                        };
                        ListItem::new(entry.label.clone()).style(style)
                    })
                    .collect();

                let list_title = if !panel_activated && panel_focus == "diff_list" {
                    "[Diff Parts]"
                } else {
                    "Diff Parts"
                };

                let list_border_style = if panel_activated && panel_focus == "diff_list" {
                    focused_style
                } else if !panel_activated && panel_focus == "diff_list" {
                    selected_style
                } else {
                    unfocused_style
                };

                let list = List::new(list_items).block(
                    Block::default()
                        .title(list_title)
                        .borders(Borders::ALL)
                        .border_style(list_border_style),
                );
                f.render_widget(list, main[0]);

                // Right panel: graph via widget
                let graph_title_text = format!(
                    "{} ({}/{})",
                    components[current_component].title,
                    current_component + 1,
                    components.len()
                );
                let graph_title = if !panel_activated && panel_focus == "graph_view" {
                    format!("[{}]", graph_title_text)
                } else {
                    graph_title_text
                };

                let graph_border_style = if panel_activated && panel_focus == "graph_view" {
                    focused_style
                } else if !panel_activated && panel_focus == "graph_view" {
                    selected_style
                } else {
                    unfocused_style
                };

                let graph_block = Block::default()
                    .title(graph_title)
                    .borders(Borders::ALL)
                    .border_style(graph_border_style);
                let inner_canvas = graph_block.inner(main[1]);

                // Pre-render animation tick
                graph_controller.viewport_state.focus();
                graph_controller.viewport_state.viewport_bounds = inner_canvas;
                graph_controller.update_animations(frame_delta);

                f.render_widget(graph_block, main[1]);

                let canvas_style = Style::default().bg(get_theme_color("canvas").unwrap());
                let widget = create_gen_graph_widget(conn)
                    .detail_level(graph_controller.get_detail_level())
                    .style(canvas_style)
                    .cursor();
                f.render_stateful_widget(widget, inner_canvas, &mut graph_controller);

                // Status bar with themed styling
                let status_bar_area = outer[1];
                let panel_messages = if !panel_activated {
                    "*tab* toggle focus | *enter* activate | *q* quit"
                } else if panel_focus == "diff_list" {
                    "*↑↓* select | *tab* toggle focus | *esc* leave panel | *q* quit"
                } else {
                    "*←→↑↓* pan | *+/-* zoom | *tab* toggle focus | *esc* leave panel | *q* quit"
                };

                let status_bar_contents = format!(
                    "{panel_messages:^width$}",
                    width = status_bar_area.width as usize
                );
                let status_line = style_text(
                    &status_bar_contents,
                    Style::default().fg(get_theme_color("text_muted").unwrap()),
                    Style::default().fg(get_theme_color("highlight").unwrap()),
                );
                let status_bar = Paragraph::new(status_line)
                    .style(Style::default().bg(get_theme_color("statusbar").unwrap()));
                f.render_widget(status_bar, status_bar_area);
            })?;

            if event::poll(std::time::Duration::from_millis(100))?
                && let Event::Key(key) = event::read()?
            {
                if !panel_activated {
                    // NAVIGATION MODE: Tab to toggle focus, Enter to activate, q to quit
                    match key.code {
                        KeyCode::Tab | KeyCode::Left | KeyCode::Right => {
                            panel_focus = if panel_focus == "diff_list" {
                                "graph_view"
                            } else {
                                "diff_list"
                            };
                        }
                        KeyCode::Enter => {
                            panel_activated = true;
                        }
                        KeyCode::Esc | KeyCode::Char('q') => break,
                        _ => {}
                    }
                } else {
                    // ACTIVE MODE: Esc to deactivate; panel-specific controls
                    if key.code == KeyCode::Esc {
                        panel_activated = false;
                    } else if key.code == KeyCode::Tab {
                        // Toggle focus between panels while staying activated
                        panel_focus = if panel_focus == "diff_list" {
                            "graph_view"
                        } else {
                            "diff_list"
                        };
                    } else if key.code == KeyCode::Char('q') {
                        break;
                    } else if panel_focus == "diff_list" {
                        // List panel: Up/Down to select
                        match key.code {
                            KeyCode::Up => {
                                if selected > 0 {
                                    selected -= 1;
                                    if let Some(entry) = entries.get(selected) {
                                        expanded_db = Some(entry.db_path.clone());
                                    }
                                }
                            }
                            KeyCode::Down => {
                                if selected + 1 < entries.len() {
                                    selected += 1;
                                    if let Some(entry) = entries.get(selected) {
                                        expanded_db = Some(entry.db_path.clone());
                                    }
                                }
                            }
                            _ => {}
                        }
                    } else {
                        // Graph panel: delegate to controller
                        let _ = graph_controller.handle_key_event(key);
                    }
                }
            }
        }
        Ok(())
    })();

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    result
}

fn collect_components(graphs: &[BlockGroupDiff], change_label: &'static str) -> Vec<DiffComponent> {
    let mut components = Vec::new();
    let highlight_color = match change_label {
        "Add" => Color::Green,
        "Remove" => Color::Red,
        _ => Color::White,
    };
    for graph_diff in graphs {
        let parts = split_connected_components(&graph_diff.graph);
        let (collection, sample, block_group) = if let Some(bg) = &graph_diff.block_group {
            (
                bg.collection_name.clone(),
                bg.sample_name
                    .clone()
                    .unwrap_or_else(|| "Reference".to_string()),
                bg.name.clone(),
            )
        } else {
            (
                String::from("Unknown"),
                String::from("Unknown"),
                String::from("Unknown"),
            )
        };
        if parts.len() <= 1 {
            let diff_graph = graph_diff.graph.clone();
            components.push(build_component(
                &diff_graph,
                change_label,
                highlight_color,
                &block_group_label(graph_diff),
                collection,
                sample,
                block_group,
                None,
            ));
        } else {
            let total = parts.len();
            for (idx, diff_graph) in parts.into_iter().enumerate() {
                components.push(build_component(
                    &diff_graph,
                    change_label,
                    highlight_color,
                    &format!(
                        "{} (part {}/{})",
                        block_group_label(graph_diff),
                        idx + 1,
                        total
                    ),
                    collection.clone(),
                    sample.clone(),
                    block_group.clone(),
                    Some(format!("part {}/{}", idx + 1, total)),
                ));
            }
        }
    }
    components
}

#[allow(clippy::too_many_arguments)]
fn build_component(
    diff_graph: &DiffGenGraph,
    change_label: &'static str,
    highlight_color: Color,
    title: &str,
    collection: String,
    sample: String,
    block_group: String,
    part_label: Option<String>,
) -> DiffComponent {
    let graph: gen_graph::GenGraph = DiffGenGraphRef(diff_graph).into();
    let highlight_edges = collect_highlight_edges(diff_graph);
    let highlight_nodes = collect_highlight_nodes(diff_graph);
    DiffComponent {
        title: format!("{change_label} {title}"),
        collection,
        sample,
        block_group,
        part_label,
        graph,
        highlight_nodes,
        highlight_edges,
        highlight_color,
        change_label,
    }
}

/// Collect edges that are new in the diff graph, returning them as a flat
/// vector of (source, target) pairs suitable for set_edge_highlight.
fn collect_highlight_edges(
    diff_graph: &DiffGenGraph,
) -> Vec<(gen_graph::GraphNode, gen_graph::GraphNode)> {
    let mut edges = Vec::new();
    for (src, dest, edge_data) in diff_graph.all_edges() {
        if edge_data.iter().any(|edge| edge.is_new) {
            edges.push((src.node, dest.node));
        }
    }
    edges
}

/// Collect nodes that are new in the diff graph, returning them as a flat
/// vector suitable for set_node_highlight.
fn collect_highlight_nodes(diff_graph: &DiffGenGraph) -> Vec<gen_graph::GraphNode> {
    diff_graph
        .nodes()
        .filter_map(|node| node.is_new.then_some(node.node))
        .collect()
}

fn build_entries(
    db_order: &[String],
    components: &[DiffComponent],
    components_by_db: &HashMap<String, Vec<usize>>,
    expanded_db: &Option<String>,
) -> Vec<ListEntry> {
    let mut entries = Vec::new();
    for db_path in db_order {
        entries.push(ListEntry {
            label: db_path.clone(),
            component_index: None,
            db_path: db_path.clone(),
            is_header: true,
        });
        if expanded_db.as_ref() == Some(db_path)
            && let Some(indices) = components_by_db.get(db_path)
        {
            for index in indices {
                let component = &components[*index];
                let part = component
                    .part_label
                    .as_ref()
                    .map(|p| format!(" | {p}"))
                    .unwrap_or_default();
                let label = format!(
                    "  {change} | {collection} | {sample} | {bg}{part}",
                    change = component.change_label,
                    collection = component.collection,
                    sample = component.sample,
                    bg = component.block_group,
                    part = part
                );
                entries.push(ListEntry {
                    label,
                    component_index: Some(*index),
                    db_path: db_path.clone(),
                    is_header: false,
                });
            }
        }
    }
    entries
}

fn resolve_selected_component(
    entries: &[ListEntry],
    selected: usize,
    components_by_db: &HashMap<String, Vec<usize>>,
    expanded_db: Option<&String>,
) -> Option<usize> {
    if let Some(entry) = entries.get(selected) {
        if let Some(index) = entry.component_index {
            return Some(index);
        }
        if entry.is_header
            && let Some(indices) = components_by_db.get(&entry.db_path)
        {
            return indices.first().copied();
        }
    }
    if let Some(db_path) = expanded_db
        && let Some(indices) = components_by_db.get(db_path)
    {
        return indices.first().copied();
    }
    None
}