From 7ded21409fbb9fbf49354666c097adb2aa1e56e9 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Wed, 15 Jan 2025 12:49:22 +0000 Subject: [PATCH] Store fixed amount of lines --- components/monitors/cu_consolemon/src/lib.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/components/monitors/cu_consolemon/src/lib.rs b/components/monitors/cu_consolemon/src/lib.rs index ab8cad7ac..93cc16b1c 100644 --- a/components/monitors/cu_consolemon/src/lib.rs +++ b/components/monitors/cu_consolemon/src/lib.rs @@ -293,21 +293,28 @@ struct UI { } struct DebugLog { - debug_log: Mutex, + debug_log: Mutex>, + max_col: Mutex, } impl DebugLog { fn new() -> Self { Self { - debug_log: Mutex::new(String::new()), + debug_log: Mutex::new(Vec::new()), + max_col: Mutex::new(100), } } fn push_logs>(&self, logs: A) { - self.debug_log.lock().unwrap().push_str(logs.as_ref()) + let mut log_lines = self.debug_log.lock().unwrap(); + let max_col = *self.max_col.lock().unwrap(); + log_lines.push(logs.as_ref().to_string()); + if log_lines.len() > max_col as usize { + log_lines.remove(0); + } } - fn get_logs(&self) -> std::sync::MutexGuard<'_, String> { + fn get_logs(&self) -> std::sync::MutexGuard<'_, Vec> { self.debug_log.lock().unwrap() } } @@ -583,7 +590,7 @@ impl UI { self.debug_output.push_logs(&error_buffer); let debug_output = self.debug_output.get_logs(); - let p = Paragraph::new(debug_output.as_str()).block( + let p = Paragraph::new(debug_output.join("")).block( Block::default() .title(" Debug Output ") .borders(Borders::ALL), @@ -693,6 +700,8 @@ impl UI { } _ => {} } + } else if let Event::Resize(columns, _rows) = event::read()? { + *self.debug_output.max_col.lock().unwrap() = columns; } } }