diff options
Diffstat (limited to '')
| -rw-r--r-- | src/command/client/search.rs | 11 | ||||
| -rw-r--r-- | src/command/client/search/duration.rs | 26 |
2 files changed, 28 insertions, 9 deletions
diff --git a/src/command/client/search.rs b/src/command/client/search.rs index e528576d..53471ec1 100644 --- a/src/command/client/search.rs +++ b/src/command/client/search.rs @@ -16,7 +16,7 @@ mod duration; mod event; mod history_list; mod interactive; -pub use duration::format_duration; +pub use duration::{format_duration, format_duration_into}; #[allow(clippy::struct_excessive_bools)] #[derive(Parser)] @@ -74,6 +74,11 @@ pub struct Cmd { /// Show only the text of the command #[arg(long)] cmd_only: bool, + + /// Available variables: {command}, {directory}, {duration}, {user}, {host} and {time}. + /// Example: --format "{time} - [{duration}] - {directory}$\t{command}" + #[arg(long, short)] + format: Option<String>, } impl Cmd { @@ -97,6 +102,7 @@ impl Cmd { self.exit, self.exclude_exit, self.exclude_cwd, + self.format, self.before, self.after, self.limit, @@ -122,6 +128,7 @@ async fn run_non_interactive( exit: Option<i64>, exclude_exit: Option<i64>, exclude_cwd: Option<String>, + format: Option<String>, before: Option<String>, after: Option<String>, limit: Option<i64>, @@ -202,6 +209,6 @@ async fn run_non_interactive( .map(std::borrow::ToOwned::to_owned) .collect(); - super::history::print_list(&results, list_mode); + super::history::print_list(&results, list_mode, format.as_deref()); Ok(results.len()) } diff --git a/src/command/client/search/duration.rs b/src/command/client/search/duration.rs index 1dc4245f..08dadb95 100644 --- a/src/command/client/search/duration.rs +++ b/src/command/client/search/duration.rs @@ -1,10 +1,11 @@ +use core::fmt; use std::{ops::ControlFlow, time::Duration}; #[allow(clippy::module_name_repetitions)] -pub fn format_duration(f: Duration) -> String { - fn item(name: &str, value: u64) -> ControlFlow<String> { +pub fn format_duration_into(dur: Duration, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn item(unit: &'static str, value: u64) -> ControlFlow<(&'static str, u64)> { if value > 0 { - ControlFlow::Break(format!("{value}{name}")) + ControlFlow::Break((unit, value)) } else { ControlFlow::Continue(()) } @@ -13,7 +14,7 @@ pub fn format_duration(f: Duration) -> String { // impl taken and modified from // https://github.com/tailhook/humantime/blob/master/src/duration.rs#L295-L331 // Copyright (c) 2016 The humantime Developers - fn fmt(f: Duration) -> ControlFlow<String, ()> { + fn fmt(f: Duration) -> ControlFlow<(&'static str, u64), ()> { let secs = f.as_secs(); let nanos = f.subsec_nanos(); @@ -43,8 +44,19 @@ pub fn format_duration(f: Duration) -> String { ControlFlow::Continue(()) } - match fmt(f) { - ControlFlow::Break(b) => b, - ControlFlow::Continue(()) => String::from("0s"), + match fmt(dur) { + ControlFlow::Break((unit, value)) => write!(f, "{value}{unit}"), + ControlFlow::Continue(()) => write!(f, "0s"), + } +} + +#[allow(clippy::module_name_repetitions)] +pub fn format_duration(f: Duration) -> String { + struct F(Duration); + impl fmt::Display for F { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + format_duration_into(self.0, f) + } } + F(f).to_string() } |
