git_explore/explore/
workspace.rs1use crate::*;
2
3const MAX_DEPTH: usize = 100;
4pub fn find_folders(opt: ListOption) -> Result<Vec<Workspace>> {
6 let mut paths = Vec::new();
7 collect_folders(
8 &opt,
9 Path::new(&opt.base.base_dir),
10 Path::new(""),
11 0,
12 &mut paths,
13 )?;
14
15 Ok(paths)
16}
17#[derive(Debug)]
18pub struct Workspace {
19 pub kind: FilterKind,
20 pub workspace: PathBuf,
21}
22
23fn collect_folders<P, R>(
24 opt: &ListOption,
25 base_path: P,
26 child_path: R,
27 depth: usize,
28 paths: &mut Vec<Workspace>,
29) -> Result<()>
30where
31 P: AsRef<Path>,
32 R: AsRef<Path>,
33{
34 let full_path = base_path.as_ref().join(child_path).canonicalize()?;
35
36 for entry_res in walkdir::WalkDir::new(&full_path).min_depth(1).max_depth(1) {
37 let entry = entry_res?;
38 let entry_path = entry.path();
39
40 if opt.filter.name.eq(entry.file_name().to_str().unwrap()) {
41 paths.push(Workspace {
42 kind: if entry_path.is_dir() {
43 FilterKind::Directory
44 } else {
45 FilterKind::File
46 },
47 workspace: full_path.clone(),
48 });
49 if !opt.base.deep_recurse {
50 return Ok(());
51 }
52 }
53 if depth < MAX_DEPTH && entry_path.is_dir() {
54 collect_folders(opt, &full_path, entry_path, depth + 1, paths)?;
55 }
56 }
57
58 Ok(())
59}
60
61#[test]
62fn search() {
63 use crate::*;
64 use clap::Parser;
65 let cli = RepoCli::parse_from([KEY_COMMAND, "list", "-d", KEY_BASEPATH]);
66 let ret = if let Some(Command::List(opt)) = cli.command {
68 find_folders(opt)
69 } else {
70 todo!()
71 };
72 insta::assert_debug_snapshot!(ret)
73}