Preserve current position when new items are added to a buffer
All checks were successful
CI / lint (push) Successful in 5m3s
CI / Build and test (, 1.73.0) (push) Successful in 9m34s
CI / Build and test (, beta) (push) Successful in 9m34s
CI / Build and test (, nightly) (push) Successful in 8m46s

This commit is contained in:
2023-11-17 14:14:24 +01:00
parent 9b29d4d9e5
commit dd404147ab
5 changed files with 291 additions and 67 deletions

View File

@ -32,7 +32,7 @@ use crate::widgets::Prerender;
const MAX_MEM_LOG_LINES: usize = 1000;
pub struct LogBuffer {
lines: VecDeque<(String, Prerender)>,
lines: VecDeque<(u64, String, Prerender)>,
receiver: UnboundedReceiver<String>,
}
@ -73,17 +73,29 @@ impl Buffer for LogBuffer {
if self.lines.len() >= MAX_MEM_LOG_LINES {
self.lines.pop_front();
}
self.lines.push_back((line, Prerender::new()));
let line_id = self
.lines
.back()
.map(|(last_id, _, _)| last_id + 1)
.unwrap_or(0);
self.lines.push_back((line_id, line, Prerender::new()));
}
fn content<'a>(&'a self) -> Box<dyn ExactSizeIterator<Item = BufferItem<'a>> + 'a> {
use ansi_to_tui::IntoText;
Box::new(self.lines.iter().rev().map(|(line, prerender)| BufferItem {
content: BufferItemContent::Text(line.clone().into_text().unwrap_or_else(|e| {
tracing::error!("Could not convert line from ANSI codes to ratatui: {}", e);
Text::raw(line)
})),
prerender,
}))
Box::new(
self
.lines
.iter()
.rev()
.map(|(line_id, line, prerender)| BufferItem {
content: BufferItemContent::Text(line.clone().into_text().unwrap_or_else(|e| {
tracing::error!("Could not convert line from ANSI codes to ratatui: {}", e);
Text::raw(line)
})),
prerender,
unique_id: Some(*line_id),
}),
)
}
}