browsing/
browsing.rs

1//! An example of browsing a git repo using `radicle-surf`.
2//!
3//! How to run:
4//!
5//!     cargo run --example browsing <git_repo_path>
6//!
7//! This program browses the given repo and prints out the files and
8//! the directories in a tree-like structure.
9
10use radicle_surf::{
11    fs::{self, Directory},
12    Repository,
13};
14use std::{env, time::Instant};
15
16fn main() {
17    let repo_path = match env::args().nth(1) {
18        Some(path) => path,
19        None => {
20            print_usage();
21            return;
22        }
23    };
24    let repo = Repository::discover(repo_path).unwrap();
25    let now = Instant::now();
26    let head = repo.head().unwrap();
27    let root = repo.root_dir(head).unwrap();
28    print_directory(&root, &repo, 0);
29
30    let elapsed_millis = now.elapsed().as_millis();
31    println!("browse with print: {elapsed_millis} ms");
32}
33
34fn print_directory(d: &Directory, repo: &Repository, indent_level: usize) {
35    let indent = " ".repeat(indent_level * 4);
36    println!("{}{}/", &indent, d.name());
37    for entry in d.entries(repo).unwrap() {
38        match entry {
39            fs::Entry::File(f) => println!("    {}{}", &indent, f.name()),
40            fs::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1),
41            fs::Entry::Submodule(s) => println!("    {}{}", &indent, s.name()),
42        }
43    }
44}
45
46fn print_usage() {
47    println!("Usage:");
48    println!("cargo run --example browsing <repo_path>");
49}