kuizer-gui 0.1.2

Desktop interface for kuizer, built on GPUI
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
use super::*;

impl Kuizer {
    pub(super) fn rescan(&mut self, cx: &mut Context<Self>) {
        let shown = if let Phase::Ready(report) = &self.phase {
            self.stale = Some(report.scanned);
            true
        } else {
            self.set_phase(Phase::Loading(Box::default()), cx);
            self.stale = None;
            self.selection.clear();
            self.rows.clear();
            false
        };
        if let Some(cancel) = self.cancel.take() {
            cancel.cancel();
        }
        self.pending = Part::expected(&self.options);
        self.early.clear();
        self.streamed = false;
        self.progress = Activity::default();
        self.started = Instant::now();
        let cancel = Cancel::new();
        self.cancel = Some(cancel.clone());
        let (sender, mut receiver) = mpsc::unbounded();
        let config = self.config.clone();
        let roots = self.roots.clone();
        let options = self.options.clone();
        let snapshots = self.snapshots.clone();
        let spawned = spawn_worker("kuizer-gui-scan", move || {
            scan_parts(
                &config, &roots, &options, &cancel, &snapshots, &sender, shown,
            );
        });
        if let Err(error) = spawned {
            self.scan_over();
            if shown {
                self.show_toast(Notice::Failed(format!("Could not scan: {error}")), cx);
            } else {
                self.set_phase(Phase::Failed(error), cx);
                cx.notify();
            }
            return;
        }
        self.task = Some(cx.spawn(async move |this, cx| {
            while let Some(first) = receiver.next().await {
                let mut updates = vec![first];
                while let Ok(update) = receiver.try_recv() {
                    updates.push(update);
                }
                let done = updates
                    .iter()
                    .any(|update| matches!(update, Update::Done(_)));
                let updated = this.update(cx, |app, cx| app.take(updates, cx));
                if done || updated.is_err() {
                    return;
                }
            }
            let _ = this.update(cx, |app, cx| {
                app.take(
                    vec![Update::Done(
                        Err("the scan stopped unexpectedly".to_owned()),
                    )],
                    cx,
                );
            });
        }));
    }

    /// Applies everything a running scan sent since the window last heard from it, then
    /// refreshes what depends on the sections that changed, once.
    pub(super) fn take(&mut self, updates: Vec<Update>, cx: &mut Context<Self>) {
        let activity_only = updates
            .iter()
            .all(|update| matches!(update, Update::Activity(_)));
        let mut dropped = Vec::new();
        let (mut rows, mut map, mut sections, mut grown) = (false, false, false, false);
        for update in updates {
            match update {
                Update::Activity(activity) => match &mut self.phase {
                    Phase::Loading(shown) => *shown = activity,
                    _ => self.progress = *activity,
                },
                Update::Cached(report) => {
                    if self.cancel.is_some() && self.cached(*report, &mut dropped) {
                        rows = true;
                        map = true;
                    }
                }
                Update::Loaded(loaded) => {
                    let found = matches!(loaded, Loaded::Found(_));
                    let listed = matches!(loaded, Loaded::Artifacts { .. } | Loaded::Caches(_));
                    if self.loaded(loaded, &mut dropped) {
                        if found {
                            grown = true;
                        } else if listed {
                            rows = true;
                            map = true;
                        } else {
                            sections = true;
                        }
                    }
                }
                Update::Done(Ok(scanned)) => {
                    if matches!(self.phase, Phase::Loading(_)) {
                        self.show_parts(&mut dropped);
                    }
                    if let Phase::Ready(report) = &mut self.phase {
                        report.scanned = scanned;
                    }
                    self.scan_over();
                    rows = true;
                    map = true;
                    if self.options.duplicates && self.search.is_none() {
                        self.find_duplicates(cx);
                    } else if self.tab == Tab::Duplicates {
                        self.load_duplicates(cx);
                    }
                }
                Update::Done(Err(error)) => {
                    self.scan_over();
                    let phase = if error == model::CANCELLED {
                        Phase::Start
                    } else {
                        Phase::Failed(error)
                    };
                    self.set_phase(phase, cx);
                }
            }
        }
        let changed = rows || map || sections || grown;
        if changed {
            if let Phase::Ready(report) = &mut self.phase {
                report.drop_paths(&self.excluded);
            }
            if rows || map || sections {
                self.refresh_stats();
            } else {
                self.grow_stats();
            }
            if rows || grown {
                self.refresh_rows();
            }
            if map || ((rows || grown) && self.map.shown) {
                self.refresh_map();
            }
            if self
                .menu
                .as_ref()
                .is_some_and(|menu| !self.known(&menu.target))
            {
                self.menu = None;
            }
        }
        if !dropped.is_empty() {
            cx.background_spawn(async move { drop(dropped) }).detach();
        }
        if activity_only && !changed && matches!(self.phase, Phase::Ready(_)) {
            if self.content_reads_progress() {
                cx.notify();
            }
            return;
        }
        cx.notify();
    }

    pub(super) fn content_reads_progress(&self) -> bool {
        self.tab != Tab::Overview
            && self
                .tab
                .part()
                .is_some_and(|part| self.pending.contains(&part))
    }

    pub(super) fn scan_over(&mut self) {
        self.cancel = None;
        self.stale = None;
        self.pending.clear();
        self.early.clear();
        self.streamed = false;
    }

    /// Shows the saved scan of these folders while the fresh one runs. Before anything fresh
    /// is in, it is the whole report; after that it only fills the sections still pending.
    pub(super) fn cached(&mut self, report: Report, dropped: &mut Vec<Loaded>) -> bool {
        if matches!(self.phase, Phase::Loading(_)) {
            if let Phase::Loading(activity) = &mut self.phase {
                self.progress = std::mem::take(&mut **activity);
            }
            self.stale = Some(report.scanned);
            self.adopt(report);
            self.apply_early(dropped);
            return true;
        }
        let scanned = report.scanned;
        let mut changed = false;
        for part in report.into_parts() {
            let Some(kind) = part.part() else {
                continue;
            };
            let streaming = kind == Part::Artifacts
                && self.streamed
                && self
                    .report()
                    .is_some_and(|shown| !shown.candidates.is_empty());
            if !self.pending.contains(&kind) || streaming {
                dropped.push(part);
                continue;
            }
            if kind == Part::Artifacts {
                self.streamed = false;
            }
            self.stale = Some(scanned);
            self.replace(part, dropped);
            changed = true;
        }
        changed
    }

    pub(super) fn loaded(&mut self, loaded: Loaded, dropped: &mut Vec<Loaded>) -> bool {
        if matches!(self.phase, Phase::Loading(_)) {
            if !matches!(loaded, Loaded::Found(_) | Loaded::Artifacts { .. }) {
                self.early.push(loaded);
                return false;
            }
            self.show_parts(dropped);
        }
        if let Loaded::Found(batch) = loaded {
            return match (self.streamed, &mut self.phase) {
                (true, Phase::Ready(report)) => {
                    dropped.push(report.apply(Loaded::Found(batch)));
                    true
                }
                _ => {
                    dropped.push(Loaded::Found(batch));
                    false
                }
            };
        }
        if let Some(part) = loaded.part() {
            self.pending.remove(&part);
            if part == Part::Artifacts {
                self.streamed = false;
            }
        }
        self.replace(loaded, dropped);
        true
    }

    /// Leaves the activity view for the results once the first artifacts are in, with every
    /// part that arrived before them.
    pub(super) fn show_parts(&mut self, dropped: &mut Vec<Loaded>) {
        if let Phase::Loading(activity) = &mut self.phase {
            self.progress = std::mem::take(&mut **activity);
        }
        self.phase = Phase::Ready(Box::new(Report::empty(&self.roots, &self.options)));
        self.streamed = true;
        self.apply_early(dropped);
    }

    pub(super) fn apply_early(&mut self, dropped: &mut Vec<Loaded>) {
        for part in std::mem::take(&mut self.early) {
            if let Some(kind) = part.part() {
                self.pending.remove(&kind);
            }
            self.replace(part, dropped);
        }
    }

    /// Puts a part into its section of the report on screen and keeps the selection of that
    /// section on the same paths.
    pub(super) fn replace(&mut self, loaded: Loaded, dropped: &mut Vec<Loaded>) {
        let Phase::Ready(report) = &mut self.phase else {
            dropped.push(loaded);
            return;
        };
        let source = loaded.part().and_then(Part::source);
        let kept: HashSet<PathBuf> = source.map_or_else(HashSet::new, |source| {
            report
                .section(source)
                .filter(|candidate| self.selection.contains(&(source, candidate.id)))
                .map(|candidate| candidate.path.clone())
                .collect()
        });
        dropped.push(report.apply(loaded));
        if let Some(source) = source {
            let stale: Vec<(Source, u64)> = self
                .selection
                .keys()
                .filter(|(selected, _)| *selected == source)
                .copied()
                .collect();
            for key in &stale {
                self.selection.remove(key);
            }
            for candidate in report
                .section(source)
                .filter(|candidate| kept.contains(&candidate.path))
            {
                self.selection.insert((source, candidate.id), candidate);
            }
        }
    }

    /// Changes the phase and drops the report it replaces off the UI thread.
    pub(super) fn set_phase(&mut self, phase: Phase, cx: &mut Context<Self>) {
        if let Phase::Ready(report) = std::mem::replace(&mut self.phase, phase) {
            cx.background_spawn(async move { drop(report) }).detach();
        }
        if !matches!(self.phase, Phase::Ready(_)) {
            self.search = None;
        }
    }

    pub(super) fn adopt(&mut self, report: Report) {
        let selection: HashSet<(Source, u64)> = {
            let kept: HashSet<(Source, &Path)> = self
                .selected()
                .into_iter()
                .map(|(source, candidate)| (source, candidate.path.as_path()))
                .collect();
            Source::ALL
                .into_iter()
                .flat_map(|source| {
                    report
                        .section(source)
                        .map(move |candidate| (source, candidate))
                })
                .filter(|(source, candidate)| kept.contains(&(*source, candidate.path.as_path())))
                .map(|(source, candidate)| (source, candidate.id))
                .collect()
        };
        self.selection = Selection::pending(selection);
        self.phase = Phase::Ready(Box::new(report));
        self.menu = None;
        self.prune();
    }
}

pub(super) fn spawn_worker<T: Send + 'static>(
    name: &str,
    work: impl FnOnce() -> T + Send + 'static,
) -> Result<oneshot::Receiver<T>, String> {
    let (sender, receiver) = oneshot::channel();
    std::thread::Builder::new()
        .name(name.to_owned())
        .spawn(move || {
            let _ = sender.send(work());
        })
        .map_err(|error| format!("the {name} thread did not start: {error}"))?;
    Ok(receiver)
}

pub(super) fn scan_parts(
    config: &Config,
    roots: &[PathBuf],
    options: &ScanOptions,
    cancel: &Cancel,
    snapshots: &Snapshots,
    sender: &mpsc::UnboundedSender<Update>,
    shown: bool,
) {
    let saved = (!shown)
        .then(|| {
            let (snapshots, roots, options, sender) = (
                snapshots.clone(),
                roots.to_vec(),
                options.clone(),
                sender.clone(),
            );
            std::thread::Builder::new()
                .name("kuizer-gui-snapshot".to_owned())
                .spawn(move || {
                    if let Some(report) = snapshots.load(&roots, &options) {
                        let _ = sender.unbounded_send(Update::Cached(Box::new(report)));
                    }
                })
                .ok()
        })
        .flatten();
    let saved = Mutex::new(saved);
    let shown_first = || {
        if let Some(loading) = saved.lock().ok().and_then(|mut saved| saved.take()) {
            let _ = loading.join();
        }
    };
    let tracker = {
        let sender = sender.clone();
        Tracker::new(move |activity| {
            let _ = sender.unbounded_send(Update::Activity(Box::new(activity)));
        })
    };
    let recorder = Recorder::new(roots, options);
    let result = model::load(config, roots, options, cancel, &tracker, &|loaded| {
        recorder.record(&loaded);
        shown_first();
        let _ = sender.unbounded_send(Update::Loaded(loaded));
    });
    shown_first();
    let scanned = SystemTime::now();
    let finished = result.is_ok();
    let _ = sender.unbounded_send(Update::Done(result.map(|()| scanned)));
    if finished {
        recorder.save(snapshots, scanned);
    }
}