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
|
use std::env;
use std::path::PathBuf;
use directories::UserDirs;
use eyre::{eyre, Result};
use structopt::StructOpt;
use crate::local::database::Database;
use crate::local::history::History;
use crate::local::import::Zsh;
use indicatif::ProgressBar;
#[derive(StructOpt)]
pub enum Cmd {
#[structopt(
about="import history for the current shell",
aliases=&["a", "au", "aut"],
)]
Auto,
#[structopt(
about="import history from the zsh history file",
aliases=&["z", "zs"],
)]
Zsh,
}
impl Cmd {
pub fn run(&self, db: &mut impl Database) -> Result<()> {
println!(" A'Tuin ");
println!("======================");
println!(" \u{1f30d} ");
println!(" \u{1f418}\u{1f418}\u{1f418}\u{1f418} ");
println!(" \u{1f422} ");
println!("======================");
println!("Importing history...");
match self {
Self::Auto => {
let shell = env::var("SHELL").unwrap_or_else(|_| String::from("NO_SHELL"));
if shell.ends_with("/zsh") {
println!("Detected ZSH");
import_zsh(db)
} else {
println!("cannot import {} history", shell);
Ok(())
}
}
Self::Zsh => import_zsh(db),
}
}
}
fn import_zsh(db: &mut impl Database) -> Result<()> {
// oh-my-zsh sets HISTFILE=~/.zhistory
// zsh has no default value for this var, but uses ~/.zhistory.
// we could maybe be smarter about this in the future :)
let histpath = env::var("HISTFILE");
let histpath = if let Ok(p) = histpath {
let histpath = PathBuf::from(p);
if !histpath.exists() {
return Err(eyre!(
"Could not find history file {:?}. try updating $HISTFILE",
histpath
));
}
histpath
} else {
let user_dirs = UserDirs::new().unwrap();
let home_dir = user_dirs.home_dir();
let mut candidates = [".zhistory", ".zsh_history"].iter();
loop {
match candidates.next() {
Some(candidate) => {
let histpath = home_dir.join(candidate);
if histpath.exists() {
break histpath;
}
}
None => return Err(eyre!("Could not find history file. try setting $HISTFILE")),
}
}
};
let zsh = Zsh::new(histpath)?;
let progress = ProgressBar::new(zsh.loc);
let buf_size = 100;
let mut buf = Vec::<History>::with_capacity(buf_size);
for i in zsh
.filter_map(Result::ok)
.filter(|x| !x.command.trim().is_empty())
{
buf.push(i);
if buf.len() == buf_size {
db.save_bulk(&buf)?;
progress.inc(buf.len() as u64);
buf.clear();
}
}
if !buf.is_empty() {
db.save_bulk(&buf)?;
progress.inc(buf.len() as u64);
}
progress.finish_with_message("Imported history!");
Ok(())
}
|