Skip to content

Commit

Permalink
Implement neutral delta times (#768)
Browse files Browse the repository at this point in the history
If the delta time is exactly 0, then it shouldn't have a sign at all and
be considered neither semantically ahead or behind, like a skipped
delta, and thus it gets assigned a neutral color (i.e. the text color).
  • Loading branch information
CryZe authored Feb 3, 2024
1 parent 1e1dd89 commit d8ba361
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/analysis/state_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ pub fn split_color(
) -> SemanticColor {
if show_best_segments && check_best_segment(timer, segment_index, method) {
SemanticColor::BestSegment
} else if let Some(time_difference) = time_difference {
} else if let Some(time_difference) = time_difference.filter(|t| t != &TimeSpan::zero()) {
let last_delta = segment_index
.checked_sub(1)
.and_then(|n| last_delta(timer.run(), n, comparison, method));
Expand Down
1 change: 1 addition & 0 deletions src/analysis/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod empty_run;
mod semantic_colors;
46 changes: 46 additions & 0 deletions src/analysis/tests/semantic_colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
analysis::split_color,
comparison,
settings::SemanticColor,
util::tests_helper::{
create_timer, make_progress_run_with_splits_opt, run_with_splits, span, start_run,
},
Timer, TimingMethod,
};

#[test]
fn segment_colors_are_correct() {
let mut timer = create_timer(&["A", "B"]);

run_with_splits(&mut timer, &[10.0, 20.0]);

start_run(&mut timer);
make_progress_run_with_splits_opt(&mut timer, &[Some(7.0)]);

assert_eq!(color(&timer, -2.5), SemanticColor::AheadLosingTime);
assert_eq!(color(&timer, -5.0), SemanticColor::AheadGainingTime);

assert_eq!(color(&timer, 0.0), SemanticColor::Default);

timer.reset(false);

start_run(&mut timer);
make_progress_run_with_splits_opt(&mut timer, &[Some(15.0)]);

assert_eq!(color(&timer, 2.5), SemanticColor::BehindGainingTime);
assert_eq!(color(&timer, 8.0), SemanticColor::BehindLosingTime);

assert_eq!(color(&timer, 0.0), SemanticColor::Default);
}

fn color(timer: &Timer, delta: f64) -> SemanticColor {
split_color(
timer,
Some(span(delta)),
1,
true,
false,
comparison::personal_best::NAME,
TimingMethod::GameTime,
)
}
41 changes: 39 additions & 2 deletions src/timing/formatter/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Inner {
/// * Hours without Decimal Dropping `+12:34:56.1`
/// * Hours with Decimal Dropping `+12:34:56`
/// * Negative Times `−23.1`
/// * Exactly zero `0.0`
pub struct Delta(bool, Accuracy);

impl Delta {
Expand Down Expand Up @@ -74,11 +75,14 @@ impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.time {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
let bit_or = total_seconds | nanoseconds as i64;
let (total_seconds, nanoseconds) = if bit_or < 0 {
f.write_str(MINUS)?;
((-total_seconds) as u64, (-nanoseconds) as u32)
} else {
f.write_str(PLUS)?;
if bit_or > 0 {
f.write_str(PLUS)?;
}
(total_seconds as u64, nanoseconds as u32)
};
// These are intentionally not data dependent, such that the CPU can
Expand Down Expand Up @@ -115,3 +119,36 @@ impl Display for Inner {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn sign_works() {
assert_eq!(
Delta::new()
.format(TimeSpan::from_seconds(-1.5))
.to_string(),
"−1.5"
);
assert_eq!(
Delta::new()
.format(TimeSpan::from_seconds(-0.5))
.to_string(),
"−0.5"
);

// We drop the sign entirely when it's exactly 0.
assert_eq!(Delta::new().format(TimeSpan::zero()).to_string(), "0.0");

assert_eq!(
Delta::new().format(TimeSpan::from_seconds(0.5)).to_string(),
"+0.5"
);
assert_eq!(
Delta::new().format(TimeSpan::from_seconds(1.5)).to_string(),
"+1.5"
);
}
}

0 comments on commit d8ba361

Please sign in to comment.