1pub mod cli;
9pub mod config;
10pub mod git;
11pub mod scanner;
12pub mod shell;
13pub mod ui;
14
15use std::io::{self, IsTerminal, Write};
16use std::process::ExitCode;
17
18use cli::{Command, ParsedArgs};
19
20pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
21
22pub fn run() -> Result<ExitCode> {
23 let args: Vec<String> = std::env::args().collect();
24 let parsed = cli::parse(&args)?;
25
26 match parsed.command {
27 Command::Help => {
28 print_help();
29 Ok(ExitCode::SUCCESS)
30 }
31 Command::Version => {
32 println!("git-nav {}", env!("CARGO_PKG_VERSION"));
33 Ok(ExitCode::SUCCESS)
34 }
35 Command::Init { shell: sh } => {
36 print!("{}", shell::init_snippet(&sh));
37 Ok(ExitCode::SUCCESS)
38 }
39 Command::InstallShell => {
40 let path = shell::install(None)?;
41 eprintln!("Installed git-nav shell function to {}.", path.display());
42 eprintln!("Reload your shell (e.g. `exec $SHELL -l`) and use `gnav`.");
43 Ok(ExitCode::SUCCESS)
44 }
45 Command::ConfigPath => {
46 println!("{}", config::path().display());
47 Ok(ExitCode::SUCCESS)
48 }
49 Command::Reset => {
50 let cfg = config::prompt_and_save()?;
51 eprintln!(
52 "Config saved: root={}, depth={}",
53 cfg.root.display(),
54 cfg.depth
55 );
56 Ok(ExitCode::SUCCESS)
57 }
58 Command::Run => run_interactive(parsed),
59 }
60}
61
62fn run_interactive(parsed: ParsedArgs) -> Result<ExitCode> {
63 let mut cfg = config::load_or_init()?;
64 if let Some(r) = parsed.root_override {
65 cfg.root = r;
66 }
67 if let Some(d) = parsed.depth_override {
68 cfg.depth = d;
69 }
70
71 let root = cfg.expanded_root();
72 if !root.exists() {
73 return Err(format!("root does not exist: {}", root.display()).into());
74 }
75
76 let repos = scanner::find_repos(&root, cfg.depth);
77 if repos.is_empty() {
78 eprintln!("No git repos found under {}", root.display());
79 return Ok(ExitCode::SUCCESS);
80 }
81
82 let mut infos = git::gather_infos(&repos);
83 infos.sort_by_key(|a| a.name.to_lowercase());
84
85 let stdout_is_tty = io::stdout().is_terminal();
86 let stderr_is_tty = io::stderr().is_terminal();
87
88 let selection = if stderr_is_tty {
89 ui::tui::select(&infos)?
90 } else {
91 ui::render::print_table(&infos);
92 prompt_number(&infos)?
93 };
94
95 let Some(idx) = selection else {
96 return Ok(ExitCode::SUCCESS);
97 };
98
99 let path = &infos[idx].path;
100 if stdout_is_tty {
101 eprintln!();
102 eprintln!("Selected: {path}");
103 eprintln!();
104 eprintln!("cd didn't happen because git-nav was run directly.");
105 eprintln!("Install the shell wrapper once with: git-nav --install-shell");
106 eprintln!("Or eval it in your rc file: eval \"$(git-nav --init zsh)\"");
107 } else {
108 println!("{path}");
109 }
110 Ok(ExitCode::SUCCESS)
111}
112
113fn prompt_number(infos: &[git::RepoInfo]) -> Result<Option<usize>> {
114 eprint!("\nSelect # to cd into (Enter to quit): ");
115 io::stderr().flush().ok();
116 let mut input = String::new();
117 if io::stdin().read_line(&mut input).is_err() {
118 return Ok(None);
119 }
120 let choice = input.trim();
121 if choice.is_empty() {
122 return Ok(None);
123 }
124 match choice.parse::<usize>() {
125 Ok(n) if n >= 1 && n <= infos.len() => Ok(Some(n - 1)),
126 _ => {
127 eprintln!("Invalid selection.");
128 Ok(None)
129 }
130 }
131}
132
133fn print_help() {
134 println!(
135 "git-nav {} — jump between local git repos",
136 env!("CARGO_PKG_VERSION")
137 );
138 println!();
139 println!("USAGE:");
140 println!(" git-nav [OPTIONS]");
141 println!(" git-nav --init <bash|zsh|fish>");
142 println!(" git-nav --install-shell");
143 println!();
144 println!("OPTIONS:");
145 println!(" -r, --root <PATH> Override configured root");
146 println!(" -d, --depth <N> Override configured max depth (default 4)");
147 println!(" --config-path Print the config file path and exit");
148 println!(" --reset Re-run the first-time setup prompt");
149 println!(" --init <SHELL> Print shell function for `eval`");
150 println!(" --install-shell Append the shell function to your rc file");
151 println!(" -h, --help Show this help");
152 println!(" -V, --version Show version");
153}