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
|
use std::env;
use eyre::Result;
use structopt::StructOpt;
use crate::local::database::Database;
use crate::local::history::History;
#[derive(StructOpt)]
pub enum Cmd {
#[structopt(
about="begins a new command in the history",
aliases=&["s", "st", "sta", "star"],
)]
Start { command: Vec<String> },
#[structopt(
about="finishes a new command in the history (adds time, exit code)",
aliases=&["e", "en"],
)]
End {
id: String,
#[structopt(long, short)]
exit: i64,
},
#[structopt(
about="list all items in history",
aliases=&["l", "li", "lis"],
)]
List {
#[structopt(long)]
distinct: bool,
},
}
fn print_list(h: &[History]) {
for i in h {
println!("{}", i.command);
}
}
impl Cmd {
pub fn run(&self, db: &mut impl Database) -> Result<()> {
match self {
Self::Start { command: words } => {
let command = words.join(" ");
let cwd = env::current_dir()?.display().to_string();
let h = History::new(
chrono::Utc::now().timestamp_nanos(),
command,
cwd,
-1,
-1,
None,
None,
);
// print the ID
// we use this as the key for calling end
println!("{}", h.id);
db.save(&h)?;
Ok(())
}
Self::End { id, exit } => {
let mut h = db.load(id)?;
h.exit = *exit;
h.duration = chrono::Utc::now().timestamp_nanos() - h.timestamp;
db.update(&h)?;
Ok(())
}
Self::List { .. } => {
let history = db.list()?;
print_list(&history);
Ok(())
}
}
}
}
|