use super::output::LogLine;
use std::{collections::VecDeque, sync::Arc};
pub const DEFAULT_LIMIT: usize = 5_000;
pub struct OutputBuffer {
pub lines: VecDeque<Arc<LogLine>>,
limit: usize,
first: u64,
pub anchor: Option<u64>,
pub horizontal: u16,
pub query: String,
pub found: Option<u64>,
}
impl OutputBuffer {
pub fn new(limit: usize) -> Self {
Self {
lines: VecDeque::new(),
limit: limit.max(1),
first: 0,
anchor: None,
horizontal: 0,
query: String::new(),
found: None,
}
}
pub fn push(&mut self, line: Arc<LogLine>) {
if self.lines.len() == self.limit {
self.lines.pop_front();
self.first += 1;
if let Some(anchor) = &mut self.anchor {
*anchor = (*anchor).max(self.first);
}
if self.found.is_some_and(|n| n < self.first) {
self.found = None;
}
}
self.lines.push_back(line);
}
pub fn start(&self, height: usize) -> usize {
self.anchor
.map(|n| n.saturating_sub(self.first) as usize)
.unwrap_or_else(|| self.bottom(height))
.min(self.bottom(height))
}
fn bottom(&self, height: usize) -> usize {
self.lines.len().saturating_sub(height.max(1))
}
pub fn scroll(&mut self, delta: isize, height: usize) {
let start = self
.start(height)
.saturating_add_signed(delta)
.min(self.bottom(height));
self.anchor = (start < self.bottom(height)).then_some(self.first + start as u64);
}
pub fn top(&mut self) {
self.anchor = Some(self.first);
}
pub fn follow(&mut self) {
self.anchor = None;
}
pub fn clear(&mut self) {
self.first += self.lines.len() as u64;
self.lines.clear();
self.horizontal = 0;
self.anchor = None;
self.found = None;
}
pub fn search(&mut self, forward: bool, fresh: bool) -> bool {
if self.query.is_empty() || self.lines.is_empty() {
self.found = None;
return false;
}
let query = self.query.to_lowercase();
let len = self.lines.len();
let start = if fresh {
if forward {
len - 1
} else {
0
}
} else {
self.found
.or(self.anchor)
.unwrap_or(self.first)
.saturating_sub(self.first) as usize
% len
};
for offset in 1..=len {
let index = if forward {
(start + offset) % len
} else {
(start + len - offset) % len
};
if self.lines[index].text.to_lowercase().contains(&query) {
let number = self.first + index as u64;
self.found = Some(number);
self.anchor = Some(number);
return true;
}
}
self.found = None;
false
}
pub fn is_found(&self, index: usize) -> bool {
self.found == Some(self.first + index as u64)
}
pub fn matches(&self, text: &str) -> bool {
!self.query.is_empty() && text.to_lowercase().contains(&self.query.to_lowercase())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dev::{events::OutputStream, output::Decoder};
fn line(text: &str) -> Arc<LogLine> {
Arc::new(Decoder::default().line(0, OutputStream::Stdout, text))
}
#[test]
fn bounded_buffer_drops_oldest_and_preserves_paused_anchor() {
let mut b = OutputBuffer::new(3);
for text in ["a", "b", "c"] {
b.push(line(text));
}
b.scroll(-1, 1);
b.push(line("d"));
assert_eq!(
b.lines.iter().map(|l| l.text.as_str()).collect::<Vec<_>>(),
["b", "c", "d"]
);
assert_eq!(b.lines[b.start(1)].text, "b");
b.push(line("e"));
assert_eq!(b.lines[b.start(1)].text, "c");
}
#[test]
fn following_resumes_only_after_scrolling_to_bottom() {
let mut b = OutputBuffer::new(20);
for _ in 0..10 {
b.push(line("x"));
}
assert_eq!(b.start(3), 7);
b.scroll(-2, 3);
b.push(line("new"));
assert_eq!(b.start(3), 5);
b.scroll(100, 3);
assert!(b.anchor.is_none());
b.push(line("new"));
assert_eq!(b.start(3), 9);
b.top();
assert_eq!(b.start(3), 0);
b.follow();
assert_eq!(b.start(3), 9);
}
#[test]
fn search_wraps_both_ways_and_clear_resets_position() {
let mut b = OutputBuffer::new(10);
for text in ["Error one", "fine", "error two"] {
b.push(line(text));
}
b.query = "ERROR".into();
assert!(b.search(true, true));
assert!(b.is_found(0));
assert!(b.search(true, false));
assert!(b.is_found(2));
assert!(b.search(true, false));
assert!(b.is_found(0));
assert!(b.search(false, false));
assert!(b.is_found(2));
b.query = "missing".into();
assert!(!b.search(true, true));
b.clear();
assert!(b.lines.is_empty());
assert!(b.anchor.is_none());
}
}