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
use crate::history::History;
use crate::settings::{HistoryFormat, Settings};
use crate::shell_history;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
pub fn clean(settings: &Settings, history: &History, command: &str) {
history.delete_command(command);
match settings.history_format {
HistoryFormat::Bash | HistoryFormat::Zsh { .. } => {
clean_temporary_files(&settings.mcfly_history, settings.history_format, command);
let histfile = PathBuf::from(
env::var("HISTFILE")
.or_else(|_| env::var("MCFLY_HISTFILE"))
.unwrap_or_else(|err| {
panic!(
"McFly error: Please ensure that HISTFILE/MCFLY_HISTFILE is set ({})",
err
)
}),
);
shell_history::delete_lines(&histfile, settings.history_format, command)
}
HistoryFormat::Fish => {}
}
}
fn clean_temporary_files(mcfly_history: &Path, history_format: HistoryFormat, command: &str) {
let path = mcfly_history;
if let Some(directory) = path.parent() {
let expanded_path = fs::canonicalize(directory).unwrap_or_else(|err| {
panic!(
"McFly error: The contents of $MCFLY_HISTORY appear invalid ({})",
err
)
});
let paths = fs::read_dir(&expanded_path).unwrap();
for entry in paths.flatten() {
if let Some(file_name) = entry.path().file_name() {
if let Some(valid_unicode_str) = file_name.to_str() {
if valid_unicode_str.starts_with("mcfly.") {
shell_history::delete_lines(&entry.path(), history_format, command);
}
}
}
}
}
}