Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor: Centralize Shell enum and completion logic
Maximilian Roosefe80f4a8fee
diff --git a/src/commands/completion.rs b/src/commands/completion.rsindex 5ee94f992..d8bad6124 100644--- a/src/commands/completion.rs+++ b/src/commands/completion.rs@@ -14,19 +14,16 @@ use clap::Command;use clap_complete::{Shell as CompletionShell, generate};use std::io;use worktrunk::git::{GitError, Repository};--#[derive(clap::ValueEnum, Clone, Copy)]-pub enum Shell {- Bash,- Fish,- Zsh,-}+use worktrunk::shell::Shell;pub fn handle_completion(shell: Shell, cli_cmd: &mut Command) {let completion_shell = match shell {- Shell::Bash => CompletionShell::Bash,+ Shell::Bash | Shell::Oil => CompletionShell::Bash,Shell::Fish => CompletionShell::Fish,Shell::Zsh => CompletionShell::Zsh,+ _ => unreachable!(+ "CLI parsing ensures only shells that support completion can be passed here"+ ),};generate(completion_shell, cli_cmd, "wt", &mut io::stdout());}diff --git a/src/commands/init.rs b/src/commands/init.rsindex 24ada4b85..557f7d2ab 100644--- a/src/commands/init.rs+++ b/src/commands/init.rs@@ -18,22 +18,21 @@ pub fn handle_init(shell_name: &str, cmd_name: &str, cli_cmd: &mut Command) -> Rprintln!();println!("# Static completions (commands and flags)");+ // Check if shell supports completion+ if !shell.supports_completion() {+ eprintln!("Completion not yet supported for {}", shell);+ std::process::exit(1);+ }+// Generate completions to a string so we can filter out hidden commandslet mut completion_output = Vec::new();let completion_shell = match shell {- shell::Shell::Bash => CompletionShell::Bash,+ shell::Shell::Bash | shell::Shell::Oil => CompletionShell::Bash,shell::Shell::Fish => CompletionShell::Fish,shell::Shell::Zsh => CompletionShell::Zsh,- // Oil Shell is POSIX-compatible, use Bash completions- shell::Shell::Oil => CompletionShell::Bash,- // Other shells don't have completion support yet- shell::Shell::Elvish- | shell::Shell::Nushell- | shell::Shell::Powershell- | shell::Shell::Xonsh => {- eprintln!("Completion not yet supported for {}", shell);- std::process::exit(1);- }+ _ => unreachable!(+ "supports_completion() check above ensures we only reach this for supported shells"+ ),};generate(completion_shell, cli_cmd, "wt", &mut completion_output);diff --git a/src/commands/mod.rs b/src/commands/mod.rsindex 289cccfdd..ae8bb0846 100644--- a/src/commands/mod.rs+++ b/src/commands/mod.rs@@ -4,8 +4,11 @@ pub mod list;pub mod merge;pub mod worktree;-pub use completion::{Shell, handle_complete, handle_completion};+pub use completion::{handle_complete, handle_completion};pub use init::handle_init;pub use list::handle_list;pub use merge::handle_merge;pub use worktree::{handle_push, handle_remove, handle_switch};++// Re-export Shell from the canonical location+pub use worktrunk::shell::Shell;diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 9ebf91b42..60331b32a 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -127,7 +127,7 @@ pub fn handle_switch(if base.is_some() && !create {eprintln!("{}",- format_warning("--base flag is only used with --create, ignoring")+ worktrunk::styling::format_warning("--base flag is only used with --create, ignoring"));}@@ -338,7 +338,3 @@ pub fn handle_push(target: Option<&str>, allow_merge_commits: bool) -> Result<()println!("Successfully pushed to '{}'", target_branch);Ok(())}--fn format_warning(msg: &str) -> String {- worktrunk::styling::format_warning(msg)-}diff --git a/src/shell.rs b/src/shell.rsindex b6909ab3e..7021be781 100644--- a/src/shell.rs+++ b/src/shell.rs@@ -2,7 +2,7 @@ use askama::Template;use std::fmt;/// Supported shells-#[derive(Debug, Clone, Copy)]+#[derive(Debug, Clone, Copy, clap::ValueEnum)]pub enum Shell {Bash,Elvish,@@ -47,6 +47,13 @@ impl fmt::Display for Shell {}}+impl Shell {+ /// Returns true if this shell supports completion generation+ pub fn supports_completion(&self) -> bool {+ matches!(self, Self::Bash | Self::Fish | Self::Zsh | Self::Oil)+ }+}+/// Shell integration configurationpub struct ShellInit {pub shell: Shell,diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex d909a730a..94aa4fd86 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -148,7 +148,7 @@ _wt() {return 0;;wt__completion)- opts="-h --help bash fish zsh"+ opts="-h --help bash elvish fish nushell oil powershell xonsh zsh"if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0diff --git a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snapindex 770f933ac..3f3d4027d 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap@@ -150,7 +150,7 @@ _wt() {return 0;;wt__completion)- opts="-h --help bash fish zsh"+ opts="-h --help bash elvish fish nushell oil powershell xonsh zsh"if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 2faeff2dc..fbcab426e 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -159,7 +159,7 @@ _arguments "${_arguments_options[@]}" : \_arguments "${_arguments_options[@]}" : \'-h[Print help]' \'--help[Print help]' \-':shell -- Shell to generate completions for:(bash fish zsh)' \+':shell -- Shell to generate completions for:(bash elvish fish nushell oil powershell xonsh zsh)' \&& ret=0;;(complete)