Skip to content

Commit ed37b70

Browse files
committed
Add millisecond threshold
1 parent e0d9d02 commit ed37b70

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

reffect/src/fmt.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,70 @@ use std::fmt;
22

33
#[derive(Debug, Clone, Copy)]
44
pub struct Time {
5-
value: u32,
6-
threshold: u32,
5+
millis: u32,
6+
min_threshold: u32,
7+
milli_threshold: u32,
78
}
89

910
impl Time {
10-
pub const DEFAULT_THRESHOLD: u32 = 60_000;
11+
pub const DEFAULT_MIN_THRESHOLD: u32 = 60_000;
12+
pub const DEFAULT_MILLI_THRESHOLD: u32 = 10_000;
1113
pub const SEC: u32 = 1000;
1214
pub const MIN: u32 = 60 * Self::SEC;
1315

1416
#[allow(unused)]
15-
pub const fn new(min: u32, sec: u32, ms: u32) -> Self {
16-
Self::with_threshold(min, sec, ms, Self::DEFAULT_THRESHOLD)
17+
pub const fn new(mins: u32, secs: u32, millis: u32) -> Self {
18+
Self::with_threshold(
19+
mins,
20+
secs,
21+
millis,
22+
Self::DEFAULT_MIN_THRESHOLD,
23+
Self::DEFAULT_MILLI_THRESHOLD,
24+
)
1725
}
1826

1927
#[allow(unused)]
20-
pub const fn with_threshold(min: u32, sec: u32, ms: u32, threshold: u32) -> Self {
28+
pub const fn with_threshold(
29+
mins: u32,
30+
secs: u32,
31+
millis: u32,
32+
min_threshold: u32,
33+
milli_threshold: u32,
34+
) -> Self {
2135
Self {
22-
value: Self::MIN * min + Self::SEC * sec + ms,
23-
threshold,
36+
millis: Self::MIN * mins + Self::SEC * secs + millis,
37+
min_threshold,
38+
milli_threshold,
2439
}
2540
}
2641

27-
pub fn format(value: u32, threshold: u32) -> String {
28-
Self { value, threshold }.to_string()
42+
#[allow(unused)]
43+
pub fn format(millis: u32, min_threshold: u32, milli_threshold: u32) -> String {
44+
Self {
45+
millis,
46+
min_threshold,
47+
milli_threshold,
48+
}
49+
.to_string()
2950
}
3051
}
3152

3253
impl fmt::Display for Time {
3354
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34-
let Self { value, threshold } = self;
35-
if value >= threshold {
36-
let min = value / Self::MIN;
37-
let secs = (value % Self::MIN) as f32 / Self::SEC as f32;
38-
write!(f, "{min}:{secs:0>4.1}")
55+
let Self {
56+
millis,
57+
min_threshold,
58+
milli_threshold,
59+
} = *self;
60+
61+
if millis >= min_threshold {
62+
let mins = millis / Self::MIN;
63+
let secs = (millis % Self::MIN) as f32 / Self::SEC as f32;
64+
write!(f, "{mins}:{secs:02.0}")
3965
} else {
40-
write!(f, "{value:.1}")
66+
let secs = millis as f32 / Self::SEC as f32;
67+
let prec = if millis >= milli_threshold { 0 } else { 1 };
68+
write!(f, "{secs:.prec$}")
4169
}
4270
}
4371
}

reffect/src/settings/general.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,34 @@ impl GeneralSettings {
5151
#[serde(default)]
5252
pub struct FormatSettings {
5353
pub minutes_threshold: u32,
54+
pub millis_threshold: u32,
5455
}
5556

5657
impl Default for FormatSettings {
5758
fn default() -> Self {
5859
Self {
5960
minutes_threshold: 60_000,
61+
millis_threshold: 10_000,
6062
}
6163
}
6264
}
6365

6466
impl FormatSettings {
6567
pub fn render_options(&mut self, ui: &Ui) {
66-
let Self { minutes_threshold } = self;
68+
let Self {
69+
minutes_threshold,
70+
millis_threshold,
71+
} = self;
6772

6873
input_seconds("Minute threshold", minutes_threshold);
6974
helper(ui, || {
70-
ui.text("Above how many seconds to display MM:SS format")
75+
ui.text("Above how many seconds to display MM:SS format");
76+
});
77+
78+
input_seconds("Millisecond threshold", millis_threshold);
79+
helper(ui, || {
80+
ui.text("Below how many seconds to display milliseconds");
81+
ui.text("MM:SS format always hides milliseconds");
7182
});
7283
}
7384
}

reffect/src/trigger/progress/active.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl ProgressActive {
284284

285285
fn duration_text(time: u32, settings: &FormatSettings) -> String {
286286
if time > 0 {
287-
Time::format(time, settings.minutes_threshold)
287+
Time::format(time, settings.minutes_threshold, settings.millis_threshold)
288288
} else {
289289
String::new()
290290
}

0 commit comments

Comments
 (0)