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
use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::sync::{Arc, Mutex};
thread_local!(static CMD_HISTORY_FILE: Arc<Mutex<std::fs::File>> = Arc::new(Mutex::new({
let data_dir = xdg::BaseDirectories::with_prefix("meli").unwrap();
OpenOptions::new().append(true)
.create(true)
.read(true)
.open(data_dir.place_data_file("cmd_history").unwrap()).unwrap()
})));
pub fn log_cmd(mut cmd: String) {
CMD_HISTORY_FILE.with(|f| {
cmd.push('\n');
f.lock().unwrap().write_all(cmd.as_bytes()).unwrap();
});
}
pub fn old_cmd_history() -> Vec<String> {
let mut ret = Vec::new();
CMD_HISTORY_FILE.with(|f| {
let mut old_history = String::new();
f.lock().unwrap().read_to_string(&mut old_history).unwrap();
ret.extend(old_history.lines().map(|s| s.to_string()));
});
ret
}