diff --git a/crates/atuin-dotfiles/src/shell.rs b/crates/atuin-dotfiles/src/shell.rs index d4cacf8f..ce760fe1 100644 --- a/crates/atuin-dotfiles/src/shell.rs +++ b/crates/atuin-dotfiles/src/shell.rs @@ -10,6 +10,7 @@ pub mod bash; pub mod fish; pub mod xonsh; pub mod zsh; +pub mod nu; #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Alias { diff --git a/crates/atuin-dotfiles/src/shell/nu.rs b/crates/atuin-dotfiles/src/shell/nu.rs new file mode 100644 index 00000000..d190552c --- /dev/null +++ b/crates/atuin-dotfiles/src/shell/nu.rs @@ -0,0 +1,7 @@ +pub async fn var_config() -> String { + // Because nushell won't autoupdate, we just parse the output of `atuin dotfiles var list` in + // nushell and load the env vars that way + + // we should only do this if the dotfiles are enabled + String::from(r#"atuin dotfiles var list | lines | parse "export {name}={value}" | reduce -f {} {|it, acc| $acc | upsert $it.name $it.value} | load-env"#) +} diff --git a/crates/atuin/src/command/client/init.rs b/crates/atuin/src/command/client/init.rs index 8238a69b..dba126d0 100644 --- a/crates/atuin/src/command/client/init.rs +++ b/crates/atuin/src/command/client/init.rs @@ -7,6 +7,7 @@ use eyre::{Result, WrapErr}; mod bash; mod fish; +mod nu; mod xonsh; mod zsh; @@ -38,51 +39,6 @@ pub enum Shell { } impl Cmd { - fn init_nu(&self) { - let full = include_str!("../../shell/atuin.nu"); - println!("{full}"); - - if std::env::var("ATUIN_NOBIND").is_err() { - const BIND_CTRL_R: &str = r"$env.config = ( - $env.config | upsert keybindings ( - $env.config.keybindings - | append { - name: atuin - modifier: control - keycode: char_r - mode: [emacs, vi_normal, vi_insert] - event: { send: executehostcommand cmd: (_atuin_search_cmd) } - } - ) -)"; - const BIND_UP_ARROW: &str = r" -$env.config = ( - $env.config | upsert keybindings ( - $env.config.keybindings - | append { - name: atuin - modifier: none - keycode: up - mode: [emacs, vi_normal, vi_insert] - event: { - until: [ - {send: menuup} - {send: executehostcommand cmd: (_atuin_search_cmd '--shell-up-key-binding') } - ] - } - } - ) -) -"; - if !self.disable_ctrl_r { - println!("{BIND_CTRL_R}"); - } - if !self.disable_up_arrow { - println!("{BIND_UP_ARROW}"); - } - } - } - fn static_init(&self) { match self.shell { Shell::Zsh => { @@ -95,7 +51,7 @@ $env.config = ( fish::init_static(self.disable_up_arrow, self.disable_ctrl_r); } Shell::Nu => { - self.init_nu(); + nu::init_static(self.disable_up_arrow, self.disable_ctrl_r); } Shell::Xonsh => { xonsh::init_static(self.disable_up_arrow, self.disable_ctrl_r); @@ -143,7 +99,9 @@ $env.config = ( ) .await?; } - Shell::Nu => self.init_nu(), + Shell::Nu => { + nu::init(self.disable_up_arrow, self.disable_ctrl_r).await?; + } Shell::Xonsh => { xonsh::init( alias_store, diff --git a/crates/atuin/src/command/client/init/nu.rs b/crates/atuin/src/command/client/init/nu.rs new file mode 100644 index 00000000..54d886f0 --- /dev/null +++ b/crates/atuin/src/command/client/init/nu.rs @@ -0,0 +1,55 @@ +use eyre::Result; + +const BIND_CTRL_R: &str = r"$env.config = ( + $env.config | upsert keybindings ( + $env.config.keybindings + | append { + name: atuin + modifier: control + keycode: char_r + mode: [emacs, vi_normal, vi_insert] + event: { send: executehostcommand cmd: (_atuin_search_cmd) } + } + ) +)"; +const BIND_UP_ARROW: &str = r" +$env.config = ( + $env.config | upsert keybindings ( + $env.config.keybindings + | append { + name: atuin + modifier: none + keycode: up + mode: [emacs, vi_normal, vi_insert] + event: { + until: [ + {send: menuup} + {send: executehostcommand cmd: (_atuin_search_cmd '--shell-up-key-binding') } + ] + } + } + ) +) +"; + +pub fn init_static(disable_up_arrow: bool, disable_ctrl_r: bool) { + let full = include_str!("../../../shell/atuin.nu"); + println!("{full}"); + + if !disable_ctrl_r && std::env::var("ATUIN_NOBIND").is_err() { + println!("{BIND_CTRL_R}"); + } + if !disable_up_arrow && std::env::var("ATUIN_NOBIND").is_err() { + println!("{BIND_UP_ARROW}"); + } +} + +pub async fn init(disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> { + init_static(disable_up_arrow, disable_ctrl_r); + + let vars = atuin_dotfiles::shell::nu::var_config().await; + + println!("{vars}"); + + Ok(()) +}