Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cortex-tui/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ use cortex_tui_capture::{
};
use ratatui::buffer::Buffer as RatatuiBuffer;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use tracing::{debug, info, warn};

/// Minimum interval between frame captures when capture_all is enabled (1 second)
const MIN_CAPTURE_INTERVAL_SECS: u64 = 1;

/// Static frame counter for unique frame labeling
static FRAME_COUNTER: AtomicU64 = AtomicU64::new(0);

Expand All @@ -54,6 +58,8 @@ pub struct TuiCapture {
modal_open: bool,
/// Last captured event description
last_event: String,
/// Last time a frame was captured (for rate limiting capture_all mode)
last_capture_time: Option<Instant>,
}

impl TuiCapture {
Expand All @@ -80,6 +86,7 @@ impl TuiCapture {
autocomplete_visible: false,
modal_open: false,
last_event: String::new(),
last_capture_time: None,
}
}

Expand Down Expand Up @@ -201,6 +208,9 @@ impl TuiCapture {
///
/// This should be called after each render. It captures the current
/// buffer state as ASCII art.
///
/// When `capture_all` is enabled, frames are rate-limited to at most
/// one per second to avoid excessive logging and storage.
pub fn capture_frame(&mut self, buffer: &RatatuiBuffer, label: Option<&str>) {
if let Some(ref mut manager) = self.inner {
let snapshot = BufferSnapshot::from_ratatui_buffer(buffer);
Expand All @@ -212,6 +222,15 @@ impl TuiCapture {
let event = std::mem::take(&mut self.last_event);
format!("{} - {}", self.current_view, event)
} else if self.capture_all {
// Rate limit capture_all frames to max 1 per second
let now = Instant::now();
if let Some(last_time) = self.last_capture_time {
if now.duration_since(last_time).as_secs() < MIN_CAPTURE_INTERVAL_SECS {
// Skip this frame - not enough time has passed
return;
}
}
self.last_capture_time = Some(now);
let frame_num = FRAME_COUNTER.fetch_add(1, Ordering::SeqCst);
format!("{} - frame {}", self.current_view, frame_num)
} else {
Expand Down
Loading