| 1 | //! Working and verification indicators from the shared terminal component kit. |
| 2 | //! |
| 3 | //! This replaces the Engine's duplicate frame tables, earned-marker delay and |
| 4 | //! frame indexing. The Engine still supplies clocks and motion policy, and its |
| 5 | //! existing backend performs ASCII/color adaptation. Quiet indicators use the |
| 6 | //! kit's semantic current-work mark rather than a frozen animation frame. |
| 7 | |
| 8 | use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; |
| 9 | |
| 10 | use codewhale_ratatui::{MotionMode, VerificationSpinner, spin}; |
| 11 | |
| 12 | #[cfg(any(test, feature = "test-support"))] |
| 13 | pub const BRAILLE_SPINNER_FRAMES: [&str; 8] = spin::FRAMES; |
| 14 | #[cfg(test)] |
| 15 | const VERIFY_TICK_FRAMES: [&str; 8] = VerificationSpinner::FRAMES; |
| 16 | pub const LIVE_MARKER_DELAY_MS: u64 = spin::EARN_DELAY.as_millis() as u64; |
| 17 | pub const LIVE_STATIC_MARKER: &str = spin::PENDING_FRAME; |
| 18 | pub const BRAILLE_SPINNER_STILL_FRAME: &str = spin::STILL_FRAME; |
| 19 | pub const BRAILLE_SPINNER_FRAME_MS: u64 = spin::FRAME_INTERVAL.as_millis() as u64; |
| 20 | |
| 21 | #[must_use] |
| 22 | pub fn braille_spinner_frame_for_elapsed_ms(elapsed_ms: u128, low_motion: bool) -> &'static str { |
| 23 | spin::frame(elapsed(elapsed_ms), motion(low_motion), false) |
| 24 | } |
| 25 | |
| 26 | #[must_use] |
| 27 | pub fn braille_spinner_frame(started_at: Option<Instant>, low_motion: bool) -> &'static str { |
| 28 | braille_spinner_frame_for_elapsed_ms(marker_elapsed_ms(started_at), low_motion) |
| 29 | } |
| 30 | |
| 31 | #[must_use] |
| 32 | pub fn verification_tick_frame(started_at: Option<Instant>, low_motion: bool) -> &'static str { |
| 33 | VerificationSpinner::frame( |
| 34 | elapsed(marker_elapsed_ms(started_at)), |
| 35 | motion(low_motion), |
| 36 | false, |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | fn motion(low_motion: bool) -> MotionMode { |
| 41 | if low_motion { |
| 42 | MotionMode::Reduced |
| 43 | } else { |
| 44 | MotionMode::Full |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn elapsed(milliseconds: u128) -> Duration { |
| 49 | Duration::from_millis(u64::try_from(milliseconds).unwrap_or(u64::MAX)) |
| 50 | } |
| 51 | |
| 52 | fn marker_elapsed_ms(started_at: Option<Instant>) -> u128 { |
| 53 | started_at.map_or_else( |
| 54 | || { |
| 55 | SystemTime::now() |
| 56 | .duration_since(UNIX_EPOCH) |
| 57 | .map_or(0, |duration| duration.as_millis()) |
| 58 | }, |
| 59 | |started| started.elapsed().as_millis(), |
| 60 | ) |
| 61 | } |
| 62 | |
| 63 | #[cfg(test)] |
| 64 | mod tests { |
| 65 | use super::*; |
| 66 | |
| 67 | #[test] |
| 68 | fn braille_spinner_advances_at_shared_cadence() { |
| 69 | // Assert cadence behavior against the frame table rather than specific |
| 70 | // glyphs so the whale-spout pattern can be retuned without churn here. |
| 71 | assert_eq!( |
| 72 | braille_spinner_frame_for_elapsed_ms(0, false), |
| 73 | LIVE_STATIC_MARKER |
| 74 | ); |
| 75 | assert_eq!( |
| 76 | braille_spinner_frame_for_elapsed_ms(u128::from(LIVE_MARKER_DELAY_MS) - 1, false), |
| 77 | LIVE_STATIC_MARKER |
| 78 | ); |
| 79 | assert_eq!( |
| 80 | braille_spinner_frame_for_elapsed_ms(u128::from(LIVE_MARKER_DELAY_MS), false), |
| 81 | BRAILLE_SPINNER_FRAMES[0] |
| 82 | ); |
| 83 | assert_eq!( |
| 84 | braille_spinner_frame_for_elapsed_ms( |
| 85 | u128::from(LIVE_MARKER_DELAY_MS + BRAILLE_SPINNER_FRAME_MS), |
| 86 | false, |
| 87 | ), |
| 88 | BRAILLE_SPINNER_FRAMES[1] |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn active_marker_uses_a_stable_five_hertz_wall_clock() { |
| 94 | assert_eq!(BRAILLE_SPINNER_FRAME_MS, 200); |
| 95 | for (index, frame) in BRAILLE_SPINNER_FRAMES.iter().enumerate() { |
| 96 | assert_eq!( |
| 97 | braille_spinner_frame_for_elapsed_ms( |
| 98 | u128::from(LIVE_MARKER_DELAY_MS) |
| 99 | + u128::from(BRAILLE_SPINNER_FRAME_MS) * index as u128, |
| 100 | false, |
| 101 | ), |
| 102 | *frame |
| 103 | ); |
| 104 | assert_eq!( |
| 105 | unicode_width::UnicodeWidthStr::width(*frame), |
| 106 | 1, |
| 107 | "active marker frames must never shift adjacent text" |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn working_swell_has_no_blank_flash_or_loop_seam() { |
| 114 | let dots: Vec<u32> = BRAILLE_SPINNER_FRAMES |
| 115 | .iter() |
| 116 | .map(|frame| (u32::from(frame.chars().next().unwrap()) - 0x2800).count_ones()) |
| 117 | .collect(); |
| 118 | for index in 0..dots.len() { |
| 119 | assert!((2..=6).contains(&dots[index])); |
| 120 | assert_eq!(dots[index].abs_diff(dots[(index + 1) % dots.len()]), 1); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn braille_spinner_respects_low_motion() { |
| 126 | assert_eq!( |
| 127 | braille_spinner_frame_for_elapsed_ms(u128::from(BRAILLE_SPINNER_FRAME_MS) * 3, true), |
| 128 | BRAILLE_SPINNER_STILL_FRAME |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn verification_tick_is_distinct_and_freezes_legibly() { |
| 134 | let start = Instant::now() - std::time::Duration::from_millis(LIVE_MARKER_DELAY_MS); |
| 135 | assert_eq!( |
| 136 | verification_tick_frame(Some(start), false), |
| 137 | VERIFY_TICK_FRAMES[0] |
| 138 | ); |
| 139 | assert_eq!( |
| 140 | verification_tick_frame(Some(start), true), |
| 141 | BRAILLE_SPINNER_STILL_FRAME |
| 142 | ); |
| 143 | assert_ne!(VERIFY_TICK_FRAMES[0], BRAILLE_SPINNER_FRAMES[0]); |
| 144 | } |
| 145 | } |
| 146 |