diff --git a/Cargo.lock b/Cargo.lock index b8cf2bde..df2c1bfb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,7 +90,6 @@ dependencies = [ "indicatif", "itertools", "log", - "owo-colors", "pretty_env_logger", "serde", "serde_json", @@ -1381,12 +1380,6 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" -[[package]] -name = "owo-colors" -version = "3.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b" - [[package]] name = "parking_lot" version = "0.11.2" diff --git a/Cargo.toml b/Cargo.toml index 51302f23..2b77a8c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,7 +65,6 @@ clap = { version = "3.1.11", features = ["derive"] } clap_complete = "3.1.2" fs-err = "2.7" whoami = "1.1.2" -owo-colors = "3.4.0" [dependencies.tracing-subscriber] version = "0.3" diff --git a/src/command/client/history.rs b/src/command/client/history.rs index 7cbe27f3..6e265e30 100644 --- a/src/command/client/history.rs +++ b/src/command/client/history.rs @@ -11,7 +11,6 @@ use atuin_client::settings::Settings; #[cfg(feature = "sync")] use atuin_client::sync; -use owo_colors::OwoColorize; #[derive(Subcommand)] #[clap(infer_subcommands = true)] @@ -53,17 +52,34 @@ pub enum Cmd { }, } +#[derive(Clone, Copy, Debug)] +pub enum ListMode { + Human, + CmdOnly, + Regular, +} + +impl ListMode { + pub const fn from_flags(human: bool, cmd_only: bool) -> Self { + if human { + ListMode::Human + } else if cmd_only { + ListMode::CmdOnly + } else { + ListMode::Regular + } + } +} + #[allow(clippy::cast_sign_loss)] -pub fn print_list(h: &[History], human: bool, cmd_only: bool) { +pub fn print_list(h: &[History], list_mode: ListMode) { let w = std::io::stdout(); let mut w = w.lock(); - if human { - print_human_list(&mut w, h); - } else if cmd_only { - print_cmd_only(&mut w, h); - } else { - print_basic(&mut w, h); + match list_mode { + ListMode::Human => print_human_list(&mut w, h), + ListMode::CmdOnly => print_cmd_only(&mut w, h), + ListMode::Regular => print_regular(&mut w, h), } w.flush().expect("failed to flush history"); @@ -81,19 +97,12 @@ pub fn print_human_list(w: &mut StdoutLock, h: &[History]) { let time = h.timestamp.format("%Y-%m-%d %H:%M:%S"); let cmd = h.command.trim(); - let exit_color = if h.success() { - owo_colors::AnsiColors::Green - } else { - owo_colors::AnsiColors::Red - }; - let duration = duration.color(exit_color); - writeln!(w, "{time} ยท {duration}\t{cmd}").expect("failed to write history"); } } #[allow(clippy::cast_sign_loss)] -pub fn print_basic(w: &mut StdoutLock, h: &[History]) { +pub fn print_regular(w: &mut StdoutLock, h: &[History]) { for h in h.iter().rev() { let duration = humantime::format_duration(Duration::from_nanos(std::cmp::max(h.duration, 0) as u64)) @@ -216,14 +225,14 @@ impl Cmd { } }; - print_list(&history, *human, *cmd_only); + print_list(&history, ListMode::from_flags(*human, *cmd_only)); Ok(()) } Self::Last { human, cmd_only } => { let last = db.last().await?; - print_list(&[last], *human, *cmd_only); + print_list(&[last], ListMode::from_flags(*human, *cmd_only)); Ok(()) } diff --git a/src/command/client/search.rs b/src/command/client/search.rs index 5f51a664..45b1f978 100644 --- a/src/command/client/search.rs +++ b/src/command/client/search.rs @@ -23,6 +23,7 @@ use atuin_client::{ }; use super::event::{Event, Events}; +use super::history::ListMode; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -73,21 +74,33 @@ impl Cmd { db: &mut (impl Database + Send + Sync), settings: &Settings, ) -> Result<()> { - run( - settings, - self.cwd, - self.exit, - self.interactive, - self.human, - self.exclude_exit, - self.exclude_cwd, - self.before, - self.after, - self.cmd_only, - &self.query, - db, - ) - .await + if self.interactive { + let item = select_history( + &self.query, + settings.search_mode, + settings.filter_mode, + settings.style, + db, + ) + .await?; + eprintln!("{}", item); + } else { + let list_mode = ListMode::from_flags(self.human, self.cmd_only); + run_non_interactive( + settings, + list_mode, + self.cwd, + self.exit, + self.exclude_exit, + self.exclude_cwd, + self.before, + self.after, + &self.query, + db, + ) + .await?; + }; + Ok(()) } } @@ -565,117 +578,98 @@ async fn select_history( // This is supposed to more-or-less mirror the command line version, so ofc // it is going to have a lot of args #[allow(clippy::too_many_arguments)] -pub async fn run( +async fn run_non_interactive( settings: &Settings, + list_mode: ListMode, cwd: Option, exit: Option, - interactive: bool, - human: bool, exclude_exit: Option, exclude_cwd: Option, before: Option, after: Option, - cmd_only: bool, query: &[String], db: &mut (impl Database + Send + Sync), ) -> Result<()> { - let dir = if let Some(cwd) = cwd { - if cwd == "." { - let current = std::env::current_dir()?; - let current = current.as_os_str(); - let current = current.to_str().unwrap(); + let dir = if cwd.as_deref() == Some(".") { + let current = std::env::current_dir()?; + let current = current.as_os_str(); + let current = current.to_str().unwrap(); - Some(current.to_owned()) - } else { - Some(cwd) - } + Some(current.to_owned()) } else { - None + cwd }; - if interactive { - let item = select_history( - query, + let context = current_context(); + + let results = db + .search( + None, settings.search_mode, settings.filter_mode, - settings.style, - db, + &context, + query.join(" ").as_str(), ) .await?; - eprintln!("{}", item); - } else { - let context = current_context(); - let results = db - .search( - None, - settings.search_mode, - settings.filter_mode, - &context, - query.join(" ").as_str(), - ) - .await?; - - // TODO: This filtering would be better done in the SQL query, I just - // need a nice way of building queries. - let results: Vec = results - .iter() - .filter(|h| { - if let Some(exit) = exit { - if h.exit != exit { - return false; - } + // TODO: This filtering would be better done in the SQL query, I just + // need a nice way of building queries. + let results: Vec = results + .iter() + .filter(|h| { + if let Some(exit) = exit { + if h.exit != exit { + return false; } + } - if let Some(exit) = exclude_exit { - if h.exit == exit { - return false; - } + if let Some(exit) = exclude_exit { + if h.exit == exit { + return false; } + } - if let Some(cwd) = &exclude_cwd { - if h.cwd.as_str() == cwd.as_str() { - return false; - } + if let Some(cwd) = &exclude_cwd { + if h.cwd.as_str() == cwd.as_str() { + return false; } + } - if let Some(cwd) = &dir { - if h.cwd.as_str() != cwd.as_str() { - return false; - } + if let Some(cwd) = &dir { + if h.cwd.as_str() != cwd.as_str() { + return false; } + } - if let Some(before) = &before { - let before = chrono_english::parse_date_string( - before.as_str(), - Utc::now(), - chrono_english::Dialect::Uk, - ); + if let Some(before) = &before { + let before = chrono_english::parse_date_string( + before.as_str(), + Utc::now(), + chrono_english::Dialect::Uk, + ); - if before.is_err() || h.timestamp.gt(&before.unwrap()) { - return false; - } + if before.is_err() || h.timestamp.gt(&before.unwrap()) { + return false; } + } - if let Some(after) = &after { - let after = chrono_english::parse_date_string( - after.as_str(), - Utc::now(), - chrono_english::Dialect::Uk, - ); + if let Some(after) = &after { + let after = chrono_english::parse_date_string( + after.as_str(), + Utc::now(), + chrono_english::Dialect::Uk, + ); - if after.is_err() || h.timestamp.lt(&after.unwrap()) { - return false; - } + if after.is_err() || h.timestamp.lt(&after.unwrap()) { + return false; } + } - true - }) - .map(std::borrow::ToOwned::to_owned) - .collect(); - - super::history::print_list(&results, human, cmd_only); - } + true + }) + .map(std::borrow::ToOwned::to_owned) + .collect(); + super::history::print_list(&results, list_mode); Ok(()) }