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
use super::IRustError;
use std::fs;
use std::path;
const NEW_HISTORY_MARK: &str = "##NewHistoryMark##\n//\n";
#[derive(Default)]
pub struct History {
history: Vec<String>,
cursor: usize,
history_file_path: path::PathBuf,
pub lock: bool,
last_buffer: Vec<char>,
}
impl History {
pub fn new() -> Result<Self, IRustError> {
let history_file_path = crate::irust::cargo_cmds::IRUST_DIR.join("history");
if !history_file_path.exists() {
fs::File::create(&history_file_path)?;
}
let history: String = fs::read_to_string(&history_file_path)?;
let history: Vec<String> = if history.starts_with(NEW_HISTORY_MARK) {
history
.split("\n//\n")
.skip(1)
.map(ToOwned::to_owned)
.collect()
} else {
history.lines().map(ToOwned::to_owned).collect()
};
let cursor = 0;
Ok(Self {
history,
cursor,
history_file_path,
lock: false,
last_buffer: Vec::new(),
})
}
pub fn down(&mut self, buffer: &[char]) -> Option<String> {
if !self.lock {
self.last_buffer = buffer.to_owned();
self.cursor = 1;
}
self.cursor = self.cursor.saturating_sub(1);
if self.cursor == 0 {
return Some(self.last_buffer.iter().copied().collect());
}
let (filtered, _filtered_len) = self.filter(&self.last_buffer);
filtered.map(ToOwned::to_owned)
}
pub fn up(&mut self, buffer: &[char]) -> Option<String> {
if !self.lock {
self.last_buffer = buffer.to_owned();
self.cursor = 0;
}
self.cursor += 1;
let (filtered, filtered_len) = self.filter(&self.last_buffer);
let res = filtered.map(ToOwned::to_owned);
if self.cursor + 1 >= filtered_len {
self.cursor = filtered_len;
}
res
}
pub fn push(&mut self, buffer: String) {
if !buffer.is_empty() && Some(&buffer) != self.history.last() {
self.history.push(buffer);
self.go_to_last();
}
}
pub fn save(&self) -> Result<(), IRustError> {
let is_comment = |s: &str| -> bool { s.trim_start().starts_with("//") };
let mut history = self.history.clone();
if history.is_empty() || history[0] != NEW_HISTORY_MARK {
history.insert(0, NEW_HISTORY_MARK.to_string());
}
let history: Vec<String> = history
.into_iter()
.map(|e| {
let e: Vec<String> = e
.lines()
.filter(|l| !is_comment(l))
.map(ToOwned::to_owned)
.collect();
e.join("\n")
})
.collect();
let history = history.join("\n//\n");
fs::write(&self.history_file_path, history)?;
Ok(())
}
fn filter(&self, buffer: &[char]) -> (Option<&String>, usize) {
let mut f: Vec<&String> = self
.history
.iter()
.filter(|h| h.contains(&buffer.iter().collect::<String>()))
.rev()
.collect();
f.dedup();
let len = f.len();
(
f.get(self.cursor.saturating_sub(1)).map(ToOwned::to_owned),
len,
)
}
fn go_to_last(&mut self) {
if !self.history.is_empty() {
self.cursor = 0;
}
}
pub fn find(&self, needle: &str) -> Option<&String> {
self.history.iter().find(|h| h.contains(needle))
}
pub fn lock(&mut self) {
self.lock = true;
}
pub fn unlock(&mut self) {
self.lock = false;
}
}