reovim_kernel/block/
history.rs1use {crate::mm::Edit, std::time::Instant};
7
8#[derive(Debug, Clone)]
10pub struct HistoryEntry {
11 edits: Vec<Edit>,
13 timestamp: Instant,
15 seq_num: u64,
17}
18
19impl HistoryEntry {
20 #[must_use]
22 pub fn edits(&self) -> &[Edit] {
23 &self.edits
24 }
25
26 #[must_use]
28 pub const fn timestamp(&self) -> Instant {
29 self.timestamp
30 }
31
32 #[must_use]
34 pub const fn seq_num(&self) -> u64 {
35 self.seq_num
36 }
37}
38
39#[derive(Debug)]
55pub struct History {
56 entries: Vec<HistoryEntry>,
58 max_entries: usize,
60 seq_counter: u64,
62}
63
64impl Default for History {
65 fn default() -> Self {
66 Self::new()
67 }
68}
69
70impl History {
71 pub const DEFAULT_MAX_ENTRIES: usize = 10000;
73
74 #[must_use]
76 pub fn new() -> Self {
77 Self::with_max_entries(Self::DEFAULT_MAX_ENTRIES)
78 }
79
80 #[must_use]
82 pub fn with_max_entries(max_entries: usize) -> Self {
83 Self {
84 entries: Vec::new(),
85 max_entries: max_entries.max(1),
86 seq_counter: 0,
87 }
88 }
89
90 pub fn record(&mut self, edits: Vec<Edit>) {
92 if edits.is_empty() {
93 return;
94 }
95
96 self.seq_counter += 1;
97
98 let entry = HistoryEntry {
99 edits,
100 timestamp: Instant::now(),
101 seq_num: self.seq_counter,
102 };
103
104 self.entries.push(entry);
105
106 while self.entries.len() > self.max_entries {
108 self.entries.remove(0);
109 }
110 }
111
112 #[must_use]
114 pub fn entries(&self) -> &[HistoryEntry] {
115 &self.entries
116 }
117
118 #[must_use]
120 pub fn last(&self) -> Option<&HistoryEntry> {
121 self.entries.last()
122 }
123
124 #[must_use]
126 pub fn get(&self, index: usize) -> Option<&HistoryEntry> {
127 self.entries.get(index)
128 }
129
130 pub fn clear(&mut self) {
132 self.entries.clear();
133 }
135
136 #[must_use]
138 pub const fn len(&self) -> usize {
139 self.entries.len()
140 }
141
142 #[must_use]
144 pub const fn is_empty(&self) -> bool {
145 self.entries.is_empty()
146 }
147
148 #[must_use]
150 pub const fn max_entries(&self) -> usize {
151 self.max_entries
152 }
153
154 pub fn set_max_entries(&mut self, max: usize) {
156 self.max_entries = max.max(1);
157 while self.entries.len() > self.max_entries {
158 self.entries.remove(0);
159 }
160 }
161
162 #[must_use]
164 pub const fn seq_counter(&self) -> u64 {
165 self.seq_counter
166 }
167
168 #[must_use]
170 pub fn since(&self, seq_num: u64) -> Vec<&HistoryEntry> {
171 self.entries
172 .iter()
173 .filter(|e| e.seq_num > seq_num)
174 .collect()
175 }
176
177 #[must_use]
179 pub fn between(&self, start: Instant, end: Instant) -> Vec<&HistoryEntry> {
180 self.entries
181 .iter()
182 .filter(|e| e.timestamp >= start && e.timestamp <= end)
183 .collect()
184 }
185}