Skip to content

Commit 63d17e5

Browse files
committed
feat(update): only list selected updates in summary
1 parent 8d63fbe commit 63d17e5

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

src/commands/update.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ struct Bucket {
117117
indices: Vec<InstanceIdx>,
118118
}
119119

120+
/// Keep only the rows the user picked, then rewrite each remaining row's
121+
/// `dependency_outdated_count` to the sum of bucket counts that survived
122+
/// the filter for its (group, dependency). Otherwise a multi-bucket dep
123+
/// with one bucket left would still print the original total.
124+
pub fn filter_rows_for_display(rows: &[UpdateRow], selection: &[bool]) -> Vec<UpdateRow> {
125+
let mut filtered: Vec<UpdateRow> = rows
126+
.iter()
127+
.zip(selection.iter())
128+
.filter(|&(_, &picked)| picked)
129+
.map(|(row, _)| row.clone())
130+
.collect();
131+
let mut totals: std::collections::HashMap<(usize, String), usize> = std::collections::HashMap::new();
132+
for row in &filtered {
133+
*totals.entry((row.group_idx, row.dependency_name.clone())).or_insert(0) += row.bucket_count;
134+
}
135+
for row in &mut filtered {
136+
if let Some(total) = totals.get(&(row.group_idx, row.dependency_name.clone())) {
137+
row.dependency_outdated_count = *total;
138+
}
139+
}
140+
filtered
141+
}
142+
120143
pub fn run<D: DiskIo>(mut ctx: Context, registry_updates: RegistryUpdates, io: &D, tui: &dyn Tui) -> Result<Context, SyncpackError> {
121144
let now = update_row::unix_now();
122145
let rows = build_update_rows(&ctx, &registry_updates, now);
@@ -158,9 +181,12 @@ pub fn run<D: DiskIo>(mut ctx: Context, registry_updates: RegistryUpdates, io: &
158181
(None, (0..rows.len()).collect())
159182
};
160183

161-
update_row::render_rows(&rows, selection.as_deref());
184+
let filtered_for_display: Option<Vec<UpdateRow>> = selection.as_deref().map(|sel| filter_rows_for_display(&rows, sel));
185+
let display_rows: &[UpdateRow] = filtered_for_display.as_deref().unwrap_or(&rows);
186+
187+
update_row::render_rows(display_rows, None);
162188

163-
let counts = update_row::count_diffs(&rows);
189+
let counts = update_row::count_diffs(display_rows);
164190
update_row::render_summary(counts);
165191

166192
// Apply selected rows by copying expected → actual through the

src/commands/update_test.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use {
22
crate::{
3-
commands::update::build_update_rows,
3+
commands::update::{build_update_rows, filter_rows_for_display},
44
test::{builder::TestBuilder, mock_tui::MockTui},
55
},
66
serde_json::json,
@@ -256,6 +256,45 @@ async fn mock_tui_cancel_returns_none() {
256256
assert!(tui.pick(&rows, 80, 24).is_none());
257257
}
258258

259+
#[tokio::test]
260+
async fn filter_rows_for_display_drops_unselected_and_recomputes_dep_total() {
261+
let (ctx, updates) = TestBuilder::new()
262+
.with_packages(vec![
263+
json!({
264+
"name": "package-a",
265+
"version": "1.0.0",
266+
"dependencies": {"foo": "^1.0.0"}
267+
}),
268+
json!({
269+
"name": "package-b",
270+
"version": "1.0.0",
271+
"dependencies": {"foo": "~1.0.0"}
272+
}),
273+
json!({
274+
"name": "package-c",
275+
"version": "1.0.0",
276+
"dependencies": {"foo": "1.0.0"}
277+
}),
278+
])
279+
.with_registry_updates(json!({"foo": ["1.0.0", "1.0.1"]}))
280+
.run_with_updates()
281+
.await;
282+
let rows = build_update_rows(&ctx, &updates.unwrap(), FROZEN_NOW);
283+
assert_eq!(rows.len(), 3, "three buckets for foo");
284+
for row in &rows {
285+
assert_eq!(row.dependency_outdated_count, 3, "pre-filter total reflects all buckets");
286+
}
287+
288+
let selection = vec![true, false, false];
289+
let filtered = filter_rows_for_display(&rows, &selection);
290+
assert_eq!(filtered.len(), 1, "only the selected bucket survives");
291+
assert_eq!(
292+
filtered[0].dependency_outdated_count, 1,
293+
"dep total recomputed to selected bucket counts"
294+
);
295+
assert_eq!(filtered[0].bucket_count, 1);
296+
}
297+
259298
mod apply {
260299
use {
261300
super::*,

0 commit comments

Comments
 (0)