use ratatui::text::Text;
use crate::logger::LogRecord;
use crate::ui::log_buffer::LogBuffer;
pub(super) struct LogView {
buffer: LogBuffer,
cursor: usize,
unread: usize,
}
impl LogView {
pub(super) fn new(buffer_size: usize) -> Self {
Self {
buffer: LogBuffer::new(buffer_size),
cursor: 0,
unread: 0,
}
}
pub(super) fn push(&mut self, record: LogRecord, width: u16, verbose: bool) {
if self.cursor != 0 {
self.cursor = self
.cursor
.saturating_add(record.format_for_term(width, verbose).len());
self.unread += 1;
}
self.buffer.push(record);
}
pub(super) fn scroll_up(&mut self, n: usize) {
self.cursor = self.cursor.saturating_add(n);
}
pub(super) fn scroll_down(&mut self, n: usize) {
self.cursor = self.cursor.saturating_sub(n);
if self.cursor == 0 {
self.unread = 0;
}
}
pub(super) fn reset_scroll(&mut self) {
self.cursor = 0;
self.unread = 0;
}
pub(super) fn clear(&mut self) {
self.buffer.clear();
self.cursor = 0;
self.unread = 0;
}
pub(super) fn format(&mut self, width: u16, height: u16, verbose: bool) -> Text<'static> {
let (text, clamped) = self.buffer.to_text(width, height, self.cursor, verbose);
self.cursor = clamped;
text
}
pub(super) fn unread(&self) -> usize {
self.unread
}
}