diff options
author | Ellie Huxtable <e@elm.sh> | 2021-04-26 11:50:31 +0100 |
---|---|---|
committer | Ellie Huxtable <e@elm.sh> | 2021-04-26 11:57:30 +0100 |
commit | 7b5c3d543d198a18884c990d540f5debc8a4d8d5 (patch) | |
tree | ffc6b9121f1b1299f1b30a26322e1988683c06c7 /src | |
parent | 4f16e8411e24891b140690798c47747f5c9bb91e (diff) |
Support bash, resolves #3
Diffstat (limited to 'src')
-rw-r--r-- | src/command/import.rs | 69 | ||||
-rw-r--r-- | src/command/init.rs | 25 | ||||
-rw-r--r-- | src/command/mod.rs | 4 | ||||
-rw-r--r-- | src/shell/atuin.bash | 30 | ||||
-rw-r--r-- | src/shell/atuin.zsh | 2 |
5 files changed, 119 insertions, 11 deletions
diff --git a/src/command/import.rs b/src/command/import.rs index 931e7af4..09df5839 100644 --- a/src/command/import.rs +++ b/src/command/import.rs @@ -7,7 +7,7 @@ use structopt::StructOpt; use atuin_client::database::Database; use atuin_client::history::History; -use atuin_client::import::Zsh; +use atuin_client::import::{bash::Bash, zsh::Zsh}; use indicatif::ProgressBar; #[derive(StructOpt)] @@ -23,11 +23,17 @@ pub enum Cmd { aliases=&["z", "zs"], )] Zsh, + + #[structopt( + about="import history from the bash history file", + aliases=&["b", "ba", "bas"], + )] + Bash, } impl Cmd { pub async fn run(&self, db: &mut (impl Database + Send + Sync)) -> Result<()> { - println!(" A'Tuin "); + println!(" Atuin "); println!("======================"); println!(" \u{1f30d} "); println!(" \u{1f418}\u{1f418}\u{1f418}\u{1f418} "); @@ -49,6 +55,7 @@ impl Cmd { } Self::Zsh => import_zsh(db).await, + Self::Bash => import_bash(db).await, } } } @@ -120,3 +127,61 @@ async fn import_zsh(db: &mut (impl Database + Send + Sync)) -> Result<()> { Ok(()) } + +// TODO: don't just copy paste this lol +async fn import_bash(db: &mut (impl Database + Send + Sync)) -> Result<()> { + // oh-my-zsh sets HISTFILE=~/.zhistory + // zsh has no default value for this var, but uses ~/.zhistory. + // we could maybe be smarter about this in the future :) + + let histpath = env::var("HISTFILE"); + + let histpath = if let Ok(p) = histpath { + let histpath = PathBuf::from(p); + + if !histpath.exists() { + return Err(eyre!( + "Could not find history file {:?}. try updating $HISTFILE", + histpath + )); + } + + histpath + } else { + let user_dirs = UserDirs::new().unwrap(); + let home_dir = user_dirs.home_dir(); + + home_dir.join(".bash_history") + }; + + let bash = Bash::new(histpath)?; + + let progress = ProgressBar::new(bash.loc); + + let buf_size = 100; + let mut buf = Vec::<History>::with_capacity(buf_size); + + for i in bash + .filter_map(Result::ok) + .filter(|x| !x.command.trim().is_empty()) + { + buf.push(i); + + if buf.len() == buf_size { + db.save_bulk(&buf).await?; + progress.inc(buf.len() as u64); + + buf.clear(); + } + } + + if !buf.is_empty() { + db.save_bulk(&buf).await?; + progress.inc(buf.len() as u64); + } + + progress.finish(); + println!("Import complete!"); + + Ok(()) +} diff --git a/src/command/init.rs b/src/command/init.rs index 022021d0..ed1555a9 100644 --- a/src/command/init.rs +++ b/src/command/init.rs @@ -1,19 +1,32 @@ use std::env; use eyre::{eyre, Result}; +use structopt::StructOpt; + +#[derive(StructOpt)] +pub enum Cmd { + #[structopt(about = "zsh setup")] + Zsh, + #[structopt(about = "bash setup")] + Bash, +} fn init_zsh() { let full = include_str!("../shell/atuin.zsh"); println!("{}", full); } -pub fn init() -> Result<()> { - let shell = env::var("SHELL")?; +fn init_bash() { + let full = include_str!("../shell/atuin.bash"); + println!("{}", full); +} - if shell.ends_with("zsh") { - init_zsh(); +impl Cmd { + pub fn run(&self) -> Result<()> { + match self { + Self::Zsh => init_zsh(), + Self::Bash => init_bash(), + } Ok(()) - } else { - Err(eyre!("Could not detect shell, or shell unsupported")) } } diff --git a/src/command/mod.rs b/src/command/mod.rs index 78e6402e..b16aae4d 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -37,7 +37,7 @@ pub enum AtuinCmd { Stats(stats::Cmd), #[structopt(about = "output shell setup")] - Init, + Init(init::Cmd), #[structopt(about = "generates a UUID")] Uuid, @@ -101,7 +101,7 @@ impl AtuinCmd { Self::Import(import) => import.run(&mut db).await, Self::Server(server) => server.run(&server_settings).await, Self::Stats(stats) => stats.run(&mut db, &client_settings).await, - Self::Init => init::init(), + Self::Init(init) => init.run(), Self::Search { cwd, exit, diff --git a/src/shell/atuin.bash b/src/shell/atuin.bash new file mode 100644 index 00000000..43de3640 --- /dev/null +++ b/src/shell/atuin.bash @@ -0,0 +1,30 @@ +_atuin_preexec() { + id=$(atuin history start "$1") + export ATUIN_HISTORY_ID="$id" +} + +_atuin_precmd() { + local EXIT="$?" + + [[ -z "${ATUIN_HISTORY_ID}" ]] && return + + + (RUST_LOG=error atuin history end $ATUIN_HISTORY_ID --exit $EXIT &) > /dev/null 2>&1 +} + + +__atuin_history () +{ + tput rmkx + HISTORY="$(RUST_LOG=error atuin search -i $BUFFER 3>&1 1>&2 2>&3)" + tput smkx + + READLINE_LINE=${HISTORY} + READLINE_POINT=${#READLINE_LINE} +} + + +preexec_functions+=(_atuin_preexec) +precmd_functions+=(_atuin_precmd) + +bind -x '"\C-r": __atuin_history' diff --git a/src/shell/atuin.zsh b/src/shell/atuin.zsh index cdef5e54..6a24de50 100644 --- a/src/shell/atuin.zsh +++ b/src/shell/atuin.zsh @@ -6,7 +6,7 @@ export ATUIN_HISTORY="atuin history list" export ATUIN_BINDKEYS="true" _atuin_preexec(){ - id=$(atuin history start $1) + id=$(atuin history start "$1") export ATUIN_HISTORY_ID="$id" } |