Add frequent exit checks throughout render loop

The issue was that sleep() was blocking for 16-33ms without checking
the exit flag. Now:
- Check should_exit at 5 points in the render loop
- Break sleep into 5ms chunks, checking between each chunk
- This gives <5ms response time to exit commands

Exit responds within 5ms: Press 'q', ESC, or Ctrl+C

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-09 07:03:57 +01:00
parent 4e88ea5677
commit 946289544d

View File

@@ -65,17 +65,22 @@ impl<'a> Renderer<'a> {
});
loop {
let frame_start = std::time::Instant::now();
// Check if user requested exit
// Check for exit FIRST
if should_exit.load(Ordering::Relaxed) {
break;
}
let frame_start = std::time::Instant::now();
// Calculate progress with easing
let linear_progress = timeline.progress();
let eased_progress = self.easing.ease(linear_progress);
// Check again before rendering
if should_exit.load(Ordering::Relaxed) {
break;
}
// Apply effect
let effect_result = self.effect.apply(self.ascii_art, eased_progress);
@@ -86,6 +91,11 @@ impl<'a> Renderer<'a> {
effect_result.text.clone()
};
// Check before terminal operations
if should_exit.load(Ordering::Relaxed) {
break;
}
// Render to terminal
terminal.clear()?;
terminal.refresh_size()?;
@@ -117,6 +127,11 @@ impl<'a> Renderer<'a> {
}
}
// Check if user wants to exit
if should_exit.load(Ordering::Relaxed) {
break;
}
// Check if animation is complete before advancing
if timeline.is_complete() {
break;
@@ -128,7 +143,19 @@ impl<'a> Renderer<'a> {
let elapsed = frame_start.elapsed();
if elapsed < frame_duration {
sleep(frame_duration - elapsed).await;
let sleep_duration = frame_duration - elapsed;
// Break sleep into small chunks to check should_exit frequently
let chunk_duration = Duration::from_millis(5);
let mut remaining = sleep_duration;
while remaining > Duration::ZERO {
if should_exit.load(Ordering::Relaxed) {
break;
}
let sleep_time = remaining.min(chunk_duration);
sleep(sleep_time).await;
remaining = remaining.saturating_sub(sleep_time);
}
}
}