Skip to content

Commit 490cecf

Browse files
committed
added history filtering
1 parent 985b874 commit 490cecf

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

src/args.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ impl Cmd {
7373
Some(name) => Cmd::Remove(Rabbit::RequestName(name)),
7474
None => Cmd::Remove(Rabbit::RequestPath(current_dir.to_path_buf())),
7575
},
76-
"ls" | "list" => Cmd::Passthrough("show_ls".to_string()),
77-
"show_ls" => Cmd::ListHops,
78-
"version" | "v" => Cmd::Passthrough("show_version".to_string()),
79-
"show_version" => Cmd::PrintMsg(format!(
76+
"ls" | "list" => Cmd::Passthrough("_ls".to_string()),
77+
"_ls" => Cmd::ListHops,
78+
"version" | "v" => Cmd::Passthrough("_version".to_string()),
79+
"_version" => Cmd::PrintMsg(format!(
8080
"{} 🐇 {}{}",
8181
"bunnyhop".cyan().bold(),
8282
"v.".bold(),
@@ -86,8 +86,8 @@ impl Cmd {
8686
Cmd::SetBrb(env::current_dir().expect("[error] Unable to add brb location."))
8787
}
8888
"back" => Cmd::BrbHop,
89-
"help" => Cmd::Passthrough("show_help".to_string()),
90-
"show_help" => Cmd::PrintHelp,
89+
"help" => Cmd::Passthrough("_help".to_string()),
90+
"_help" => Cmd::PrintHelp,
9191
whatevs => Cmd::Use(Rabbit::RequestName(whatevs.to_string())),
9292
},
9393
None => Cmd::PrintMsg("[error] Unable to parse current arguments.".to_string()),

src/lib.rs

+41-26
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Config {
2727
#[derive(Deserialize, PartialEq, Debug)]
2828
pub struct Settings {
2929
pub default_editor: String,
30-
pub max_history_entries: usize,
30+
pub max_history: usize,
3131
pub ls_display_block: usize,
3232
}
3333

@@ -100,7 +100,7 @@ impl Hopper {
100100
let mut new_conf =
101101
fs::File::create(&env.config_file).expect("[error] Unable to create config file.");
102102
new_conf
103-
.write_all(b"[settings]\ndefault_editor=\"nvim\"\nmax_history_entries=200\nls_display_block=0")
103+
.write_all(b"[settings]\ndefault_editor=\"nvim\"\nmax_history=0\nls_display_block=0")
104104
.expect("[error] Unable to generate default config file.");
105105
};
106106
let toml_str: String = fs::read_to_string(env.config_file.clone()).unwrap();
@@ -163,6 +163,25 @@ impl Hopper {
163163
Ok(())
164164
}
165165

166+
fn map_editor<T: AsRef<Path>>(&self, f: T) -> String {
167+
let ext_option = f.as_ref().extension();
168+
match &self.config.editors {
169+
Some(editor_map) => match ext_option {
170+
Some(ext) => match editor_map.get(
171+
&(ext
172+
.to_str()
173+
.expect("[error] Cannot extract extension.")
174+
.to_string()),
175+
) {
176+
Some(special_editor) => special_editor.to_string(),
177+
None => self.config.settings.default_editor.to_string(),
178+
},
179+
None => self.config.settings.default_editor.to_string(),
180+
},
181+
None => self.config.settings.default_editor.to_string(),
182+
}
183+
}
184+
166185
pub fn use_hop(&mut self, shortcut_name: String) -> anyhow::Result<()> {
167186
let query = format!(
168187
"SELECT location FROM named_hops WHERE name=\"{}\"",
@@ -173,9 +192,10 @@ impl Hopper {
173192
let location = statement.read::<String, _>("location")?;
174193
let location_path = PathBuf::from(&location);
175194
if location_path.is_file() {
195+
let editor = self.map_editor(&location_path);
176196
println!(
177197
"__cmd__ {} {}",
178-
self.config.settings.default_editor, location
198+
editor, location
179199
);
180200
} else {
181201
println!("__cd__ {}", location);
@@ -187,22 +207,7 @@ impl Hopper {
187207
Some((dir, short)) => {
188208
self.log_history(dir.as_path().display().to_string(), short)?;
189209
if dir.is_file() {
190-
let ext_option = dir.extension();
191-
let editor = match &self.config.editors {
192-
Some(editor_map) => match ext_option {
193-
Some(ext) => match editor_map.get(
194-
&(ext
195-
.to_str()
196-
.expect("[error] Cannot extract extension.")
197-
.to_string()),
198-
) {
199-
Some(special_editor) => special_editor,
200-
None => &self.config.settings.default_editor,
201-
},
202-
None => &self.config.settings.default_editor,
203-
},
204-
None => &self.config.settings.default_editor,
205-
};
210+
let editor = self.map_editor(&dir);
206211
println!("__cmd__ {} {}", editor, dir.as_path().display().to_string());
207212
} else {
208213
println!("__cd__ {}", dir.as_path().display().to_string());
@@ -226,13 +231,23 @@ impl Hopper {
226231
}
227232

228233
pub fn log_history(&self, location: String, name: String) -> anyhow::Result<()> {
229-
let query = format!(
230-
"INSERT INTO history (time, name, location) VALUES ({}, \"{}\", \"{}\") ",
231-
Local::now().format("%Y%m%d%H%M%S"),
232-
name,
233-
location
234-
);
235-
self.db.execute(&query)?;
234+
if self.config.settings.max_history > 0 {
235+
let query = format!(
236+
"INSERT INTO history (time, name, location) VALUES ({}, \"{}\", \"{}\") ",
237+
Local::now().format("%Y%m%d%H%M%S"),
238+
name,
239+
location
240+
);
241+
self.db.execute(&query)?;
242+
let mut count_result = self.db.prepare("SELECT COUNT(*) AS hist_count, MIN(time) AS hist_min FROM history")?;
243+
if let Ok(sqlite::State::Row) = count_result.next() {
244+
let history_count = count_result.read::<i64, _>("hist_count")?;
245+
let history_min = count_result.read::<String, _>("hist_min")?;
246+
if history_count > self.config.settings.max_history as i64 {
247+
self.db.execute(&format!("DELETE FROM history WHERE time=\"{}\"", history_min))?;
248+
};
249+
};
250+
};
236251
Ok(())
237252
}
238253

tests/test_hopper.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn test_read_default_configs() {
4646
let default_config = bunnyhop::Config {
4747
settings: Settings {
4848
default_editor: "nvim".to_string(),
49-
max_history_entries: 200,
49+
max_history: 0,
5050
ls_display_block: 0,
5151
},
5252
editors: None,
@@ -65,7 +65,7 @@ fn test_read_configs_with_alt_editors() {
6565
alt_toml.push("hop.toml");
6666
let mut alt_toml_file =
6767
fs::File::create(&alt_toml).expect("Unable to create alternate hop.toml.");
68-
alt_toml_file.write_all(b"[settings]\ndefault_editor=\"vi\"\nmax_history_entries=100\nls_display_block=10\n[editors]\npy=\"nano\"\nipynb=\"code\"\nrust=\"nvim\"").expect("Unable to generate alternate hop.toml.");
68+
alt_toml_file.write_all(b"[settings]\ndefault_editor=\"vi\"\nmax_history=100\nls_display_block=10\n[editors]\npy=\"nano\"\nipynb=\"code\"\nrust=\"nvim\"").expect("Unable to generate alternate hop.toml.");
6969
let hopper = get_test_hopper(&config_dir);
7070
let expected_editors = HashMap::from_iter(
7171
[("py", "nano"), ("ipynb", "code"), ("rust", "nvim")]
@@ -75,7 +75,7 @@ fn test_read_configs_with_alt_editors() {
7575
let default_config = bunnyhop::Config {
7676
settings: Settings {
7777
default_editor: "vi".to_string(),
78-
max_history_entries: 100,
78+
max_history: 100,
7979
ls_display_block: 10,
8080
},
8181
editors: Some(expected_editors),

0 commit comments

Comments
 (0)