diff --git a/codex-rs/tui/src/status_indicator_widget.rs b/codex-rs/tui/src/status_indicator_widget.rs index 1e421acdcd..48a94db5e1 100644 --- a/codex-rs/tui/src/status_indicator_widget.rs +++ b/codex-rs/tui/src/status_indicator_widget.rs @@ -293,10 +293,7 @@ impl StatusIndicator<'_> { fn lines(&self, width: u16) -> Vec> { let row = self.row; let now = row.prepared_at.unwrap_or_else(Instant::now); - let elapsed_duration = self.timer.display_started_at.map_or_else( - || self.timer.elapsed_at(now), - |started_at| now.saturating_duration_since(started_at), - ); + let elapsed_duration = self.timer.display_elapsed_at(now); let pretty_elapsed = fmt_elapsed_compact(elapsed_duration.as_secs()); let progress = MotionMode::from_animations_enabled(row.animations_enabled && row.effects.progress); @@ -365,19 +362,16 @@ impl Renderable for StatusIndicator<'_> { if area.is_empty() { return; } - if self.row.animations_enabled || self.timer.display_started_at.is_some() { - let interval_ms = if self.row.animations_enabled - && (self.row.effects.progress - || (self.row.effects.shimmer - && !contains_rtl_or_arabic_number(self.row.header()))) - { - 32 - } else { - 1_000 - }; - self.row - .frame_requester - .schedule_frame_in(Duration::from_millis(interval_ms)); + let animated = self.row.animations_enabled + && (self.row.effects.progress + || (self.row.effects.shimmer && !contains_rtl_or_arabic_number(self.row.header()))); + let delay = if animated { + Some(Duration::from_millis(32)) + } else { + self.timer.next_tick_delay(Instant::now()) + }; + if let Some(delay) = delay { + self.row.frame_requester.schedule_frame_in(delay); } Paragraph::new(Text::from(self.lines(area.width))).render(area, buf); } @@ -412,19 +406,20 @@ mod tests { } #[test] - fn static_bidi_headers_only_schedule_clock_ticks_without_progress() { - for (header, progress, expected_ms) in [ - ("שלום עולם", false, 1_000), - ("Checking lines ١٢-٣٤", false, 1_000), - ("Working", false, 32), - ("שלום עולם", true, 32), + fn static_headers_schedule_clock_ticks_and_animated_headers_schedule_faster_frames() { + for (header, animations, progress, expected_ms) in [ + ("Working", false, true, 1_000), + ("שלום עולם", true, false, 1_000), + ("Checking lines ١٢-٣٤", true, false, 1_000), + ("Working", true, false, 32), + ("שלום עולם", true, true, 32), ] { let (tx, _rx) = unbounded_channel(); let (requester, mut requests) = FrameRequester::test_channel(); let mut row = StatusIndicatorWidget::new( AppEventSender::new(tx), requester, - /*animations_enabled*/ true, + animations, codex_config::types::TuiEffects { progress, shimmer: true, @@ -434,7 +429,7 @@ mod tests { row.update_header(header.to_owned()); let timer = StatusTimer::default(); let area = Rect::new(0, 0, 80, 1); - let before = Instant::now(); + let before = timer.last_resume_at; row.with_timer(&timer) .render(area, &mut Buffer::empty(area)); let after = Instant::now(); @@ -539,7 +534,7 @@ mod tests { Default::default(), ); let mut timer = StatusTimer::default(); - timer.pause_at(timer.last_resume_at); + timer.pause_at(timer.last_resume_at + Duration::from_secs(1)); let mut terminal = Terminal::new(TestBackend::new(80, 1)).expect("terminal"); terminal @@ -550,7 +545,7 @@ mod tests { .map(ratatui::buffer::Cell::symbol) .collect::(); - assert!(line.starts_with("Working (0s • esc to interrupt)")); + insta::assert_snapshot!(line.trim_end(), @"Working (1s • esc to interrupt)"); } #[test] diff --git a/codex-rs/tui/src/status_indicator_widget/timer.rs b/codex-rs/tui/src/status_indicator_widget/timer.rs index 8820e129be..6fa5b8fc15 100644 --- a/codex-rs/tui/src/status_indicator_widget/timer.rs +++ b/codex-rs/tui/src/status_indicator_widget/timer.rs @@ -25,6 +25,23 @@ impl Default for StatusTimer { } impl StatusTimer { + pub(super) fn display_elapsed_at(&self, now: Instant) -> Duration { + self.display_started_at.map_or_else( + || self.elapsed_at(now), + |started_at| now.saturating_duration_since(started_at), + ) + } + + /// Time until the displayed second changes, independent of decorative motion. + pub(super) fn next_tick_delay(&self, now: Instant) -> Option { + if self.is_paused && self.display_started_at.is_none() { + return None; + } + let elapsed = self.display_elapsed_at(now); + let fraction = Duration::from_nanos(u64::from(elapsed.subsec_nanos())); + Some(Duration::from_secs(1) - fraction) + } + pub(crate) fn reset(&mut self, elapsed: Duration) { self.elapsed_running = elapsed; self.last_resume_at = Instant::now(); diff --git a/codex-rs/tui/src/status_indicator_widget/timer_tests.rs b/codex-rs/tui/src/status_indicator_widget/timer_tests.rs index 25144c9102..cfcd844b9e 100644 --- a/codex-rs/tui/src/status_indicator_widget/timer_tests.rs +++ b/codex-rs/tui/src/status_indicator_widget/timer_tests.rs @@ -1,6 +1,21 @@ use super::*; use pretty_assertions::assert_eq; +#[test] +fn next_tick_tracks_the_displayed_clock_through_pauses_and_phase_overrides() { + let mut timer = StatusTimer::default(); + timer.pause_at(timer.last_resume_at + Duration::from_millis(250)); + let now = timer.last_resume_at + Duration::from_secs(10); + assert_eq!(timer.next_tick_delay(now), None); + + timer.display_started_at = Some(now - Duration::from_millis(800)); + assert_eq!(timer.next_tick_delay(now), Some(Duration::from_millis(200))); + + timer.display_started_at = None; + timer.resume_at(now); + assert_eq!(timer.next_tick_delay(now), Some(Duration::from_millis(750))); +} + #[test] fn pauses_and_resumes_without_counting_wait_time() { let baseline = Instant::now();