ratatui_code_editor/
history.rs1use std::collections::VecDeque;
2use crate::code::{EditBatch};
3
4pub struct History {
5 index: usize,
6 max_items: usize,
7 edits: VecDeque<EditBatch>,
8}
9
10impl History {
11 pub fn new(max_items: usize) -> Self {
12 Self {
13 index: 0,
14 max_items,
15 edits: VecDeque::new(),
16 }
17 }
18
19 pub fn push(&mut self, batch: EditBatch) {
20 while self.edits.len() > self.index {
21 self.edits.pop_back();
22 }
23
24 if self.edits.len() == self.max_items {
25 self.edits.pop_front();
26 self.index -= 1;
27 }
28
29 self.edits.push_back(batch);
30 self.index += 1;
31 }
32
33 pub fn undo(&mut self) -> Option<EditBatch> {
34 if self.index == 0 {
35 None
36 } else {
37 self.index -= 1;
38 self.edits.get(self.index).cloned()
39 }
40 }
41
42 pub fn redo(&mut self) -> Option<EditBatch> {
43 if self.index >= self.edits.len() {
44 None
45 } else {
46 let batch = self.edits.get(self.index).cloned();
47 self.index += 1;
48 batch
49 }
50 }
51}