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
use std::{
fs::File,
io::{self, BufRead, BufReader, Read, Seek},
path::{Path, PathBuf},
};
use chrono::prelude::*;
use chrono::Utc;
use directories::BaseDirs;
use eyre::{eyre, Result};
use super::{count_lines, Importer};
use crate::history::History;
#[derive(Debug)]
pub struct Fish<R> {
file: BufReader<R>,
strbuf: String,
loc: usize,
}
impl<R: Read + Seek> Fish<R> {
fn new(r: R) -> Result<Self> {
let mut buf = BufReader::new(r);
let loc = count_lines(&mut buf)?;
Ok(Self {
file: buf,
strbuf: String::new(),
loc,
})
}
}
impl<R: Read> Fish<R> {
fn new_entry(&mut self) -> io::Result<bool> {
let inner = self.file.fill_buf()?;
Ok(inner.starts_with(b"- "))
}
}
impl Importer for Fish<File> {
const NAME: &'static str = "fish";
fn histpath() -> Result<PathBuf> {
let base = BaseDirs::new().ok_or_else(|| eyre!("could not determine data directory"))?;
let data = base.data_local_dir();
let session = std::env::var("fish_history").unwrap_or_else(|_| String::from("fish"));
let session = if session == "default" {
String::from("fish")
} else {
session
};
let mut histpath = data.join("fish");
histpath.push(format!("{}_history", session));
if histpath.exists() {
Ok(histpath)
} else {
Err(eyre!("Could not find history file. Try setting $HISTFILE"))
}
}
fn parse(path: impl AsRef<Path>) -> Result<Self> {
Self::new(File::open(path)?)
}
}
impl<R: Read> Iterator for Fish<R> {
type Item = Result<History>;
fn next(&mut self) -> Option<Self::Item> {
let mut time: Option<DateTime<Utc>> = None;
let mut cmd: Option<String> = None;
loop {
self.strbuf.clear();
match self.file.read_line(&mut self.strbuf) {
Ok(0) => break,
Err(e) => return Some(Err(e.into())),
_ => (),
}
self.strbuf.pop();
if let Some(c) = self.strbuf.strip_prefix("- cmd: ") {
let c = c.replace(r"\\", r"\");
let c = c.replace(r"\n", "\n");
cmd = Some(c);
} else if let Some(t) = self.strbuf.strip_prefix(" when: ") {
if let Ok(t) = t.parse::<i64>() {
time = Some(Utc.timestamp(t, 0));
}
} else {
}
match self.new_entry() {
Ok(true) if cmd.is_some() => break,
Err(e) => return Some(Err(e.into())),
_ => (),
}
}
let cmd = cmd?;
let time = time.unwrap_or_else(Utc::now);
Some(Ok(History::new(
time,
cmd,
"unknown".into(),
-1,
-1,
None,
None,
)))
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.loc))
}
}
#[cfg(test)]
mod test {
use chrono::{TimeZone, Utc};
use std::io::Cursor;
use super::Fish;
use crate::history::History;
macro_rules! fishtory {
($timestamp:literal, $command:literal) => {
History::new(
Utc.timestamp($timestamp, 0),
$command.into(),
"unknown".into(),
-1,
-1,
None,
None,
)
};
}
#[test]
fn parse_complex() {
let input = r#"- cmd: history --help
when: 1639162832
- cmd: cat ~/.bash_history
when: 1639162851
paths:
- ~/.bash_history
- cmd: ls ~/.local/share/fish/fish_history
when: 1639162890
paths:
- ~/.local/share/fish/fish_history
- cmd: cat ~/.local/share/fish/fish_history
when: 1639162893
paths:
- ~/.local/share/fish/fish_history
ERROR
- CORRUPTED: ENTRY
CONTINUE:
- AS
- NORMAL
- cmd: echo "foo" \\\n'bar' baz
when: 1639162933
- cmd: cat ~/.local/share/fish/fish_history
when: 1639162939
paths:
- ~/.local/share/fish/fish_history
- cmd: echo "\\"" \\\\ "\\\\"
when: 1639163063
- cmd: cat ~/.local/share/fish/fish_history
when: 1639163066
paths:
- ~/.local/share/fish/fish_history
"#;
let cursor = Cursor::new(input);
let fish = Fish::new(cursor).unwrap();
let history = fish.collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(
history,
vec![
fishtory!(1639162832, "history --help"),
fishtory!(1639162851, "cat ~/.bash_history"),
fishtory!(1639162890, "ls ~/.local/share/fish/fish_history"),
fishtory!(1639162893, "cat ~/.local/share/fish/fish_history"),
fishtory!(1639162933, "echo \"foo\" \\\n'bar' baz"),
fishtory!(1639162939, "cat ~/.local/share/fish/fish_history"),
fishtory!(1639163063, r#"echo "\"" \\ "\\""#),
fishtory!(1639163066, "cat ~/.local/share/fish/fish_history"),
]
);
}
}