aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/client/search/duration.rs
diff options
context:
space:
mode:
authorBaptiste <32563450+BapRx@users.noreply.github.com>2023-01-26 11:57:52 +0100
committerGitHub <noreply@github.com>2023-01-26 10:57:52 +0000
commit893a395f12d29b6b6676db3672fb7e600c49aff7 (patch)
tree2944bc8b07ec3dd44eca75fa411a5dc1d93fa1c6 /src/command/client/search/duration.rs
parentUpdate contributors.rs (diff)
downloadatuin-893a395f12d29b6b6676db3672fb7e600c49aff7.zip
feat(history): Add new flag to allow custom output format (#662)
* feat(history): Add new flag to allow custom output format * more efficient formatting * add user and host * docs Co-authored-by: Conrad Ludgate <conrad.ludgate@truelayer.com>
Diffstat (limited to '')
-rw-r--r--src/command/client/search/duration.rs26
1 files changed, 19 insertions, 7 deletions
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()
}