aboutsummaryrefslogtreecommitdiffstats
path: root/src/command
diff options
context:
space:
mode:
authorJamie Quigley <jamie@quigley.xyz>2022-12-24 17:18:44 +0000
committerGitHub <noreply@github.com>2022-12-24 17:18:44 +0000
commita5616aea8fa28db6f8f7b1a44e28f90aab3363b7 (patch)
treee4669336601c6f7a6f14dc980493800f98dd0014 /src/command
parentAllow overriding filter and search modes from CLI (#635) (diff)
downloadatuin-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 '')
-rw-r--r--src/command/init.rs104
-rw-r--r--src/command/mod.rs1
2 files changed, 84 insertions, 21 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(),
}
}
}
diff --git a/src/command/mod.rs b/src/command/mod.rs
index 038b24c8..1411bfd2 100644
--- a/src/command/mod.rs
+++ b/src/command/mod.rs
@@ -25,7 +25,6 @@ pub enum AtuinCmd {
Server(server::Cmd),
/// Output shell setup
- #[command(subcommand)]
Init(init::Cmd),
/// Generate a UUID