cargo-port 0.0.2

A TUI for inspecting and managing Rust projects
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::thread;

use walkdir::WalkDir;

use crate::ci;
use crate::ci::CiRun;
use crate::ci::GhRun;
use crate::list;
use crate::project::GitInfo;
use crate::project::RustProject;

/// Members within a workspace are organized into groups by their first subdirectory.
/// The "inline" group (empty name) contains members directly under the workspace root
/// or under the primary `crates/` directory -- these are shown without a folder header.
pub struct MemberGroup {
    pub name:    String,
    pub members: Vec<RustProject>,
}

pub struct ProjectNode {
    pub project:   RustProject,
    pub groups:    Vec<MemberGroup>,
    pub worktrees: Vec<Self>,
    pub vendored:  Vec<RustProject>,
}

impl ProjectNode {
    pub fn has_members(&self) -> bool { self.groups.iter().any(|g| !g.members.is_empty()) }

    pub fn has_children(&self) -> bool { self.has_members() || !self.worktrees.is_empty() }
}

/// A flattened entry for fuzzy search.
pub struct FlatEntry {
    pub node_index:   usize,
    pub group_index:  usize,
    pub member_index: usize,
    pub name:         String,
}

pub enum BackgroundMsg {
    DiskUsage {
        path:  String,
        bytes: u64,
    },
    CiRuns {
        path: String,
        runs: Vec<CiRun>,
    },
    GitInfo {
        path: String,
        info: GitInfo,
    },
    CratesIoVersion {
        path:      String,
        version:   String,
        downloads: u64,
    },
    RepoMeta {
        path:        String,
        stars:       u64,
        description: Option<String>,
    },
    ProjectDiscovered {
        project: RustProject,
    },
    ScanActivity {
        path: String,
    },
    ScanComplete,
    NetworkOffline,
}

impl BackgroundMsg {
    /// Returns the project path this message relates to, if any.
    pub fn path(&self) -> Option<&str> {
        match self {
            Self::DiskUsage { path, .. }
            | Self::CiRuns { path, .. }
            | Self::GitInfo { path, .. }
            | Self::CratesIoVersion { path, .. }
            | Self::RepoMeta { path, .. } => Some(path),
            Self::ProjectDiscovered { project } => Some(&project.path),
            Self::ScanActivity { .. } | Self::ScanComplete | Self::NetworkOffline => None,
        }
    }
}

/// What a CI fetch function returns. Forces callers to handle the
/// "network failed but cache exists" case explicitly -- the compiler won't
/// let you silently discard cached runs.
pub enum CiFetchResult {
    /// Fresh runs (network succeeded), merged with cache.
    Loaded(Vec<CiRun>),
    /// Network failed; returning whatever the disk cache had.
    CacheOnly(Vec<CiRun>),
}

pub const CACHE_DIR: &str = "cargo-port/ci-cache";

/// Base cache directory: `$TMPDIR/cargo-port/ci-cache`.
pub fn cache_dir() -> Option<PathBuf> {
    std::env::var("TMPDIR")
        .ok()
        .map(PathBuf::from)
        .or_else(|| Some(PathBuf::from("/tmp")))
        .map(|d| d.join(CACHE_DIR))
}

/// Repo-keyed cache directory: `$TMPDIR/cargo-port/ci-cache/{owner}/{repo}`.
fn repo_cache_dir(owner: &str, repo: &str) -> Option<PathBuf> {
    cache_dir().map(|d| d.join(owner).join(repo))
}

/// Public accessor for clearing the cache directory.
pub fn repo_cache_dir_pub(owner: &str, repo: &str) -> Option<PathBuf> {
    repo_cache_dir(owner, repo)
}

const NO_MORE_RUNS_MARKER: &str = ".no_more_runs";
const OLDER_RUNS_FETCH_INCREMENT: u32 = 5;
const CONNECTIVITY_TIMEOUT_SECS: &str = "2";
const CRATES_IO_TIMEOUT_SECS: &str = "5";

/// Check if the "no more runs" marker exists for a repo.
pub fn is_exhausted(owner: &str, repo: &str) -> bool {
    repo_cache_dir(owner, repo).is_some_and(|d| d.join(NO_MORE_RUNS_MARKER).exists())
}

/// Save the "no more runs" marker for a repo.
pub fn mark_exhausted(owner: &str, repo: &str) {
    if let Some(dir) = repo_cache_dir(owner, repo) {
        let _ = std::fs::create_dir_all(&dir);
        let _ = std::fs::write(dir.join(NO_MORE_RUNS_MARKER), "");
    }
}

fn save_cached_run(owner: &str, repo: &str, ci_run: &CiRun) {
    let Some(dir) = repo_cache_dir(owner, repo) else {
        return;
    };
    let _ = std::fs::create_dir_all(&dir);
    let path = dir.join(format!("{}.json", ci_run.run_id));
    if let Ok(json) = serde_json::to_string(ci_run) {
        let _ = std::fs::write(&path, json);
    }
}

fn load_cached_run(owner: &str, repo: &str, run_id: u64) -> Option<CiRun> {
    let dir = repo_cache_dir(owner, repo)?;
    let path = dir.join(format!("{run_id}.json"));
    let contents = std::fs::read_to_string(path).ok()?;
    serde_json::from_str(&contents).ok()
}

/// Count the number of cached CI run files on disk for a given repo.
pub fn count_cached_runs(owner: &str, repo: &str) -> usize {
    let Some(dir) = repo_cache_dir(owner, repo) else {
        return 0;
    };
    let Ok(entries) = std::fs::read_dir(dir) else {
        return 0;
    };
    entries
        .flatten()
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
        .count()
}

/// Load all cached CI runs for a given repo.
pub fn load_all_cached_runs(owner: &str, repo: &str) -> Vec<CiRun> {
    let Some(dir) = repo_cache_dir(owner, repo) else {
        return Vec::new();
    };
    let Ok(entries) = std::fs::read_dir(dir) else {
        return Vec::new();
    };
    entries
        .flatten()
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
        .filter_map(|e| {
            let contents = std::fs::read_to_string(e.path()).ok()?;
            serde_json::from_str::<CiRun>(&contents).ok()
        })
        .collect()
}

/// Fetch recent CI runs from `gh run list`, then process each one (cache-first).
/// Returns the fetched/cached runs for the requested `count`.
fn fetch_recent_runs(
    repo_dir: &Path,
    repo_url: &str,
    owner: &str,
    repo: &str,
    gh_runs: &[GhRun],
) -> Vec<CiRun> {
    gh_runs
        .iter()
        .filter_map(|gh_run| {
            // Try cache first
            if let Some(cached) = load_cached_run(owner, repo, gh_run.database_id) {
                return Some(cached);
            }
            // Cache miss — fetch from `gh` and save
            let ci_run = ci::process_gh_run(gh_run, repo_dir, repo_url)?;
            save_cached_run(owner, repo, &ci_run);
            Some(ci_run)
        })
        .collect()
}

/// Merge fetched + cached runs, deduplicated by `run_id`, sorted descending.
fn merge_runs(fetched: Vec<CiRun>, cached: Vec<CiRun>) -> Vec<CiRun> {
    let mut seen = HashSet::new();
    let mut merged: Vec<CiRun> = Vec::new();

    // Fetched runs take priority
    for run in fetched {
        if seen.insert(run.run_id) {
            merged.push(run);
        }
    }
    for run in cached {
        if seen.insert(run.run_id) {
            merged.push(run);
        }
    }

    merged.sort_by(|a, b| b.run_id.cmp(&a.run_id));
    merged
}

/// Fetch CI runs, using the repo-keyed cache. Merges freshly fetched runs
/// with all previously cached runs for this repo, deduplicated and sorted by `run_id` descending.
///
/// Accepts `(repo_url, owner, repo)` derived from the *local* git remote so that
/// cache loading never depends on network availability.
pub fn fetch_ci_runs_cached(
    repo_dir: &Path,
    repo_url: &str,
    owner: &str,
    repo: &str,
    count: u32,
) -> CiFetchResult {
    let gh_runs = ci::list_runs(repo_dir, None, count).unwrap_or_default();
    let fetched = fetch_recent_runs(repo_dir, repo_url, owner, repo, &gh_runs);
    let cached = load_all_cached_runs(owner, repo);
    let merged = merge_runs(fetched, cached);

    if gh_runs.is_empty() {
        CiFetchResult::CacheOnly(merged)
    } else {
        CiFetchResult::Loaded(merged)
    }
}

/// Fetch older CI runs beyond what we currently have, by requesting a larger
/// `--limit` from `gh run list` and returning any newly discovered runs.
///
/// Accepts `(repo_url, owner, repo)` derived from the *local* git remote.
pub fn fetch_older_runs(
    repo_dir: &Path,
    repo_url: &str,
    owner: &str,
    repo: &str,
    current_count: u32,
) -> CiFetchResult {
    let fetch_count = current_count + OLDER_RUNS_FETCH_INCREMENT;
    let gh_runs = ci::list_runs(repo_dir, None, fetch_count).unwrap_or_default();
    let fetched = fetch_recent_runs(repo_dir, repo_url, owner, repo, &gh_runs);

    // Only return the fetched runs — don't merge with the full cache.
    // The caller already has runs in memory; these get merged there.
    let mut result = fetched;
    result.sort_by(|a, b| b.run_id.cmp(&a.run_id));

    if gh_runs.is_empty() {
        CiFetchResult::CacheOnly(result)
    } else {
        CiFetchResult::Loaded(result)
    }
}

/// Sent once per session when the first network operation fails.
static OFFLINE_NOTIFIED: AtomicBool = AtomicBool::new(false);

/// Quick connectivity probe — tries `gh auth status` with a 2-second timeout.
/// Returns `true` if we appear to be online.
fn check_online() -> bool {
    std::process::Command::new("curl")
        .args([
            "-sf",
            "--max-time",
            CONNECTIVITY_TIMEOUT_SECS,
            "-o",
            "/dev/null",
            "https://crates.io",
        ])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok_and(|s| s.success())
}

/// Send a one-shot offline notification if we haven't already.
fn notify_offline_once(tx: &mpsc::Sender<BackgroundMsg>) {
    if !OFFLINE_NOTIFIED.swap(true, Ordering::Relaxed) {
        let _ = tx.send(BackgroundMsg::NetworkOffline);
    }
}

pub struct CratesIoInfo {
    pub version:   String,
    pub downloads: u64,
}

pub fn fetch_crates_io_info(crate_name: &str) -> Option<CratesIoInfo> {
    let url = format!("https://crates.io/api/v1/crates/{crate_name}");
    let output = std::process::Command::new("curl")
        .args([
            "-sf",
            "--max-time",
            CRATES_IO_TIMEOUT_SECS,
            "-H",
            "User-Agent: cargo-port",
            &url,
        ])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?;
    let krate = json.get("crate")?;
    let version = krate
        .get("max_stable_version")?
        .as_str()
        .map(std::string::ToString::to_string)?;
    let downloads = krate.get("downloads")?.as_u64().unwrap_or(0);
    Some(CratesIoInfo { version, downloads })
}

pub fn dir_size(path: &Path) -> u64 {
    WalkDir::new(path)
        .into_iter()
        .flatten()
        .filter(|e| e.file_type().is_file())
        .filter_map(|e| e.metadata().ok())
        .map(|m| m.len())
        .sum()
}

pub fn build_tree(projects: Vec<RustProject>, inline_dirs: &[String]) -> Vec<ProjectNode> {
    let workspace_paths: Vec<String> = projects
        .iter()
        .filter(|p| p.is_workspace())
        .map(|p| p.path.clone())
        .collect();

    let mut nodes: Vec<ProjectNode> = Vec::new();
    let mut consumed: HashSet<usize> = HashSet::new();

    let top_level_workspaces: HashSet<usize> = projects
        .iter()
        .enumerate()
        .filter(|(_, p)| {
            p.is_workspace()
                && !workspace_paths
                    .iter()
                    .any(|ws| *ws != p.path && p.path.starts_with(&format!("{ws}/")))
        })
        .map(|(i, _)| i)
        .collect();

    for (i, project) in projects.iter().enumerate() {
        if top_level_workspaces.contains(&i) {
            let mut all_members: Vec<RustProject> = projects
                .iter()
                .enumerate()
                .filter(|(j, p)| {
                    *j != i
                        && !top_level_workspaces.contains(j)
                        && p.path.starts_with(&format!("{}/", project.path))
                })
                .map(|(j, p)| {
                    consumed.insert(j);
                    p.clone()
                })
                .collect();

            all_members.sort_by(|a, b| {
                let name_a = a.name.as_deref().unwrap_or(&a.path);
                let name_b = b.name.as_deref().unwrap_or(&b.path);
                name_a.cmp(name_b)
            });

            let groups = group_members(&project.path, all_members, inline_dirs);

            consumed.insert(i);
            nodes.push(ProjectNode {
                project: project.clone(),
                groups,
                worktrees: Vec::new(),
                vendored: Vec::new(),
            });
        }
    }

    for (i, project) in projects.iter().enumerate() {
        if consumed.contains(&i) {
            continue;
        }
        let under_workspace = workspace_paths
            .iter()
            .any(|ws| project.path.starts_with(&format!("{ws}/")));
        if !under_workspace {
            nodes.push(ProjectNode {
                project:   project.clone(),
                groups:    Vec::new(),
                worktrees: Vec::new(),
                vendored:  Vec::new(),
            });
        }
    }

    nodes.sort_by(|a, b| a.project.path.cmp(&b.project.path));

    // Detect vendored crates first, before worktree merging.
    // This catches crates like clay-layout that live inside worktree directories.
    extract_vendored(&mut nodes);

    // Merge worktree nodes into their primary project.
    // A worktree has `worktree_name = Some(...)`, the primary has `None`.
    merge_worktrees(&mut nodes);

    nodes
}

/// Group worktree nodes under their primary (non-worktree) project.
/// Projects match when they share the same package name.
/// The primary itself is also listed as a worktree entry (using its directory name).
fn merge_worktrees(nodes: &mut Vec<ProjectNode>) {
    let mut primary_indices: HashMap<String, usize> = HashMap::new();
    let mut worktree_indices: Vec<usize> = Vec::new();

    for (i, node) in nodes.iter().enumerate() {
        let Some(name) = &node.project.name else {
            continue;
        };
        if node.project.worktree_name.is_some() {
            worktree_indices.push(i);
        } else {
            primary_indices.insert(name.clone(), i);
        }
    }

    // Only process package names that actually have worktrees
    let names_with_worktrees: HashSet<String> = worktree_indices
        .iter()
        .filter_map(|&wi| nodes[wi].project.name.clone())
        .collect();

    // Collect worktree nodes to move (highest index first to preserve lower indices)
    let mut moves: Vec<(usize, String)> = worktree_indices
        .iter()
        .filter_map(|&wi| {
            let name = nodes[wi].project.name.clone()?;
            primary_indices.get(&name)?;
            Some((wi, name))
        })
        .collect();
    moves.sort_by(|a, b| b.0.cmp(&a.0));

    let mut extracted: Vec<(ProjectNode, String)> = Vec::new();
    for (wi, name) in moves {
        let wt_node = nodes.remove(wi);
        extracted.push((wt_node, name));
    }

    // Insert worktree nodes into their primaries, and add the primary itself as a worktree entry
    for (wt_node, name) in extracted {
        if let Some(primary) = nodes.iter_mut().find(|n| {
            n.project.name.as_ref().is_some_and(|n| *n == name) && n.project.worktree_name.is_none()
        }) {
            primary.worktrees.push(wt_node);
        }
    }

    // Add the primary directory itself as the first worktree entry
    for name in &names_with_worktrees {
        if let Some(primary) = nodes.iter_mut().find(|n| {
            n.project.name.as_ref().is_some_and(|n| n == name) && n.project.worktree_name.is_none()
        }) {
            let dir_name = primary
                .project
                .path
                .rsplit('/')
                .next()
                .unwrap_or(&primary.project.path)
                .to_string();
            let mut primary_as_wt = primary.project.clone();
            primary_as_wt.worktree_name = Some(dir_name);
            primary.worktrees.insert(
                0,
                ProjectNode {
                    project:   primary_as_wt,
                    groups:    Vec::new(),
                    worktrees: Vec::new(),
                    vendored:  Vec::new(),
                },
            );
        }
    }
}

/// Find standalone nodes whose path lives inside another node's directory
/// (or inside a worktree's directory) and move them into that node's `vendored` list.
fn extract_vendored(nodes: &mut Vec<ProjectNode>) {
    // Collect abs_paths of all nodes and their worktrees
    let mut parent_paths: Vec<(usize, Option<usize>, String)> = Vec::new();
    for (ni, node) in nodes.iter().enumerate() {
        parent_paths.push((ni, None, node.project.abs_path.clone()));
        for (wi, wt) in node.worktrees.iter().enumerate() {
            parent_paths.push((ni, Some(wi), wt.project.abs_path.clone()));
        }
    }

    // Find which top-level nodes are vendored inside another
    let mut vendored_map: Vec<(usize, usize, Option<usize>)> = Vec::new(); // (vendored_node_idx, parent_node_idx, parent_wt_idx)

    for (vi, vnode) in nodes.iter().enumerate() {
        // Skip nodes that have workspace members or worktrees — they're real projects
        if vnode.has_members() || !vnode.worktrees.is_empty() {
            continue;
        }
        for &(ni, wt_idx, ref parent_abs) in &parent_paths {
            if ni == vi {
                continue;
            }
            if vnode
                .project
                .abs_path
                .starts_with(&format!("{parent_abs}/"))
            {
                vendored_map.push((vi, ni, wt_idx));
                break;
            }
        }
    }

    if vendored_map.is_empty() {
        return;
    }

    // Extract vendored projects (iterate in reverse to preserve indices)
    let mut vendored_projects: Vec<(usize, Option<usize>, RustProject)> = Vec::new();
    let mut remove_indices: Vec<usize> = vendored_map.iter().map(|&(vi, _, _)| vi).collect();
    remove_indices.sort_unstable();
    remove_indices.dedup();

    for &(vi, ni, wt_idx) in &vendored_map {
        vendored_projects.push((ni, wt_idx, nodes[vi].project.clone()));
    }

    // Remove vendored nodes from the top level (reverse order)
    for &idx in remove_indices.iter().rev() {
        nodes.remove(idx);
    }

    // Adjust parent indices after removal
    for (ni, wt_idx, project) in vendored_projects {
        let adjusted_ni = remove_indices.iter().filter(|&&r| r < ni).count();
        let target_ni = ni - adjusted_ni;
        if let Some(node) = nodes.get_mut(target_ni) {
            if let Some(wi) = wt_idx {
                if let Some(wt) = node.worktrees.get_mut(wi) {
                    wt.vendored.push(project);
                }
            } else {
                node.vendored.push(project);
            }
        }
    }

    // Sort vendored lists
    for node in nodes {
        node.vendored.sort_by(|a, b| a.path.cmp(&b.path));
        for wt in &mut node.worktrees {
            wt.vendored.sort_by(|a, b| a.path.cmp(&b.path));
        }
    }
}

pub fn group_members(
    workspace_path: &str,
    members: Vec<RustProject>,
    inline_dirs: &[String],
) -> Vec<MemberGroup> {
    let prefix = format!("{workspace_path}/");

    let mut group_map: HashMap<String, Vec<RustProject>> = HashMap::new();

    for member in members {
        let relative = member.path.strip_prefix(&prefix).unwrap_or(&member.path);
        let subdir = relative.split('/').next().unwrap_or("").to_string();

        // Members in configured inline dirs or directly in the workspace root are shown inline.
        // Everything else gets grouped by first subdirectory.
        let group_name = if inline_dirs.contains(&subdir) || !relative.contains('/') {
            String::new()
        } else {
            subdir
        };

        group_map.entry(group_name).or_default().push(member);
    }

    let mut groups: Vec<MemberGroup> = group_map
        .into_iter()
        .map(|(name, members)| MemberGroup { name, members })
        .collect();

    // Sort: named directories first (alphabetically), then inline group last
    groups.sort_by(|a, b| {
        let a_inline = a.name.is_empty();
        let b_inline = b.name.is_empty();
        match (a_inline, b_inline) {
            (true, false) => std::cmp::Ordering::Greater,
            (false, true) => std::cmp::Ordering::Less,
            _ => a.name.cmp(&b.name),
        }
    });

    groups
}

pub fn build_flat_entries(nodes: &[ProjectNode]) -> Vec<FlatEntry> {
    let mut entries = Vec::new();
    for (ni, node) in nodes.iter().enumerate() {
        // Add workspace root itself
        let name = node.project.name.as_deref().unwrap_or(&node.project.path);
        entries.push(FlatEntry {
            node_index:   ni,
            group_index:  0,
            member_index: 0,
            name:         name.to_string(),
        });
        // Add all members
        for (gi, group) in node.groups.iter().enumerate() {
            for (mi, member) in group.members.iter().enumerate() {
                let name = member.name.as_deref().unwrap_or(&member.path);
                entries.push(FlatEntry {
                    node_index:   ni,
                    group_index:  gi,
                    member_index: mi,
                    name:         name.to_string(),
                });
            }
        }
    }
    entries
}

/// Fetch all details (disk, git, crates.io, CI) for a single project and send
/// results through the provided channel. Used by both the main scan and priority fetch.
pub fn fetch_project_details(
    tx: &mpsc::Sender<BackgroundMsg>,
    project_path: &str,
    abs_path: &Path,
    project_name: Option<&String>,
    has_git: bool,
    ci_run_count: u32,
) {
    // Git info first (local, instant) — also provides the repo URL for CI cache lookup
    let git_info = if has_git {
        GitInfo::detect(abs_path)
    } else {
        None
    };
    if let Some(ref info) = git_info {
        let _ = tx.send(BackgroundMsg::GitInfo {
            path: project_path.to_string(),
            info: info.clone(),
        });
    }

    // Disk usage (local but slow for large projects)
    let bytes = dir_size(abs_path);
    let _ = tx.send(BackgroundMsg::DiskUsage {
        path: project_path.to_string(),
        bytes,
    });

    // Network operations — check connectivity once before attempting
    let online = if OFFLINE_NOTIFIED.load(Ordering::Relaxed) {
        // Already notified offline — skip the check, still try
        true
    } else {
        let is_online = check_online();
        if !is_online {
            notify_offline_once(tx);
        }
        is_online
    };

    // CI runs — repo identity from *local* git remote, never from network
    if let Some(ref repo_url) = git_info.as_ref().and_then(|g| g.url.clone())
        && let Some((owner, repo)) = ci::parse_owner_repo(repo_url)
    {
        let _ = tx.send(BackgroundMsg::ScanActivity {
            path: format!("CI: {project_path}"),
        });
        let result = fetch_ci_runs_cached(abs_path, repo_url, &owner, &repo, ci_run_count);
        let is_cache_only = matches!(result, CiFetchResult::CacheOnly(_));
        let runs = match result {
            CiFetchResult::Loaded(runs) | CiFetchResult::CacheOnly(runs) => runs,
        };
        if runs.is_empty() && is_cache_only && !online {
            notify_offline_once(tx);
        }
        let _ = tx.send(BackgroundMsg::CiRuns {
            path: project_path.to_string(),
            runs,
        });
    }

    // Crates.io version + downloads (network)
    if let Some(name) = project_name
        && let Some(info) = fetch_crates_io_info(name)
    {
        let _ = tx.send(BackgroundMsg::CratesIoVersion {
            path:      project_path.to_string(),
            version:   info.version,
            downloads: info.downloads,
        });
    }

    // GitHub repo metadata (network) — use local URL, only the API call needs network
    if let Some(ref repo_url) = git_info.as_ref().and_then(|g| g.url.clone())
        && let Some((owner, repo)) = ci::parse_owner_repo(repo_url)
        && let Some(meta) = fetch_repo_meta(&owner, &repo)
    {
        let _ = tx.send(BackgroundMsg::RepoMeta {
            path:        project_path.to_string(),
            stars:       meta.stars,
            description: meta.description,
        });
    }
}

struct RepoMetaInfo {
    stars:       u64,
    description: Option<String>,
}

/// Fetch stars and description for a GitHub repo in a single API call.
fn fetch_repo_meta(owner: &str, repo: &str) -> Option<RepoMetaInfo> {
    let output = std::process::Command::new("gh")
        .args([
            "api",
            &format!("repos/{owner}/{repo}"),
            "--jq",
            "[.stargazers_count, .description] | @tsv",
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null())
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&output.stdout);
    let mut parts = text.trim().splitn(2, '\t');
    let stars = parts.next()?.parse().ok()?;
    let description = parts
        .next()
        .map(String::from)
        .filter(|s| s != "null" && !s.is_empty());
    Some(RepoMetaInfo { stars, description })
}

/// Spawn a streaming scan: walk the directory tree, and for each project discovered
/// do disk + CI together on rayon so progress fills in visibly.
/// Returns `(Sender, Receiver)` — the sender is retained by the caller for priority fetches.
///
/// When `include_non_rust` is true, directories containing `.git` (directory, not file)
/// but no `Cargo.toml` are also discovered as non-Rust projects.
pub fn spawn_streaming_scan(
    scan_root: &Path,
    ci_run_count: u32,
    exclude_dirs: &[String],
    include_non_rust: bool,
) -> (mpsc::Sender<BackgroundMsg>, Receiver<BackgroundMsg>) {
    let (tx, rx) = mpsc::channel();
    let root = scan_root.to_path_buf();
    let excludes: HashSet<String> = exclude_dirs.iter().cloned().collect();

    let scan_tx = tx.clone();
    thread::spawn(move || {
        let entries = WalkDir::new(&root)
            .into_iter()
            .filter_entry(|entry| list::should_visit_entry(entry, Some(&excludes)));

        rayon::scope(|s| {
            for entry in entries.flatten() {
                if entry.file_type().is_dir() {
                    let rel = entry
                        .path()
                        .strip_prefix(&root)
                        .unwrap_or_else(|_| entry.path())
                        .display()
                        .to_string();
                    let _ = scan_tx.send(BackgroundMsg::ScanActivity {
                        path: if rel.is_empty() { ".".to_string() } else { rel },
                    });

                    // Non-Rust project detection: .git dir present but no Cargo.toml
                    if include_non_rust
                        && entry.path().join(".git").is_dir()
                        && !entry.path().join("Cargo.toml").exists()
                    {
                        let project = RustProject::from_git_dir(entry.path());
                        let abs_path = PathBuf::from(&project.abs_path);

                        let _ = scan_tx.send(BackgroundMsg::ProjectDiscovered {
                            project: project.clone(),
                        });

                        let task_tx = scan_tx.clone();
                        let task_path = project.path.clone();
                        let task_abs = abs_path;
                        s.spawn(move |_| {
                            fetch_project_details(
                                &task_tx,
                                &task_path,
                                &task_abs,
                                None,
                                true,
                                ci_run_count,
                            );
                        });
                    }
                }
                if entry.file_type().is_file()
                    && entry.file_name() == "Cargo.toml"
                    && let Ok(project) = RustProject::from_cargo_toml(entry.path())
                {
                    let abs_path = PathBuf::from(&project.abs_path);
                    let has_git = abs_path.join(".git").exists();

                    let _ = scan_tx.send(BackgroundMsg::ProjectDiscovered {
                        project: project.clone(),
                    });

                    // Spawn one rayon task per project that does disk + CI together
                    let task_tx = scan_tx.clone();
                    let task_path = project.path.clone();
                    let task_name = project.name.clone();
                    let task_abs = abs_path;
                    s.spawn(move |_| {
                        fetch_project_details(
                            &task_tx,
                            &task_path,
                            &task_abs,
                            task_name.as_ref(),
                            has_git,
                            ci_run_count,
                        );
                    });
                }
            }
        });

        let _ = scan_tx.send(BackgroundMsg::ScanComplete);
    });

    (tx, rx)
}