1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use super::*;
use std::{
collections::{vec_deque, VecDeque},
fs::File,
io::{self, Write},
io::{BufRead, BufReader, BufWriter},
iter::IntoIterator,
ops::Index,
ops::IndexMut,
path::Path,
//time::Duration,
};
const DEFAULT_MAX_SIZE: usize = 1000;
/// Structure encapsulating command history
pub struct History {
// TODO: this should eventually be private
/// Vector of buffers to store history in
pub buffers: VecDeque<Buffer>,
/// Store a filename to save history into; if None don't save history
file_name: Option<String>,
/// Maximal number of buffers stored in the memory
/// TODO: just make this public?
max_buffers_size: usize,
/// Maximal number of lines stored in the file
// TODO: just make this public?
max_file_size: usize,
// TODO set from environment variable?
pub append_duplicate_entries: bool,
/// Append each entry to history file as entered?
pub inc_append: bool,
/// Share history across ion's with the same history file (combine with inc_append).
pub share: bool,
/// Last filesize of history file, used to optimize history sharing.
pub file_size: u64,
/// Allow loading duplicate entries, need to know this for loading history files.
pub load_duplicates: bool,
/// Writes between history compaction.
compaction_writes: usize,
}
impl Default for History {
fn default() -> Self {
Self::new()
}
}
impl History {
/// Create new History structure.
pub fn new() -> History {
History {
buffers: VecDeque::with_capacity(DEFAULT_MAX_SIZE),
file_name: None,
max_buffers_size: DEFAULT_MAX_SIZE,
max_file_size: DEFAULT_MAX_SIZE,
append_duplicate_entries: false,
inc_append: false,
share: false,
file_size: 0,
load_duplicates: true,
compaction_writes: 0,
}
}
/// Clears out the history.
pub fn clear_history(&mut self) {
self.buffers.clear();
}
/// Loads the history file from the saved path and appends it to the end of the history if append
/// is true otherwise replace history.
pub fn load_history(&mut self, append: bool) -> io::Result<u64> {
if let Some(path) = self.file_name.clone() {
let file_size = self.file_size;
self.load_history_file_test(&path, file_size, append)
.inspect(|&l| {
self.file_size = l;
})
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"History filename not set!",
))
}
}
/// Loads the history file from path and appends it to the end of the history if append is true.
pub fn load_history_file<P: AsRef<Path>>(&mut self, path: P, append: bool) -> io::Result<u64> {
self.load_history_file_test(path, 0, append)
}
/// Loads the history file from path and appends it to the end of the history.f append is true
/// (replaces if false). Only loads if length is not equal to current file size.
fn load_history_file_test<P: AsRef<Path>>(
&mut self,
path: P,
length: u64,
append: bool,
) -> io::Result<u64> {
let path = path.as_ref();
let file = if path.exists() {
File::open(path)?
} else {
let status = format!("File not found {:?}", path);
return Err(io::Error::new(io::ErrorKind::Other, status));
};
let new_length = file.metadata()?.len();
if new_length == 0 && length == 0 && !append {
// Special case, trying to load nothing and not appending- just clear.
self.clear_history();
}
if new_length != length {
if !append {
self.clear_history();
}
let reader = BufReader::new(file);
for line in reader.lines() {
match line {
Ok(line) => {
if !line.starts_with('#') {
self.buffers.push_back(Buffer::from(line));
}
}
Err(_) => break,
}
}
self.truncate();
if !self.load_duplicates {
let mut tmp_buffers: Vec<Buffer> = Vec::with_capacity(self.buffers.len());
// Remove duplicates from loaded history if we do not want it.
while let Some(buf) = self.buffers.pop_back() {
self.remove_duplicates(&buf.to_string()[..]);
tmp_buffers.push(buf);
}
while let Some(buf) = tmp_buffers.pop() {
self.buffers.push_back(buf);
}
}
}
Ok(new_length)
}
/// Removes duplicates and trims a history file to max_file_size.
/// Primarily if inc_append is set without shared history.
/// Static because it should have no side effects on a history object.
fn deduplicate_history_file<P: AsRef<Path>>(
path: P,
max_file_size: usize,
) -> io::Result<String> {
let path = path.as_ref();
let file = if path.exists() {
File::open(path)?
} else {
let status = format!("File not found {:?}", path);
return Err(io::Error::new(io::ErrorKind::Other, status));
};
let mut buf: VecDeque<String> = VecDeque::new();
let reader = BufReader::new(file);
for line in reader.lines() {
match line {
Ok(line) => {
if !line.starts_with('#') {
buf.push_back(line);
}
}
Err(_) => break,
}
}
let org_length = buf.len();
if buf.len() >= max_file_size {
let pop_out = buf.len() - max_file_size;
for _ in 0..pop_out {
buf.pop_front();
}
}
let mut tmp_buffers: Vec<String> = Vec::with_capacity(buf.len());
// Remove duplicates from loaded history if we do not want it.
while let Some(line) = buf.pop_back() {
buf.retain(|buffer| *buffer != line);
tmp_buffers.push(line);
}
while let Some(line) = tmp_buffers.pop() {
buf.push_back(line);
}
if org_length != buf.len() {
// Overwrite the history file with the deduplicated version if it changed.
let mut file = BufWriter::new(File::create(path)?);
// Write the commands to the history file.
for command in buf.into_iter() {
let _ = file.write_all(command.as_bytes());
let _ = file.write_all(b"\n");
}
}
Ok("De-duplicated history file.".to_string())
}
/// Set history file name and at the same time load the history.
pub fn set_file_name_and_load_history<P: AsRef<Path>>(&mut self, path: P) -> io::Result<u64> {
let path = path.as_ref();
self.file_name = path.to_str().map(|s| s.to_owned());
self.file_size = 0;
if path.exists() {
self.load_history_file(path, false).inspect(|&l| {
self.file_size = l;
})
} else {
File::create(path)?;
Ok(0)
}
}
/// Set maximal number of buffers stored in memory
pub fn set_max_buffers_size(&mut self, size: usize) {
self.max_buffers_size = size;
}
/// Set maximal number of entries in history file
pub fn set_max_file_size(&mut self, size: usize) {
self.max_file_size = size;
}
/// Number of items in history.
#[inline(always)]
pub fn len(&self) -> usize {
self.buffers.len()
}
/// Is the history empty
pub fn is_empty(&self) -> bool {
self.buffers.is_empty()
}
/// Add a command to the history buffer and remove the oldest commands when the max history
/// size has been met. If writing to the disk is enabled, this function will be used for
/// logging history to the designated history file.
pub fn push(&mut self, new_item: Buffer) -> io::Result<()> {
// buffers[0] is the oldest entry
// the new entry goes to the end
if !self.append_duplicate_entries
&& self.buffers.back().map(|b| b.to_string()) == Some(new_item.to_string())
{
return Ok(());
}
let item_str = String::from(new_item.clone());
self.buffers.push_back(new_item);
//self.to_max_size();
while self.buffers.len() > self.max_buffers_size {
self.buffers.pop_front();
}
if self.inc_append && self.file_name.is_some() {
if !self.load_duplicates {
// Do not want duplicates so periodically compact the history file.
self.compaction_writes += 1;
// Every 30 writes "compact" the history file by writing just in memory history. This
// is to keep the history file clean and at a reasonable size (not much over max
// history size at it's worst).
if self.compaction_writes > 29 {
if self.share {
// Reload history, we may be out of sync.
let _ = self.load_history(false);
// Commit the duplicated history.
if let Some(file_name) = self.file_name.clone() {
let _ = self.overwrite_history(file_name);
}
} else {
// Not using shared history so just de-dup the file without messing with
// our history.
if let Some(file_name) = self.file_name.clone() {
let _ =
History::deduplicate_history_file(file_name, self.max_file_size);
}
}
self.compaction_writes = 0;
}
} else {
// If allowing duplicates then no need for compaction.
self.compaction_writes = 1;
}
let file_name = self.file_name.clone().unwrap();
if let Ok(inner_file) = std::fs::OpenOptions::new().append(true).open(&file_name) {
// Leave file size alone, if it is not right trigger a reload later.
if self.compaction_writes > 0 {
// If 0 we "compacted" and nothing to write.
let mut file = BufWriter::new(inner_file);
let _ = file.write_all(item_str.as_bytes());
let _ = file.write_all(b"\n");
// Save the filesize after each append so we do not reload when we do not need to.
self.file_size += item_str.len() as u64 + 1;
}
}
}
Ok(())
}
/// Removes duplicate entries in the history
pub fn remove_duplicates(&mut self, input: &str) {
self.buffers.retain(|buffer| {
let command = buffer.lines().concat();
command != input
});
}
fn get_match<I>(&self, vals: I, search_term: &Buffer) -> Option<usize>
where
I: Iterator<Item = usize>,
{
vals.filter_map(|i| self.buffers.get(i).map(|t| (i, t)))
.find(|(_i, tested)| tested.starts_with(search_term))
.map(|(i, _)| i)
}
/// Go through the history and try to find an index (newest to oldest) which starts the same
/// as the new buffer given to this function as argument. Starts at curr_position. Does no wrap.
pub fn get_newest_match(
&self,
curr_position: Option<usize>,
new_buff: &Buffer,
) -> Option<usize> {
let pos = curr_position.unwrap_or(self.buffers.len());
if pos > 0 {
self.get_match((0..pos).rev(), new_buff)
} else {
None
}
}
pub fn get_history_subset(&self, search_term: &Buffer) -> Vec<usize> {
let mut v: Vec<usize> = Vec::new();
let mut ret: Vec<usize> = (0..self.len())
.filter(|i| {
if let Some(tested) = self.buffers.get(*i) {
let starts = tested.starts_with(search_term);
let contains = tested.contains(search_term);
if starts {
v.push(*i);
}
contains && !starts && tested != search_term
} else {
false
}
})
.collect();
ret.append(&mut v);
ret
}
pub fn search_index(&self, search_term: &Buffer) -> Vec<usize> {
(0..self.len())
.filter_map(|i| self.buffers.get(i).map(|t| (i, t)))
.filter(|(_i, tested)| tested.contains(search_term))
.map(|(i, _)| i)
.collect()
}
/// Get the history file name.
#[inline(always)]
pub fn file_name(&self) -> Option<&str> {
self.file_name.as_deref()
}
fn truncate(&mut self) {
// Find how many lines we need to move backwards
// in the file to remove all the old commands.
if self.buffers.len() >= self.max_file_size {
let pop_out = self.buffers.len() - self.max_file_size;
for _ in 0..pop_out {
self.buffers.pop_front();
}
}
}
fn overwrite_history<P: AsRef<Path>>(&mut self, path: P) -> io::Result<String> {
self.truncate();
let mut file = BufWriter::new(File::create(&path)?);
// Write the commands to the history file.
for command in self.buffers.iter().cloned() {
let _ = file.write_all(String::from(command).as_bytes());
let _ = file.write_all(b"\n");
}
Ok("Wrote history to file.".to_string())
}
pub fn commit_to_file_path<P: AsRef<Path>>(&mut self, path: P) -> io::Result<String> {
if self.inc_append {
Ok("Nothing to commit.".to_string())
} else {
self.overwrite_history(path)
}
}
pub fn commit_to_file(&mut self) {
if let Some(file_name) = self.file_name.clone() {
let _ = self.commit_to_file_path(file_name);
}
}
}
impl<'a> IntoIterator for &'a History {
type Item = &'a Buffer;
type IntoIter = vec_deque::Iter<'a, Buffer>;
fn into_iter(self) -> Self::IntoIter {
self.buffers.iter()
}
}
impl Index<usize> for History {
type Output = Buffer;
fn index(&self, index: usize) -> &Buffer {
&self.buffers[index]
}
}
impl IndexMut<usize> for History {
fn index_mut(&mut self, index: usize) -> &mut Buffer {
&mut self.buffers[index]
}
}