1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::time::SystemTime;
use nu_ansi_term::Style;
use crate::output::cell::TextCell;
use crate::output::time::TimeFormat;
use crate::theme::{DateAge, age_to_position};
pub trait Render {
/// Render a timestamp into a `TextCell`.
///
/// `date_styles` carries the six discrete per-tier colours.
/// `smooth_lut`, if `Some`, is a reference to the 256-stop
/// interpolated LUT produced by `apply_gradient_flags`:
/// when present, it overrides the discrete tier lookup with
/// a position-based smooth colour.
fn render(
self,
date_styles: &DateAge,
smooth_lut: Option<&[Style; 256]>,
format: &TimeFormat,
) -> TextCell;
}
impl Render for Option<SystemTime> {
fn render(
self,
date_styles: &DateAge,
smooth_lut: Option<&[Style; 256]>,
format: &TimeFormat,
) -> TextCell {
let (datestamp, style) = if let Some(time) = self {
let age_secs = SystemTime::now()
.duration_since(time)
.map(|d| d.as_secs())
.unwrap_or(0);
let style = if let Some(lut) = smooth_lut {
let position = age_to_position(age_secs);
let bucket = (position * 255.0).round() as usize;
lut[bucket.min(255)]
} else {
date_styles.for_age(age_secs)
};
(format.format(time), style)
} else {
// Missing timestamp: always use the discrete `old`
// colour, even in smooth mode — the file has no
// meaningful position on the gradient.
(String::from("-"), date_styles.old)
};
TextCell::paint(style, datestamp)
}
}