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
use crate::settings::HistoryFormat;
use regex::Regex;
use std::env;
use std::fmt;
use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
fn read_ignoring_utf_errors(path: &PathBuf) -> String {
let mut f =
File::open(path).unwrap_or_else(|_| panic!("McFly error: {:?} file not found", &path));
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)
.unwrap_or_else(|_| panic!("McFly error: Unable to read from {:?}", &path));
String::from_utf8_lossy(&buffer).to_string()
}
#[allow(clippy::if_same_then_else)]
fn has_leading_timestamp(line: &str) -> bool {
let mut matched_chars = 0;
for (index, c) in line.chars().enumerate() {
if index == 0 && c == '#' {
matched_chars += 1;
} else if index > 0 && index < 11 && (c.is_digit(10)) {
matched_chars += 1;
} else if index > 11 {
break;
}
}
matched_chars == 11
}
pub fn history_file_path() -> PathBuf {
let path = PathBuf::from(env::var("HISTFILE").unwrap_or_else(|err| {
panic!(format!(
"McFly error: Please ensure HISTFILE is set for your shell ({})",
err
))
}));
fs::canonicalize(&path).unwrap_or_else(|err| {
panic!(format!(
"McFly error: The contents of $HISTFILE appear invalid ({})",
err
))
})
}
#[derive(Debug)]
pub struct HistoryCommand {
pub command: String,
pub when: i64,
pub format: HistoryFormat,
}
impl HistoryCommand {
pub fn new<S>(command: S, when: i64, format: HistoryFormat) -> Self
where
S: Into<String>,
{
Self {
command: command.into(),
when,
format,
}
}
}
impl fmt::Display for HistoryCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.format {
HistoryFormat::Bash => write!(f, "{}", self.command),
HistoryFormat::Zsh { extended_history } => {
if extended_history {
write!(f, ": {}:0;{}", self.when, self.command)
} else {
write!(f, "{}", self.command)
}
}
HistoryFormat::Fish => writeln!(f, "- cmd: {}\n when: {}", self.command, self.when),
}
}
}
pub fn full_history(path: &PathBuf, history_format: HistoryFormat) -> Vec<HistoryCommand> {
match history_format {
HistoryFormat::Bash | HistoryFormat::Zsh { .. } => {
let history_contents = read_ignoring_utf_errors(&path);
let zsh_timestamp_and_duration_regex = Regex::new(r"^: \d+:\d+;").unwrap();
let when = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|err| panic!("McFly error: Time went backwards ({})", err))
.as_secs() as i64;
history_contents
.split('\n')
.filter(|line| !has_leading_timestamp(line) && !line.is_empty())
.map(|line| zsh_timestamp_and_duration_regex.replace(line, ""))
.map(|line| HistoryCommand::new(line, when, history_format))
.collect()
}
HistoryFormat::Fish => {
let mut commands = Vec::new();
let history_contents = read_ignoring_utf_errors(&path);
let mut command = None;
for line in history_contents.split('\n') {
if line.starts_with("- cmd: ") {
command = Some(line.split_at(7).1);
} else if line.starts_with(" when: ") {
let when_str = line.split_at(8).1;
let when =
i64::from_str(when_str).unwrap_or_else(|e| panic!("McFly error: fish history '{}' has 'when' that's not a valid i64 ({}) - {}", path.display(), when_str, e));
commands.push(HistoryCommand::new(
command.take().unwrap_or_else(|| panic!("McFly error: invalid fish history file '{}', found 'when' without 'cmd' ({})", path.display(), when)),
when,
history_format,
));
}
}
commands
}
}
}
pub fn last_history_line(path: &PathBuf, history_format: HistoryFormat) -> Option<String> {
full_history(path, history_format)
.last()
.map(|s| s.command.trim().to_string())
}
pub fn delete_last_history_entry_if_search(
path: &PathBuf,
history_format: HistoryFormat,
debug: bool,
) {
let mut commands = full_history(path, history_format);
if !commands.is_empty() && commands[commands.len() - 1].command.is_empty() {
commands.pop();
}
let starts_with_mcfly = Regex::new(r"^(: \d+:\d+;)?#mcfly:").unwrap();
if commands.is_empty() || !starts_with_mcfly.is_match(&commands[commands.len() - 1].command) {
return;
}
if debug {
println!(
"McFly: Removed from file '{}': {:?}",
path.display(),
commands.pop()
);
} else {
commands.pop();
}
if !commands.is_empty() && has_leading_timestamp(&commands[commands.len() - 1].command) {
commands.pop();
}
let lines = commands
.into_iter()
.map(|cmd| cmd.to_string())
.chain(Some(String::from("")))
.collect::<Vec<String>>();
fs::write(&path, lines.join("\n"))
.unwrap_or_else(|_| panic!("McFly error: Unable to update {:?}", &path));
}
pub fn delete_lines(path: &PathBuf, history_format: HistoryFormat, command: &str) {
let commands = full_history(path, history_format);
let zsh_timestamp_and_duration_regex = Regex::new(r"^: \d+:\d+;").unwrap();
let lines = commands
.into_iter()
.filter(|cmd| !command.eq(&zsh_timestamp_and_duration_regex.replace(&cmd.command, "")))
.map(|cmd| cmd.to_string())
.chain(Some(String::from("")))
.collect::<Vec<String>>();
fs::write(&path, lines.join("\n"))
.unwrap_or_else(|_| panic!("McFly error: Unable to update {:?}", &path));
}
pub fn append_history_entry(command: &HistoryCommand, path: &PathBuf, debug: bool) {
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(path)
.unwrap_or_else(|err| {
panic!(format!(
"McFly error: please make sure HISTFILE exists ({})",
err
))
});
if debug {
println!("McFly: Appended to file '{:?}': {}", &path, command);
}
if let Err(e) = writeln!(file, "{}", command) {
eprintln!("Couldn't append to file '{}': {}", path.display(), e);
}
}
#[cfg(test)]
mod tests {
use super::has_leading_timestamp;
#[test]
fn has_leading_timestamp_works() {
assert_eq!(false, has_leading_timestamp("abc"));
assert_eq!(false, has_leading_timestamp("#abc"));
assert_eq!(false, has_leading_timestamp("#123456"));
assert_eq!(true, has_leading_timestamp("#1234567890"));
assert_eq!(false, has_leading_timestamp("#123456789"));
assert_eq!(false, has_leading_timestamp("# 1234567890"));
assert_eq!(false, has_leading_timestamp("1234567890"));
assert_eq!(false, has_leading_timestamp("hello 1234567890"));
}
}