Skip to main content

codetether_agent/tui/utils/
workspace_entries.rs

1//! FS directory scanning + sorting for workspace snapshots.
2
3use std::path::Path;
4
5use super::workspace_helpers::should_skip_entry;
6use super::workspace_types::{WorkspaceEntry, WorkspaceEntryKind};
7
8pub fn collect_entries(root: &Path) -> Vec<WorkspaceEntry> {
9    let Ok(rd) = std::fs::read_dir(root) else {
10        return Vec::new();
11    };
12    rd.flatten()
13        .filter_map(|entry| {
14            let name = entry.file_name().to_string_lossy().to_string();
15            if should_skip_entry(&name) {
16                return None;
17            }
18            let kind = match entry.file_type() {
19                Ok(ft) if ft.is_dir() => WorkspaceEntryKind::Directory,
20                _ => WorkspaceEntryKind::File,
21            };
22            Some(WorkspaceEntry { name, kind })
23        })
24        .collect()
25}
26
27pub fn sort_entries(entries: &mut [WorkspaceEntry]) {
28    entries.sort_by(|a, b| match (a.kind, b.kind) {
29        (WorkspaceEntryKind::Directory, WorkspaceEntryKind::File) => std::cmp::Ordering::Less,
30        (WorkspaceEntryKind::File, WorkspaceEntryKind::Directory) => std::cmp::Ordering::Greater,
31        _ => a
32            .name
33            .to_ascii_lowercase()
34            .cmp(&b.name.to_ascii_lowercase()),
35    });
36}