Skip to main content

worktree_io/opener/
entries.rs

1/// How to check whether an editor/terminal is available on the current system.
2pub enum DetectMethod {
3    /// Binary name to look up in `PATH`.
4    Path(&'static str),
5    /// macOS `.app` bundle name (checked in `/Applications` and `~/Applications`).
6    #[cfg(target_os = "macos")]
7    MacosApp(&'static str),
8    /// Unconditionally considered present (e.g. Terminal.app on macOS).
9    Always,
10}
11
12/// A single supported editor or terminal option.
13pub struct EditorEntry {
14    /// Short aliases accepted by `resolve_editor_command` (case-insensitive).
15    pub aliases: &'static [&'static str],
16    /// Human-readable name shown in the setup prompt.
17    pub display: &'static str,
18    /// Shell command passed to the opener (`.` is replaced with the actual path).
19    pub command: &'static str,
20    /// How to check whether this option is available on the current system.
21    pub detect: DetectMethod,
22}
23
24// (aliases, display, command, binary-to-detect-in-PATH)
25const BASE: &[(&[&str], &str, &str, &str)] = &[
26    (&["cursor"], "Cursor", "cursor .", "cursor"),
27    (&["vscode", "code"], "VS Code", "code .", "code"),
28    (&["zed"], "Zed", "zed .", "zed"),
29    (&["subl"], "Sublime Text", "subl .", "subl"),
30    (&["nvim"], "Neovim", "nvim .", "nvim"),
31    (&["vim"], "Vim", "vim .", "vim"),
32    (&["tmux"], "tmux", "tmux", "tmux"),
33    (
34        &["alacritty"],
35        "Alacritty",
36        "alacritty --working-directory .",
37        "alacritty",
38    ),
39    (&["kitty"], "Kitty", "kitty --directory .", "kitty"),
40    (&["wezterm"], "WezTerm", "wezterm start --cwd .", "wezterm"),
41];
42
43/// All supported editor/terminal entries for the current platform.
44#[must_use]
45#[allow(unused_mut)]
46pub fn all_entries() -> Vec<EditorEntry> {
47    let mut v: Vec<EditorEntry> = BASE
48        .iter()
49        .map(|&(aliases, display, command, binary)| EditorEntry {
50            aliases,
51            display,
52            command,
53            detect: DetectMethod::Path(binary),
54        })
55        .collect();
56    #[cfg(target_os = "macos")]
57    {
58        use DetectMethod::{Always, MacosApp};
59        v.push(EditorEntry {
60            aliases: &["terminal"],
61            display: "Terminal",
62            command: "open -a Terminal .",
63            detect: Always,
64        });
65        v.push(EditorEntry {
66            aliases: &["iterm", "iterm2"],
67            display: "iTerm2",
68            command: "open -a iTerm .",
69            detect: MacosApp("iTerm"),
70        });
71        v.push(EditorEntry {
72            aliases: &["warp"],
73            display: "Warp",
74            command: "open -a Warp .",
75            detect: MacosApp("Warp"),
76        });
77        v.push(EditorEntry {
78            aliases: &["ghostty"],
79            display: "Ghostty",
80            command: "open -a Ghostty .",
81            detect: MacosApp("Ghostty"),
82        });
83    }
84    #[cfg(target_os = "windows")]
85    v.push(EditorEntry {
86        aliases: &["wt", "windowsterminal", "terminal"],
87        display: "Windows Terminal",
88        command: "wt -d .",
89        detect: DetectMethod::Path("wt"),
90    });
91    v
92}