diff options
| author | Jamie Quigley <jamie@quigley.xyz> | 2022-12-24 17:18:44 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-24 17:18:44 +0000 |
| commit | a5616aea8fa28db6f8f7b1a44e28f90aab3363b7 (patch) | |
| tree | e4669336601c6f7a6f14dc980493800f98dd0014 /src/command/init.rs | |
| parent | Allow overriding filter and search modes from CLI (#635) (diff) | |
| download | atuin-a5616aea8fa28db6f8f7b1a44e28f90aab3363b7.zip | |
Rework `atuin init` (#652)
* Rework `atuin init`
This allows users to disable the CTRL-R and Up Arrow bindings,
independently from one another
* Document --disable-{ctrl-r,up-arrow}
* Apply suggestions from code review
Co-authored-by: Ellie Huxtable <ellie@elliehuxtable.com>
Co-authored-by: Ellie Huxtable <ellie@elliehuxtable.com>
Diffstat (limited to 'src/command/init.rs')
| -rw-r--r-- | src/command/init.rs | 104 |
1 files changed, 84 insertions, 20 deletions
diff --git a/src/command/init.rs b/src/command/init.rs index 7faff036..585a8281 100644 --- a/src/command/init.rs +++ b/src/command/init.rs @@ -1,7 +1,20 @@ -use clap::Parser; +use clap::{Parser, ValueEnum}; #[derive(Parser)] -pub enum Cmd { +pub struct Cmd { + shell: Shell, + + /// Disable the binding of CTRL-R to atuin + #[clap(long)] + disable_ctrl_r: bool, + + /// Disable the binding of the Up Arrow key to atuin + #[clap(long)] + disable_up_arrow: bool, +} + +#[derive(Clone, Copy, ValueEnum)] +pub enum Shell { /// Zsh setup Zsh, /// Bash setup @@ -10,27 +23,78 @@ pub enum Cmd { Fish, } -fn init_zsh() { - let full = include_str!("../shell/atuin.zsh"); - println!("{full}"); -} +impl Cmd { + fn init_zsh(&self) { + let base = include_str!("../shell/atuin.zsh"); -fn init_bash() { - let full = include_str!("../shell/atuin.bash"); - println!("{full}"); -} + println!("{base}"); -fn init_fish() { - let full = include_str!("../shell/atuin.fish"); - println!("{full}"); -} + if std::env::var("ATUIN_NOBIND").is_err() { + const BIND_CTRL_R: &str = "bindkey '^r' _atuin_search_widget"; + const BIND_UP_ARROW: &str = "bindkey '^[[A' _atuin_up_search_widget +bindkey '^[OA' _atuin_up_search_widget"; + if !self.disable_ctrl_r { + println!("{BIND_CTRL_R}"); + } + if !self.disable_up_arrow { + println!("{BIND_UP_ARROW}"); + } + } + } -impl Cmd { - pub fn run(&self) { - match self { - Self::Zsh => init_zsh(), - Self::Bash => init_bash(), - Self::Fish => init_fish(), + fn init_bash(&self) { + let base = include_str!("../shell/atuin.bash"); + println!("{base}"); + + if std::env::var("ATUIN_NOBIND").is_err() { + const BIND_CTRL_R: &str = r#"bind -x '"\C-r": __atuin_history'"#; + const BIND_UP_ARROW: &str = r#"bind -x '"\e[A": __atuin_history --shell-up-key-binding' +bind -x '"\eOA": __atuin_history --shell-up-key-binding'"#; + if !self.disable_ctrl_r { + println!("{BIND_CTRL_R}"); + } + if !self.disable_up_arrow { + println!("{BIND_UP_ARROW}"); + } + } + } + + fn init_fish(&self) { + let full = include_str!("../shell/atuin.fish"); + println!("{full}"); + + if std::env::var("ATUIN_NOBIND").is_err() { + const BIND_CTRL_R: &str = r"bind \cr _atuin_search"; + const BIND_UP_ARROW: &str = r"bind -k up _atuin_bind_up +bind \eOA _atuin_bind_up +bind \e\[A _atuin_bind_up"; + const BIND_CTRL_R_INS: &str = r"bind -M insert \cr _atuin_search"; + const BIND_UP_ARROW_INS: &str = r"bind -M insert -k up _atuin_bind_up +bind -M insert \eOA _atuin_bind_up +bind -M insert \e\[A _atuin_bind_up"; + + if !self.disable_ctrl_r { + println!("{BIND_CTRL_R}"); + } + if !self.disable_up_arrow { + println!("{BIND_UP_ARROW}"); + } + + println!("if bind -M insert > /dev/null 2>&1"); + if !self.disable_ctrl_r { + println!("{BIND_CTRL_R_INS}"); + } + if !self.disable_up_arrow { + println!("{BIND_UP_ARROW_INS}"); + } + println!("end"); + } + } + pub fn run(self) { + match self.shell { + Shell::Zsh => self.init_zsh(), + Shell::Bash => self.init_bash(), + Shell::Fish => self.init_fish(), } } } |
