Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support the command of auto-start script for shell #1281

Merged
merged 4 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions zellij-utils/assets/shell/auto-start.bash
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions zellij-utils/assets/shell/auto-start.fish
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions zellij-utils/assets/shell/auto-start.zsh
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions zellij-utils/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)]
Expand All @@ -148,9 +168,14 @@ pub struct Setup {
/// Dump the specified layout file to stdout
#[clap(long)]
pub dump_layout: Option<String>,

/// Generates completion for the specified shell
#[clap(long, value_name = "SHELL")]
pub generate_completion: Option<String>,

/// Generates auto-start script for the specified shell
#[clap(long, value_name = "SHELL")]
pub generate_auto_start: Option<String>,
}

impl Setup {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)]
Expand Down