diff --git a/zellij-utils/assets/shell/auto-start.bash b/zellij-utils/assets/shell/auto-start.bash new file mode 100644 index 0000000000..2a050f0049 --- /dev/null +++ b/zellij-utils/assets/shell/auto-start.bash @@ -0,0 +1,11 @@ +if [[ -z "$ZELLIJ" ]]; then + if [[ "$ZELLIJ_AUTO_ATTACH" == "true" ]]; then + zellij attach -c + else + zellij + fi + + if [[ "$ZELLIJ_AUTO_EXIT" == "true" ]]; then + exit + fi +fi diff --git a/zellij-utils/assets/shell/auto-start.fish b/zellij-utils/assets/shell/auto-start.fish new file mode 100644 index 0000000000..c4b9e96755 --- /dev/null +++ b/zellij-utils/assets/shell/auto-start.fish @@ -0,0 +1,11 @@ +if not set -q ZELLIJ + if test "$ZELLIJ_AUTO_ATTACH" = "true" + zellij attach -c + else + zellij + end + + if test "$ZELLIJ_AUTO_EXIT" = "true" + kill $fish_pid + end +end diff --git a/zellij-utils/assets/shell/auto-start.zsh b/zellij-utils/assets/shell/auto-start.zsh new file mode 100644 index 0000000000..2a050f0049 --- /dev/null +++ b/zellij-utils/assets/shell/auto-start.zsh @@ -0,0 +1,11 @@ +if [[ -z "$ZELLIJ" ]]; then + if [[ "$ZELLIJ_AUTO_ATTACH" == "true" ]]; then + zellij attach -c + else + zellij + fi + + if [[ "$ZELLIJ_AUTO_EXIT" == "true" ]]; then + exit + fi +fi diff --git a/zellij-utils/src/setup.rs b/zellij-utils/src/setup.rs index 96bbba41a1..79392c2ee1 100644 --- a/zellij-utils/src/setup.rs +++ b/zellij-utils/src/setup.rs @@ -115,6 +115,24 @@ pub const FISH_EXTRA_COMPLETION: &[u8] = include_bytes!(concat!( "assets/completions/comp.fish" )); +pub const BASH_AUTO_START_SCRIPT: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/", + "assets/shell/auto-start.bash" +)); + +pub const FISH_AUTO_START_SCRIPT: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/", + "assets/shell/auto-start.fish" +)); + +pub const ZSH_AUTO_START_SCRIPT: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/", + "assets/shell/auto-start.zsh" +)); + pub fn dump_default_config() -> std::io::Result<()> { dump_asset(DEFAULT_CONFIG) } @@ -136,10 +154,12 @@ pub struct Setup { /// Dump the default configuration file to stdout #[clap(long)] pub dump_config: bool, + /// Disables loading of configuration file at default location, /// loads the defaults that zellij ships with #[clap(long)] pub clean: bool, + /// Checks the configuration of zellij and displays /// currently used directories #[clap(long)] @@ -148,9 +168,14 @@ pub struct Setup { /// Dump the specified layout file to stdout #[clap(long)] pub dump_layout: Option, + /// Generates completion for the specified shell #[clap(long, value_name = "SHELL")] pub generate_completion: Option, + + /// Generates auto-start script for the specified shell + #[clap(long, value_name = "SHELL")] + pub generate_auto_start: Option, } impl Setup { @@ -242,6 +267,11 @@ impl Setup { std::process::exit(0); } + if let Some(shell) = &self.generate_auto_start { + Self::generate_auto_start(shell); + std::process::exit(0); + } + if let Some(layout) = &self.dump_layout { dump_specified_layout(layout)?; std::process::exit(0); @@ -405,6 +435,30 @@ impl Setup { _ => {} }; } + + fn generate_auto_start(shell: &str) { + let shell: Shell = match shell.to_lowercase().parse() { + Ok(shell) => shell, + _ => { + eprintln!("Unsupported shell: {}", shell); + std::process::exit(1); + } + }; + + let mut out = std::io::stdout(); + match shell { + Shell::Bash => { + let _ = out.write_all(BASH_AUTO_START_SCRIPT); + } + Shell::Fish => { + let _ = out.write_all(FISH_AUTO_START_SCRIPT); + } + Shell::Zsh => { + let _ = out.write_all(ZSH_AUTO_START_SCRIPT); + } + _ => {} + } + } } #[cfg(test)]