Make buffers return their items lazily from the end instead of materializing them

This commit is contained in:
2023-11-04 20:14:51 +01:00
parent d5e5639ba7
commit 46b703ba45
4 changed files with 37 additions and 28 deletions

View File

@ -24,7 +24,7 @@ use tokio::sync::mpsc::UnboundedReceiver;
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;
use super::Buffer;
use super::{Buffer, BufferItem};
/// Maximum number of log lines to be stored in memory
const MAX_MEM_LOG_LINES: usize = 100;
@ -61,19 +61,21 @@ impl Buffer for LogBuffer {
self.lines.push_back(line);
}
fn content(&self) -> Vec<Text> {
fn content<'a>(&'a self) -> Box<dyn Iterator<Item = BufferItem<'a>> + 'a> {
use ansi_to_tui::IntoText;
let (slice1, slice2) = self.lines.as_slices();
slice1
.into_iter()
.chain(slice2.into_iter())
.cloned()
.map(|line| {
line.into_text().unwrap_or_else(|e| {
tracing::error!("Could not convert line from ANSI codes to ratatui: {}", e);
Text::raw(line)
})
})
.collect()
Box::new(
slice1
.into_iter()
.chain(slice2.into_iter())
.rev()
.cloned()
.map(|line| BufferItem {
text: line.into_text().unwrap_or_else(|e| {
tracing::error!("Could not convert line from ANSI codes to ratatui: {}", e);
Text::raw(line)
}),
}),
)
}
}