diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/command/client/import.rs | 20 | ||||
| -rw-r--r-- | src/command/init.rs | 44 | ||||
| -rw-r--r-- | src/shell/atuin.nu | 41 |
3 files changed, 104 insertions, 1 deletions
diff --git a/src/command/client/import.rs b/src/command/client/import.rs index 7d7c2caf..7abc3d44 100644 --- a/src/command/client/import.rs +++ b/src/command/client/import.rs @@ -9,7 +9,8 @@ use atuin_client::{ database::Database, history::History, import::{ - bash::Bash, fish::Fish, resh::Resh, zsh::Zsh, zsh_histdb::ZshHistDb, Importer, Loader, + bash::Bash, fish::Fish, nu::Nu, nu_histdb::NuHistDb, resh::Resh, zsh::Zsh, + zsh_histdb::ZshHistDb, Importer, Loader, }, }; @@ -29,6 +30,10 @@ pub enum Cmd { Resh, /// Import history from the fish history file Fish, + /// Import history from the nu history file + Nu, + /// Import history from the nu history file + NuHistDb, } const BATCH_SIZE: usize = 100; @@ -68,6 +73,17 @@ impl Cmd { } else if shell.ends_with("/bash") { println!("Detected Bash"); import::<Bash, DB>(db).await + } else if shell.ends_with("/nu") { + if NuHistDb::histpath().is_ok() { + println!( + "Detected Nu-HistDb, using :{}", + NuHistDb::histpath().unwrap().to_str().unwrap() + ); + import::<NuHistDb, DB>(db).await + } else { + println!("Detected Nushell"); + import::<Nu, DB>(db).await + } } else { println!("cannot import {shell} history"); Ok(()) @@ -79,6 +95,8 @@ impl Cmd { Self::Bash => import::<Bash, DB>(db).await, Self::Resh => import::<Resh, DB>(db).await, Self::Fish => import::<Fish, DB>(db).await, + Self::Nu => import::<Nu, DB>(db).await, + Self::NuHistDb => import::<NuHistDb, DB>(db).await, } } } diff --git a/src/command/init.rs b/src/command/init.rs index 585a8281..7cb4b35e 100644 --- a/src/command/init.rs +++ b/src/command/init.rs @@ -21,6 +21,8 @@ pub enum Shell { Bash, /// Fish setup Fish, + /// Nu setup + Nu, } impl Cmd { @@ -90,11 +92,53 @@ bind -M insert \e\[A _atuin_bind_up"; println!("end"); } } + + 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#"let-env config = ( + $env.config | upsert keybindings ( + $env.config.keybindings + | append { + name: atuin + modifier: control + keycode: char_r + mode: emacs + event: { send: executehostcommand cmd: (_atuin_search_cmd) } + } + ) +) +"#; + const BIND_UP_ARROW: &str = r#"let-env config = ( + $env.config | upsert keybindings ( + $env.config.keybindings + | append { + name: atuin + modifier: none + keycode: up + mode: emacs + event: { 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}"); + } + } + } + pub fn run(self) { match self.shell { Shell::Zsh => self.init_zsh(), Shell::Bash => self.init_bash(), Shell::Fish => self.init_fish(), + Shell::Nu => self.init_nu(), } } } diff --git a/src/shell/atuin.nu b/src/shell/atuin.nu new file mode 100644 index 00000000..f8886fc9 --- /dev/null +++ b/src/shell/atuin.nu @@ -0,0 +1,41 @@ +# Source this in your ~/.config/nushell/config.nu +let-env ATUIN_SESSION = (atuin uuid) + +# Magic token to make sure we don't record commands run by keybindings +let ATUIN_KEYBINDING_TOKEN = $"# (random uuid)" + +let _atuin_pre_execution = {|| + let cmd = (commandline) + if not ($cmd | str starts-with $ATUIN_KEYBINDING_TOKEN) { + let-env ATUIN_HISTORY_ID = (atuin history start -- $cmd) + } +} + +let _atuin_pre_prompt = {|| + let last_exit = $env.LAST_EXIT_CODE + if 'ATUIN_HISTORY_ID' not-in $env { + return + } + with-env { RUST_LOG: error } { + atuin history end --exit $last_exit -- $env.ATUIN_HISTORY_ID | null + } +} + +def _atuin_search_cmd [...flags: string] { + [ + $ATUIN_KEYBINDING_TOKEN, + ([ + `commandline (sh -c 'RUST_LOG=error atuin search `, + $flags, + ` -i -- "$0" 3>&1 1>&2 2>&3' (commandline))`, + ] | flatten | str join ''), + ] | str join "\n" +} + +let-env config = ( + $env.config | upsert hooks ( + $env.config.hooks + | upsert pre_execution ($env.config.hooks.pre_execution | append $_atuin_pre_execution) + | upsert pre_prompt ($env.config.hooks.pre_prompt | append $_atuin_pre_prompt) + ) +) |
