sbom-tools 0.2.0

Semantic SBOM diff and analysis tool
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
//! Timeline mode event handlers.

use crate::tui::App;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

pub(super) fn handle_timeline_keys(app: &mut App, key: KeyEvent) -> bool {
    // Handle search input mode
    if app.tabs.timeline.search.active {
        handle_timeline_search(app, key);
        return true;
    }

    // Handle jump mode
    if app.tabs.timeline.jump_mode {
        handle_timeline_jump(app, key);
        return true;
    }

    // Handle version diff modal
    if app.tabs.timeline.show_version_diff_modal {
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => {
                app.tabs.timeline.close_version_diff_modal();
            }
            KeyCode::Left | KeyCode::Char('h') => {
                // Move compare version left
                if let Some(v) = app.tabs.timeline.compare_version
                    && v > 0
                    && v - 1 != app.tabs.timeline.selected_version
                {
                    app.tabs.timeline.set_compare_version(v - 1);
                }
            }
            KeyCode::Right | KeyCode::Char('l') => {
                // Move compare version right
                if let Some(v) = app.tabs.timeline.compare_version
                    && v + 1 < app.tabs.timeline.total_versions
                    && v + 1 != app.tabs.timeline.selected_version
                {
                    app.tabs.timeline.set_compare_version(v + 1);
                }
            }
            _ => {}
        }
        // Modal swallows all keys so none leak to the global fallback.
        return true;
    }

    // Handle component history modal
    if app.tabs.timeline.show_component_history {
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => {
                app.tabs.timeline.close_component_history();
            }
            _ => {}
        }
        return true;
    }

    match key.code {
        // Navigation
        KeyCode::Tab | KeyCode::BackTab | KeyCode::Char('p') => app.tabs.timeline.toggle_panel(),
        KeyCode::Up | KeyCode::Char('k') => app.tabs.timeline.select_prev(),
        KeyCode::Down | KeyCode::Char('j') => app.tabs.timeline.select_next(),

        // Search
        KeyCode::Char('/') => {
            app.tabs.timeline.search.start();
        }

        // In-place match cycling after Enter confirmed a search.
        KeyCode::Char('n') if !app.tabs.timeline.search.matches.is_empty() => {
            app.tabs.timeline.search.next_match();
            if let Some(idx) = app.tabs.timeline.search.current_match_index() {
                app.tabs.timeline.selected_version = idx;
            }
        }
        KeyCode::Char('N') if !app.tabs.timeline.search.matches.is_empty() => {
            app.tabs.timeline.search.prev_match();
            if let Some(idx) = app.tabs.timeline.search.current_match_index() {
                app.tabs.timeline.selected_version = idx;
            }
        }

        // Sort and filter
        KeyCode::Char('s') => {
            app.tabs.timeline.toggle_sort();
            // Search matches are display positions; recompute under the new order.
            update_timeline_search_matches(app);
            app.set_status_message(format!(
                "Sort: {} {}",
                app.tabs.timeline.sort_by.label(),
                app.tabs.timeline.sort_direction.indicator()
            ));
        }
        KeyCode::Char('S') => {
            app.tabs.timeline.toggle_sort_direction();
            update_timeline_search_matches(app);
        }
        KeyCode::Char('f') => {
            app.tabs.timeline.toggle_component_filter();
            // The filter changes how many components are visible; keep the
            // navigation bound and the selection in sync with the filtered
            // list (mirrors the multi-diff 'f' resync).
            if let Some(result) = app.data.timeline_result.as_ref() {
                let visible = crate::tui::views::filtered_evolution_entries(
                    result,
                    app.tabs.timeline.component_filter,
                )
                .len();
                app.tabs.timeline.total_components = visible;
                if app.tabs.timeline.selected_component >= visible {
                    app.tabs.timeline.selected_component = visible.saturating_sub(1);
                }
            }
            app.set_status_message(format!(
                "Filter: {}",
                app.tabs.timeline.component_filter.label()
            ));
        }

        // Version diff modal
        KeyCode::Char('d') => {
            app.tabs.timeline.toggle_version_diff_modal();
        }

        // Chart metric
        KeyCode::Char('m') => {
            app.tabs.timeline.chart_metric = app.tabs.timeline.chart_metric.next();
            app.set_status_message(format!(
                "Chart metric: {}",
                app.tabs.timeline.chart_metric.label()
            ));
        }

        // Statistics panel
        KeyCode::Char('t') => {
            app.tabs.timeline.toggle_statistics();
            let status = if app.tabs.timeline.show_statistics {
                "Statistics: shown"
            } else {
                "Statistics: hidden"
            };
            app.set_status_message(status.to_string());
        }

        // Component history detail. Guarded: with a filter yielding zero
        // entries the modal would clear its rect and render nothing — an
        // invisible overlay that swallows every key except Esc/q.
        KeyCode::Enter | KeyCode::Char(' ') => {
            if app.tabs.timeline.total_components > 0 {
                app.tabs.timeline.toggle_component_history();
            } else {
                app.set_status_message("No components match the current filter");
            }
        }

        // Jump to version
        KeyCode::Char('g') => {
            app.tabs.timeline.start_jump_mode();
        }

        // Chart zoom
        KeyCode::Char('+' | '=') => {
            app.tabs.timeline.zoom_in();
            app.set_status_message(format!("Zoom: {}x", app.tabs.timeline.chart_zoom));
        }
        KeyCode::Char('-' | '_') => {
            app.tabs.timeline.zoom_out();
            app.set_status_message(format!("Zoom: {}x", app.tabs.timeline.chart_zoom));
        }

        // Chart scroll
        KeyCode::Left | KeyCode::Char('h') => {
            app.tabs.timeline.scroll_chart_left();
        }
        KeyCode::Right | KeyCode::Char('l') => {
            app.tabs.timeline.scroll_chart_right();
        }

        // Component deep dive on the selected (filtered) component. Handled
        // mode-locally so the modal opens WITH its version history: the
        // global fallback opened it with default collected_data, which
        // rendered a permanently hollow "Versions tracked: 0" modal.
        KeyCode::Char('D') => {
            open_timeline_component_deep_dive(app);
        }

        _ => return false,
    }
    true
}

/// Open the component deep dive for the selected component with the Versions
/// and Vulnerabilities sections filled from the timeline result.
fn open_timeline_component_deep_dive(app: &mut App) {
    use crate::diff::VersionChangeType;
    use crate::tui::app_states::{ComponentVersionEntry, ComponentVulnInfo};

    struct Collected {
        name: String,
        id: String,
        versions: Vec<ComponentVersionEntry>,
        vulns: Vec<ComponentVulnInfo>,
    }

    let collected = app.data.timeline_result.as_ref().and_then(|result| {
        // Resolve through the SAME filtered list the Components panel
        // displays, so the deep dive matches the highlighted row.
        let entries = crate::tui::views::filtered_evolution_entries(
            result,
            app.tabs.timeline.component_filter,
        );
        let (evo, _) = entries.get(app.tabs.timeline.selected_component)?;

        let versions: Vec<ComponentVersionEntry> = result
            .evolution_summary
            .version_history
            .get(&evo.id)
            .map(|history| {
                history
                    .iter()
                    // Absent points (before first appearance / after removal)
                    // carry no information for the version list.
                    .filter(|p| !matches!(p.change_type, VersionChangeType::Absent))
                    .map(|p| ComponentVersionEntry {
                        version: p.version.clone().unwrap_or_else(|| "-".to_string()),
                        sbom_label: p.sbom_name.clone(),
                        date: result
                            .sboms
                            .get(p.sbom_index)
                            .and_then(|s| s.timestamp.clone()),
                        change_type: match p.change_type {
                            VersionChangeType::Initial => "added",
                            VersionChangeType::Removed => "removed",
                            VersionChangeType::Unchanged => "unchanged",
                            VersionChangeType::Absent => "absent",
                            _ => "modified",
                        }
                        .to_string(),
                    })
                    .collect()
            })
            .unwrap_or_default();

        let mut vulns: Vec<ComponentVulnInfo> = Vec::new();
        for diff in &result.incremental_diffs {
            for (status, list) in [
                ("introduced", &diff.vulnerabilities.introduced),
                ("resolved", &diff.vulnerabilities.resolved),
            ] {
                for v in list.iter().filter(|v| v.component_name == evo.name) {
                    vulns.push(ComponentVulnInfo {
                        vuln_id: v.id.clone(),
                        severity: v.severity.clone(),
                        status: status.to_string(),
                        description: None,
                    });
                }
            }
        }
        vulns.sort_by(|a, b| (&a.vuln_id, &a.status).cmp(&(&b.vuln_id, &b.status)));
        vulns.dedup_by(|a, b| a.vuln_id == b.vuln_id && a.status == b.status);

        Some(Collected {
            name: evo.name.clone(),
            id: evo.id.clone(),
            versions,
            vulns,
        })
    });

    if let Some(c) = collected {
        app.overlays.component_deep_dive.open(c.name, Some(c.id));
        let data = &mut app.overlays.component_deep_dive.collected_data;
        data.version_history = c.versions;
        data.vulnerabilities = c.vulns;
    } else {
        app.set_status_message("No component selected for deep dive");
    }
}

pub(super) fn handle_timeline_search(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc => {
            app.tabs.timeline.search.cancel();
        }
        KeyCode::Enter => {
            app.tabs.timeline.search.confirm();
            if let Some(idx) = app.tabs.timeline.search.current_match_index() {
                app.tabs.timeline.selected_version = idx;
            }
        }
        KeyCode::Backspace => {
            app.tabs.timeline.search.pop();
            update_timeline_search_matches(app);
        }
        KeyCode::Char('r') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.tabs.timeline.search.toggle_mode();
            update_timeline_search_matches(app);
            app.set_status_message(format!(
                "Search mode: {}",
                app.tabs.timeline.search.mode.label()
            ));
        }
        // Live preview: Up/Down move the visible selection through the
        // matches before Enter confirms (one mental model everywhere).
        KeyCode::Down => {
            app.tabs.timeline.search.next_match();
            if let Some(idx) = app.tabs.timeline.search.current_match_index() {
                app.tabs.timeline.selected_version = idx;
            }
        }
        KeyCode::Up => {
            app.tabs.timeline.search.prev_match();
            if let Some(idx) = app.tabs.timeline.search.current_match_index() {
                app.tabs.timeline.selected_version = idx;
            }
        }
        KeyCode::Char(c) => {
            app.tabs.timeline.search.push(c);
            update_timeline_search_matches(app);
        }
        _ => {}
    }
}

pub(super) fn update_timeline_search_matches(app: &mut App) {
    let query = &app.tabs.timeline.search.query;
    if query.is_empty() {
        app.tabs.timeline.search.error = None;
        app.tabs.timeline.search.update_matches(vec![]);
        return;
    }

    // Shared matcher: same substring/regex semantics as every other search.
    let matcher =
        match crate::tui::app_states::SearchMatcher::build(query, app.tabs.timeline.search.mode) {
            Ok(m) => {
                app.tabs.timeline.search.error = None;
                m
            }
            Err(e) => {
                app.tabs.timeline.search.error = Some(e);
                app.tabs.timeline.search.update_matches(vec![]);
                return;
            }
        };

    // Matches are DISPLAY positions so Enter selects the highlighted row
    // under any sort.
    let matches: Vec<usize> = app
        .data
        .timeline_result
        .as_ref()
        .map_or_else(Vec::new, |result| {
            crate::tui::views::ordered_version_indices(result, &app.tabs.timeline)
                .iter()
                .enumerate()
                .filter(|(_, raw)| matcher.is_match(&result.sboms[**raw].name))
                .map(|(display, _)| display)
                .collect()
        });

    app.tabs.timeline.search.update_matches(matches);
}

pub(super) fn handle_timeline_jump(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc => {
            app.tabs.timeline.cancel_jump_mode();
        }
        KeyCode::Enter => {
            // Validate the input BEFORE execute_jump: a rejected jump leaves
            // the (display-index) selection untouched, and remapping it as if
            // it were a raw version would relocate the selection and lie.
            let target = app
                .tabs
                .timeline
                .jump_input
                .parse::<usize>()
                .ok()
                .map(|v| v.saturating_sub(1))
                .filter(|t| *t < app.tabs.timeline.total_versions);
            app.tabs.timeline.execute_jump();
            if let Some(raw) = target {
                // execute_jump sets a RAW (chronological) version; the
                // selection is a display index — map through the permutation.
                if let Some(result) = app.data.timeline_result.as_ref()
                    && let Some(display) =
                        crate::tui::views::ordered_version_indices(result, &app.tabs.timeline)
                            .iter()
                            .position(|&r| r == raw)
                {
                    app.tabs.timeline.selected_version = display;
                }
                app.set_status_message(format!("Jumped to version {}", raw + 1));
            } else {
                app.set_status_message("Invalid version number".to_string());
            }
        }
        KeyCode::Backspace => {
            app.tabs.timeline.jump_pop();
        }
        KeyCode::Char(c) => {
            app.tabs.timeline.jump_push(c);
        }
        _ => {}
    }
}
#[cfg(test)]
mod deep_dive_tests {
    use crate::tui::events::handle_key_event;
    use crate::tui::test_support::{demo_timeline, pin_theme};
    use crate::tui::{App, TabKind};
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    /// 'D' in Timeline mode opens the deep dive WITH the component's real id
    /// and version history (it used to open with default collected_data and
    /// render "Versions tracked: 0 / No version history available" forever).
    #[test]
    fn deep_dive_opens_with_version_history() {
        pin_theme();
        let mut app = App::new_timeline(demo_timeline());
        app.active_tab = TabKind::Summary; // deterministic tab dispatch

        let expected = {
            let result = app.data.timeline_result.as_ref().expect("timeline data");
            crate::tui::views::filtered_evolution_entries(
                result,
                app.tabs.timeline.component_filter,
            )[0]
            .0
            .name
            .clone()
        };

        handle_key_event(
            &mut app,
            KeyEvent::new(KeyCode::Char('D'), KeyModifiers::NONE),
        );
        let dive = &app.overlays.component_deep_dive;
        assert!(dive.visible, "'D' must open the deep dive");
        assert_eq!(
            dive.component_name, expected,
            "must resolve the same entry the Components panel highlights"
        );
        assert!(
            dive.component_id.is_some(),
            "the modal must not render 'ID: Unknown'"
        );
        assert!(
            !dive.collected_data.version_history.is_empty(),
            "the Versions section must carry the timeline's real history"
        );
    }
}