fix(history): bounded size

(scope): title
This commit is contained in:
OliverGuy 2026-05-07 23:56:41 +02:00
parent c5658c40c9
commit 34c6ec887d

View file

@ -2,26 +2,38 @@ use std::mem;
use super::InputSnaps; use super::InputSnaps;
// TODO: make configurable?
const MAX_LENGTH: usize = 20;
#[derive(Default)] #[derive(Default)]
pub struct InputHistory { pub struct InputHistory {
entries: Vec<String>, entries: std::collections::VecDeque<String>,
entry_snaps: Vec<Option<InputSnaps>>, entry_snaps: std::collections::VecDeque<Option<InputSnaps>>,
idx: Option<usize>, idx: Option<usize>,
draft: Option<InputSnaps>, draft: Option<InputSnaps>,
} }
impl InputHistory { impl InputHistory {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { entries: Vec::new(), entry_snaps: Vec::new(), idx: None, draft: None } Self {
entries: std::collections::VecDeque::new(),
entry_snaps: std::collections::VecDeque::new(),
idx: None,
draft: None,
}
} }
pub fn push(&mut self, value: String) { pub fn push(&mut self, value: String) {
if value.is_empty() { if value.is_empty() {
return; return;
} }
if self.entries.last().map(String::as_str) != Some(&value) { if self.entries.back().map(String::as_str) != Some(&value) {
self.entries.push(value); if self.entries.len() >= MAX_LENGTH {
self.entry_snaps.push(None); self.entries.pop_front();
self.entry_snaps.pop_front();
}
self.entries.push_back(value);
self.entry_snaps.push_back(None);
} }
self.reset(); self.reset();
} }