| 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 |
use std::time::{Duration, SystemTime}; |
| 34 |
|
| 35 |
use makeover_timing::notice_lifetime; |
| 36 |
use quasi_router::Clock; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
pub const TICK: Duration = Duration::from_secs(1); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
pub const COARSE: Duration = Duration::from_secs(30); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
pub const LINGER: Duration = match notice_lifetime(true) { |
| 75 |
Some(lifetime) => lifetime, |
| 76 |
|
| 77 |
|
| 78 |
None => panic!("a transient notice has a lifetime"), |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
#[must_use] |
| 83 |
pub const fn cadence(clock: Clock) -> Duration { |
| 84 |
match clock { |
| 85 |
Clock::Since | Clock::Until => TICK, |
| 86 |
Clock::Age => COARSE, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
#[must_use] |
| 92 |
pub(crate) fn text(clock: Clock, at: SystemTime, now: SystemTime) -> String { |
| 93 |
match clock { |
| 94 |
Clock::Since => face(now.duration_since(at).unwrap_or_default()), |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
Clock::Until => face(at.duration_since(now).unwrap_or_default()), |
| 100 |
Clock::Age => ago(now.duration_since(at).unwrap_or_default()), |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
fn face(span: Duration) -> String { |
| 109 |
let secs = span.as_secs(); |
| 110 |
format!("{}:{:02}:{:02}", secs / 3600, (secs % 3600) / 60, secs % 60) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
fn ago(span: Duration) -> String { |
| 115 |
let secs = span.as_secs(); |
| 116 |
if secs < 60 { |
| 117 |
"just now".to_owned() |
| 118 |
} else if secs < 3600 { |
| 119 |
format!("{}m ago", secs / 60) |
| 120 |
} else if secs < 86_400 { |
| 121 |
format!("{}h ago", secs / 3600) |
| 122 |
} else { |
| 123 |
format!("{}d ago", secs / 86_400) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[cfg(test)] |
| 128 |
mod tests { |
| 129 |
use super::*; |
| 130 |
|
| 131 |
fn at(seconds: u64) -> SystemTime { |
| 132 |
SystemTime::UNIX_EPOCH + Duration::from_secs(seconds) |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn a_stopwatch_counts_up_and_a_countdown_counts_down() { |
| 137 |
assert_eq!(text(Clock::Since, at(0), at(3845)), "1:04:05"); |
| 138 |
assert_eq!(text(Clock::Until, at(3845), at(0)), "1:04:05"); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn a_readout_that_has_run_out_reads_as_zero_rather_than_backwards() { |
| 143 |
assert_eq!(text(Clock::Until, at(0), at(90)), "0:00:00"); |
| 144 |
assert_eq!(text(Clock::Since, at(90), at(0)), "0:00:00"); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn a_stamp_is_coarse_and_a_stopwatch_is_not() { |
| 149 |
assert_eq!(text(Clock::Age, at(0), at(30)), "just now"); |
| 150 |
assert_eq!(text(Clock::Age, at(0), at(300)), "5m ago"); |
| 151 |
assert_eq!(text(Clock::Age, at(0), at(10_800)), "3h ago"); |
| 152 |
assert_eq!(text(Clock::Age, at(0), at(345_600)), "4d ago"); |
| 153 |
assert_eq!(text(Clock::Since, at(0), at(30)), "0:00:30"); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn a_toast_takes_its_lifetime_from_the_named_intent() { |
| 158 |
assert_eq!(LINGER, makeover_timing::Intent::Dismiss.duration()); |
| 159 |
assert_eq!(notice_lifetime(true), Some(LINGER)); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn a_banner_has_no_lifetime_rather_than_a_long_one() { |
| 164 |
assert_eq!(notice_lifetime(false), None); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn the_cadence_follows_the_granularity_the_spelling_chose() { |
| 169 |
assert_eq!(cadence(Clock::Since), TICK); |
| 170 |
assert_eq!(cadence(Clock::Until), TICK); |
| 171 |
assert_eq!(cadence(Clock::Age), COARSE); |
| 172 |
assert!(COARSE > TICK); |
| 173 |
} |
| 174 |
} |
| 175 |
|