/ lsf / src / main.rs
Swadhin Biswas feat: add --git flag, overhaul CI/CD, rewrite website, prepare for crates.io · 3/7/2026
d0f56db
94 lines · 2.2 KB
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
mod cli;
mod entry;
mod format;
mod git;
mod grid;
mod icons;
mod output;

use std::io::{self, BufWriter, Write};
use std::path::Path;
use std::process;

use clap::Parser;

fn main() {
    let args = cli::Args::parse();

    // Handle custom help/version flags
    if args.help {
        use clap::CommandFactory;
        let mut cmd = cli::Args::command();
        cmd.print_help().ok();
        println!();
        return;
    }
    if args.print_version {
        println!("lsf {}", env!("CARGO_PKG_VERSION"));
        return;
    }

    // Handle tree mode separately
    if args.tree {
        let stdout = io::stdout();
        let mut out = BufWriter::new(stdout.lock());
        let icon_map = if args.no_icons {
            std::collections::HashMap::new()
        } else {
            icons::build_icon_map()
        };
        let use_color = args.use_color();
        let show_icons = !args.no_icons;

        for path_str in &args.paths {
            let path = Path::new(path_str);
            if !path.exists() {
                eprintln!(
                    "lsf: cannot access '{}': No such file or directory",
                    path_str
                );
                continue;
            }

            // Load git status for tree view
            let git_repo = if args.git {
                git::load_git_status(path)
            } else {
                None
            };

            if let Err(e) = output::print_tree(
                &mut out,
                path,
                &args,
                &icon_map,
                use_color,
                show_icons,
                "",
                true,
                0,
                git_repo.as_ref(),
            ) {
                eprintln!("lsf: {}: {}", path_str, e);
            }
        }

        if let Err(e) = out.flush() {
            if e.kind() != io::ErrorKind::BrokenPipe {
                eprintln!("lsf: write error: {}", e);
            }
        }
        return;
    }

    // Normal listing
    if let Err(e) = output::run(&args) {
        if e.kind() == io::ErrorKind::BrokenPipe {
            // Silently exit on broken pipe (e.g., piping to `head`)
            process::exit(0);
        }
        eprintln!("lsf: {}", e);
        process::exit(1);
    }
}