use std::{fmt::Write, time::Duration};
use nu_ansi_term::Style;
use crate::styled;
pub trait FormatTime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result;
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result;
}
impl FormatTime for () {
fn format_time(&self, _w: &mut impl std::fmt::Write) -> std::fmt::Result {
Ok(())
}
fn style_timestamp(
&self,
_ansi: bool,
_elapsed: Duration,
_w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
Ok(())
}
}
#[cfg(feature = "time")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct UtcDateTime {
pub higher_precision: bool,
}
#[cfg(feature = "time")]
impl FormatTime for UtcDateTime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
let time = time::OffsetDateTime::now_utc();
write!(w, "{} {}", time.date(), time.time())
}
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
style_timestamp(ansi, self.higher_precision, elapsed, w)
}
}
#[cfg(feature = "time")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct LocalDateTime {
pub higher_precision: bool,
}
#[cfg(feature = "time")]
impl FormatTime for LocalDateTime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
let time = time::OffsetDateTime::now_local().expect("time offset cannot be determined");
write!(w, "{}", time)
}
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
style_timestamp(ansi, self.higher_precision, elapsed, w)
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Uptime {
epoch: std::time::Instant,
pub higher_precision: bool,
}
impl Default for Uptime {
fn default() -> Self {
Uptime::from(std::time::Instant::now())
}
}
impl From<std::time::Instant> for Uptime {
fn from(epoch: std::time::Instant) -> Self {
Uptime {
epoch,
higher_precision: false,
}
}
}
impl FormatTime for Uptime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
let e = self.epoch.elapsed();
write!(w, "{:4}.{:06}s", e.as_secs(), e.subsec_micros())
}
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
style_timestamp(ansi, self.higher_precision, elapsed, w)
}
}
fn style_timestamp(
ansi: bool,
higher_precision: bool,
elapsed: Duration,
w: &mut impl Write,
) -> std::fmt::Result {
if higher_precision {
format_timestamp_with_decimals(ansi, elapsed, w)
} else {
format_timestamp(ansi, elapsed, w)
}
}
fn format_timestamp(ansi: bool, elapsed: Duration, w: &mut impl Write) -> std::fmt::Result {
let millis = elapsed.as_millis();
let secs = elapsed.as_secs();
let (n, unit) = if millis < 1000 {
(millis as _, "ms")
} else if secs < 60 {
(secs, "s ")
} else {
(secs / 60, "m ")
};
let timestamp = format!("{n:>3}");
write_style_timestamp(ansi, timestamp, unit, w)
}
fn format_timestamp_with_decimals(
ansi: bool,
elapsed: Duration,
w: &mut impl Write,
) -> std::fmt::Result {
let secs = elapsed.as_secs_f64();
let (n, unit) = if secs < 0.001 {
(secs * 1_000_000.0, "μs")
} else if secs < 1.0 {
(secs * 1_000.0, "ms")
} else {
(secs, "s ")
};
let timestamp = format!(" {n:.2}");
write_style_timestamp(ansi, timestamp, unit, w)
}
fn write_style_timestamp(
ansi: bool,
timestamp: String,
unit: &str,
w: &mut impl Write,
) -> std::fmt::Result {
write!(
w,
"{timestamp}{unit}",
timestamp = styled(ansi, Style::new().dimmed(), timestamp),
unit = styled(ansi, Style::new().dimmed(), unit),
)
}
impl<F> FormatTime for &F
where
F: FormatTime,
{
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
F::format_time(self, w)
}
fn style_timestamp(
&self,
ansi: bool,
duration: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
F::style_timestamp(self, ansi, duration, w)
}
}