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
//! Search-related methods for App.
use super::app::{App, TabKind};
use super::app_states::{
ChangeType, ComponentFilter, DiffSearchResult, SearchMode, VulnChangeType, VulnFilter, VulnSort,
};
impl App {
/// Start searching
pub fn start_search(&mut self) {
self.overlays.search.active = true;
self.overlays.search.clear();
self.overlays.show_help = false;
self.overlays.show_export = false;
self.overlays.show_legend = false;
}
/// Stop searching
pub const fn stop_search(&mut self) {
self.overlays.search.active = false;
}
/// Add character to search query
pub fn search_push(&mut self, c: char) {
self.overlays.search.push_char(c);
}
/// Remove character from search query
pub fn search_pop(&mut self) {
self.overlays.search.pop_char();
}
/// Execute search with current query
pub fn execute_search(&mut self) {
if self.overlays.search.query.len() < 2 {
self.overlays.search.results.clear();
return;
}
let query = &self.overlays.search.query;
let query_lower = query.to_lowercase();
let search_mode = self.overlays.search.mode;
// Build a regex matcher when in regex mode; show error on invalid patterns.
let regex_matcher = if search_mode == SearchMode::Regex {
match regex::RegexBuilder::new(query)
.case_insensitive(true)
.build()
{
Ok(re) => {
self.overlays.search.search_error = None;
Some(re)
}
Err(e) => {
self.overlays.search.search_error = Some(format!("Invalid regex: {e}"));
self.overlays.search.results.clear();
return;
}
}
} else {
self.overlays.search.search_error = None;
None
};
// Closure that performs the appropriate match depending on mode.
let matches_query = |text: &str| -> bool {
match search_mode {
SearchMode::Substring => text.to_lowercase().contains(&query_lower),
SearchMode::Regex => regex_matcher.as_ref().is_some_and(|re| re.is_match(text)),
}
};
let mut results = Vec::new();
// Search through diff results if available (Diff mode)
if let Some(ref diff) = self.data.diff_result {
// Search added components
for comp in &diff.components.added {
if matches_query(&comp.name) {
results.push(DiffSearchResult::Component {
name: comp.name.clone(),
version: comp.new_version.clone(),
change_type: ChangeType::Added,
});
}
}
// Search removed components
for comp in &diff.components.removed {
if matches_query(&comp.name) {
results.push(DiffSearchResult::Component {
name: comp.name.clone(),
version: comp.old_version.clone(),
change_type: ChangeType::Removed,
});
}
}
// Search modified components
for change in &diff.components.modified {
if matches_query(&change.name) {
results.push(DiffSearchResult::Component {
name: change.name.clone(),
version: change.new_version.clone(),
change_type: ChangeType::Modified,
});
}
}
// Search introduced vulnerabilities
for vuln in &diff.vulnerabilities.introduced {
if matches_query(&vuln.id) {
results.push(DiffSearchResult::Vulnerability {
id: vuln.id.clone(),
component_name: vuln.component_name.clone(),
severity: Some(vuln.severity.clone()),
change_type: VulnChangeType::Introduced,
});
}
}
// Search resolved vulnerabilities
for vuln in &diff.vulnerabilities.resolved {
if matches_query(&vuln.id) {
results.push(DiffSearchResult::Vulnerability {
id: vuln.id.clone(),
component_name: vuln.component_name.clone(),
severity: Some(vuln.severity.clone()),
change_type: VulnChangeType::Resolved,
});
}
}
// Search license changes (new licenses)
for lic_change in &diff.licenses.new_licenses {
if matches_query(&lic_change.license) {
let component_name = lic_change
.components
.first()
.cloned()
.unwrap_or_else(|| "multiple".to_string());
results.push(DiffSearchResult::License {
license: lic_change.license.clone(),
component_name,
change_type: ChangeType::Added,
});
}
}
// Search license changes (removed licenses)
for lic_change in &diff.licenses.removed_licenses {
if matches_query(&lic_change.license) {
let component_name = lic_change
.components
.first()
.cloned()
.unwrap_or_else(|| "multiple".to_string());
results.push(DiffSearchResult::License {
license: lic_change.license.clone(),
component_name,
change_type: ChangeType::Removed,
});
}
}
}
// Search through single SBOM if available (View mode)
if self.data.diff_result.is_none()
&& let Some(ref sbom) = self.data.sbom
{
// Search components by name
for comp in sbom.components.values() {
if matches_query(&comp.name) {
results.push(DiffSearchResult::Component {
name: comp.name.clone(),
version: comp.version.clone(),
change_type: ChangeType::Added, // reuse Added as "present"
});
}
}
// Search vulnerabilities
for comp in sbom.components.values() {
for vuln in &comp.vulnerabilities {
if matches_query(&vuln.id) {
results.push(DiffSearchResult::Vulnerability {
id: vuln.id.clone(),
component_name: comp.name.clone(),
severity: vuln.severity.as_ref().map(|s| format!("{s:?}")),
change_type: VulnChangeType::Introduced, // reuse as "present"
});
}
}
}
// Search licenses
for comp in sbom.components.values() {
for lic in &comp.licenses.declared {
if matches_query(&lic.expression) {
results.push(DiffSearchResult::License {
license: lic.expression.clone(),
component_name: comp.name.clone(),
change_type: ChangeType::Added, // reuse as "present"
});
}
}
}
}
// F2: Filter component results to match the current component filter.
// Vulnerability and license results are kept regardless.
let comp_filter = self.components_state().filter;
if comp_filter != ComponentFilter::All {
results.retain(|r| match r {
DiffSearchResult::Component { change_type, .. } => match comp_filter {
ComponentFilter::Added => *change_type == ChangeType::Added,
ComponentFilter::Removed => *change_type == ChangeType::Removed,
ComponentFilter::Modified => *change_type == ChangeType::Modified,
// EolOnly/EolRisk don't map to search change types — keep all
_ => true,
},
// Keep vulnerability and license results regardless of filter
DiffSearchResult::Vulnerability { .. } | DiffSearchResult::License { .. } => true,
});
}
// Limit results
results.truncate(50);
self.overlays.search.results = results;
self.overlays.search.selected = 0;
}
/// Jump to the currently selected search result
pub fn jump_to_search_result(&mut self) {
if let Some(result) = self
.overlays
.search
.results
.get(self.overlays.search.selected)
.cloned()
{
match result {
DiffSearchResult::Component {
name,
version,
change_type,
..
} => {
// Prefer matching by change type + version when possible
if let Some(index) =
self.find_component_index_all(&name, Some(change_type), version.as_deref())
{
self.components_state_mut().filter = ComponentFilter::All;
self.components_state_mut().selected = index;
self.select_tab(TabKind::Components);
self.stop_search();
return;
}
// Fall back to name-only match across all components
if let Some(index) = self.find_component_index_all(&name, None, None) {
self.components_state_mut().filter = ComponentFilter::All;
self.components_state_mut().selected = index;
self.select_tab(TabKind::Components);
self.stop_search();
return;
}
self.components_state_mut().filter = ComponentFilter::All;
self.select_tab(TabKind::Components);
}
DiffSearchResult::Vulnerability {
id, change_type, ..
} => {
// Align filter/sort so the selection is stable
self.vulnerabilities_state_mut().sort_by = VulnSort::Id;
self.vulnerabilities_state_mut().filter = match change_type {
VulnChangeType::Introduced => VulnFilter::Introduced,
VulnChangeType::Resolved => VulnFilter::Resolved,
};
if let Some(index) = self.find_vulnerability_index(&id) {
self.vulnerabilities_state_mut().selected = index;
}
self.select_tab(TabKind::Vulnerabilities);
}
DiffSearchResult::License { license, .. } => {
// Find the license index
if let Some(ref diff) = self.data.diff_result {
let mut index = 0;
// Search new licenses first
for lic in &diff.licenses.new_licenses {
if lic.license == license {
self.licenses_state_mut().selected = index;
self.select_tab(TabKind::Licenses);
self.stop_search();
return;
}
index += 1;
}
// Then removed licenses
for lic in &diff.licenses.removed_licenses {
if lic.license == license {
self.licenses_state_mut().selected = index;
self.select_tab(TabKind::Licenses);
self.stop_search();
return;
}
index += 1;
}
}
self.select_tab(TabKind::Licenses);
}
}
self.stop_search();
}
}
}