git_workty/commands/
pick.rs

1use crate::git::GitRepo;
2
3use crate::ui::{print_error, UiOptions};
4use crate::worktree::list_worktrees;
5use anyhow::Result;
6use dialoguer::FuzzySelect;
7use is_terminal::IsTerminal;
8
9pub fn execute(repo: &GitRepo, _opts: &UiOptions) -> Result<()> {
10    if !std::io::stdin().is_terminal() {
11        print_error(
12            "Cannot run interactive picker in non-TTY environment",
13            Some("Use `git workty go <name>` for non-interactive selection."),
14        );
15        std::process::exit(1);
16    }
17
18    let worktrees = list_worktrees(repo)?;
19    if worktrees.is_empty() {
20        print_error("No worktrees found", None);
21        std::process::exit(1);
22    }
23
24    let items: Vec<String> = worktrees
25        .iter()
26        .map(|worktree| worktree.name().to_string())
27        .collect();
28
29    let selection = FuzzySelect::new()
30        .with_prompt("Select worktree")
31        .items(&items)
32        .default(0)
33        .interact_opt()?;
34
35    match selection {
36        Some(idx) => {
37            println!("{}", worktrees[idx].path.display());
38            Ok(())
39        }
40        None => {
41            std::process::exit(130);
42        }
43    }
44}