Skip to content

Commit d16a008

Browse files
committed
moved resources around and changed default config files
1 parent fe7c999 commit d16a008

File tree

4 files changed

+99
-53
lines changed

4 files changed

+99
-53
lines changed

src/defaults/defaults.toml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[settings]
2+
# Use this to set the default editor for opening
3+
# shortcuts to files.
4+
default_editor="vi"
5+
6+
# Use this to set the maximum number of saved entries
7+
# in the bunnyhop command history table.
8+
#
9+
# If set to 0, will continue to history indefinitely
10+
# without removing.
11+
max_history=300
12+
13+
# Use this choose how to display the results of the `ls`
14+
# or `list` command.
15+
#
16+
# If set to 0, will print out the entire list of shortcuts
17+
# at once.
18+
#
19+
# If set to some integer N > 0,
20+
# it will display N shortcuts at once and wait for a keypress
21+
# before displaying the next N shortcuts.
22+
ls_display_block=0
23+
24+
[editors]
25+
# Use this section to define alternate editors for individual
26+
# file extensions. Set the extension on the right and the command
27+
# to open the editor for that extension on the right.
28+
#
29+
# If a file is opened without a specified extension, the default
30+
# editor will be used to open it.
31+
#
32+
# For example, use Jupyter Notebooks with IPython files, you can set:
33+
#
34+
# ipynb="jupyter-notebook"
35+
#
36+
# and anytime bunnyhop is used to open a shortcut to a .ipynb file,
37+
# it will open it in Jupyter.
38+
#
39+
# As another example, if you want to open normal Python files in
40+
# VSCode, you can set:
41+
#
42+
# py="code"
43+
#
44+
# If you want to open Scala files in Intellij, you can set:
45+
#
46+
# sc="idea"
47+
# scala="idea"
48+
# sbt="idea"
49+
#
50+
# Note that it had to be defined for all of the relevant
51+
# Scala file extensions.
52+
#
53+
# I pretty much just use Neovim (the GOAT) for everything but
54+
# notebooks. For notebooks I use: `ipynb="euporia-notebook"`

src/defaults/help.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"
2+
{} {} {}
3+
1) First argument is required.
4+
2) Second argument is optional.
5+
6+
Valid first argument commands are:
7+
1) {}: command to add a shortcut to the current directory.
8+
If a second argument is given, that argument is the name that will
9+
be used to refer to the shortcut for future use.
10+
If no second argument is given, the high level name will be used.
11+
2) {} or {}: command to list the current shortcuts and their names.
12+
3) {} or {}: both commands to show current hop version info.
13+
4) {}: command to create a temporary shortcut to the current directory
14+
that can be jumped back to using the {} {} command.
15+
5) {} or {}: command to remove the shortcut specified by {}.
16+
6) {}: Any other first arguments given will be checked to see if it
17+
represents a valid directory/file to hop to. This input can be a named
18+
shortcut, a file/directory in the current directory, or a file/directory
19+
from previous {} commands.
20+
"

src/lib.rs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use press_btn_continue;
88
use serde_derive::Deserialize;
99
use sqlite;
1010
use std::{
11+
include_str,
1112
collections::HashMap,
1213
env::current_exe,
1314
env::{current_dir, var},
@@ -44,14 +45,14 @@ impl Env {
4445
Ok(loc) => PathBuf::from(&loc),
4546
Err(_) => {
4647
home_dir.push(".config");
47-
home_dir.push("hop");
48+
home_dir.push("bunnyhop");
4849
home_dir
4950
}
5051
};
5152
let mut hop_config_file = PathBuf::from(&config_dir);
5253
match var("HOP_CONFIG_FILE_NAME") {
5354
Ok(name) => hop_config_file.push(name),
54-
Err(_) => hop_config_file.push("hop.toml"),
55+
Err(_) => hop_config_file.push("bunnyhop.toml"),
5556
};
5657
let mut database_dir = match var("HOP_DATABASE_DIRECTORY") {
5758
Ok(loc) => PathBuf::from(&loc),
@@ -70,7 +71,7 @@ impl Env {
7071
};
7172
match var("HOP_DATABASE_FILE_NAME") {
7273
Ok(name) => database_dir.push(name),
73-
Err(_) => database_dir.push("hop.sqlite"),
74+
Err(_) => database_dir.push("bunnyhop.db"),
7475
};
7576

7677
Env {
@@ -79,8 +80,7 @@ impl Env {
7980
}
8081
}
8182
}
82-
// Suppressing assignment warnings as functionality that uses `config` will be added in the future.
83-
#[allow(dead_code)]
83+
8484
pub struct Hopper {
8585
pub config: Config,
8686
pub env: Env,
@@ -99,8 +99,9 @@ impl Hopper {
9999
.expect("[error] Unable to create config directory.");
100100
let mut new_conf =
101101
fs::File::create(&env.config_file).expect("[error] Unable to create config file.");
102+
static DEFAULT_CONFIGS: &str = include_str!("defaults/defaults.toml");
102103
new_conf
103-
.write_all(b"[settings]\ndefault_editor=\"nvim\"\nmax_history=0\nls_display_block=0")
104+
.write_all(DEFAULT_CONFIGS.as_bytes())
104105
.expect("[error] Unable to generate default config file.");
105106
};
106107
let toml_str: String = fs::read_to_string(env.config_file.clone()).unwrap();
@@ -128,7 +129,7 @@ impl Hopper {
128129
})
129130
}
130131

131-
pub fn add_hop<T: AsRef<Path>>(&mut self, path: T, name: &str) -> anyhow::Result<()> {
132+
fn add_hop<T: AsRef<Path>>(&mut self, path: T, name: &str) -> anyhow::Result<()> {
132133
let query = format!(
133134
"INSERT OR REPLACE INTO named_hops (name, location) VALUES (\"{}\", \"{}\")",
134135
name,
@@ -139,7 +140,7 @@ impl Hopper {
139140
Ok(())
140141
}
141142

142-
pub fn remove_hop(&mut self, bunny: args::Rabbit) -> anyhow::Result<()> {
143+
fn remove_hop(&mut self, bunny: args::Rabbit) -> anyhow::Result<()> {
143144
let output_pair = match bunny {
144145
args::Rabbit::RequestName(name) => {
145146
Some((format!("name=\"{}\"", name), format!("shortcut: {}", name)))
@@ -182,7 +183,7 @@ impl Hopper {
182183
}
183184
}
184185

185-
pub fn use_hop(&mut self, shortcut_name: String) -> anyhow::Result<()> {
186+
fn use_hop(&mut self, shortcut_name: String) -> anyhow::Result<()> {
186187
let query = format!(
187188
"SELECT location FROM named_hops WHERE name=\"{}\"",
188189
&shortcut_name
@@ -221,7 +222,7 @@ impl Hopper {
221222
}
222223
}
223224

224-
pub fn just_do_it(&mut self, bunny: Rabbit) -> anyhow::Result<()> {
225+
fn just_do_it(&mut self, bunny: Rabbit) -> anyhow::Result<()> {
225226
match bunny {
226227
Rabbit::File(hop_name, hop_path) => self.add_hop(hop_path, &hop_name),
227228
Rabbit::Dir(hop_name, hop_path) => self.add_hop(hop_path, &hop_name),
@@ -230,7 +231,7 @@ impl Hopper {
230231
}
231232
}
232233

233-
pub fn log_history(&self, location: String, name: String) -> anyhow::Result<()> {
234+
fn log_history(&self, location: String, name: String) -> anyhow::Result<()> {
234235
if self.config.settings.max_history > 0 {
235236
let query = format!(
236237
"INSERT INTO history (time, name, location) VALUES ({}, \"{}\", \"{}\") ",
@@ -251,7 +252,7 @@ impl Hopper {
251252
Ok(())
252253
}
253254

254-
pub fn check_dir(&self, name: &str) -> Option<(PathBuf, String)> {
255+
fn check_dir(&self, name: &str) -> Option<(PathBuf, String)> {
255256
read_dir(current_dir().unwrap())
256257
.expect("[error] Unable to search contents of current directory.")
257258
.filter(|f| f.is_ok())
@@ -269,7 +270,7 @@ impl Hopper {
269270
.find(|(_, path_end)| path_end == name)
270271
}
271272

272-
pub fn list_hops(&self) -> anyhow::Result<()> {
273+
fn list_hops(&self) -> anyhow::Result<()> {
273274
let query = format!("SELECT name, location FROM named_hops");
274275
let mut query_result = self.db.prepare(&query)?;
275276
let mut hops: Vec<(String, String)> = Vec::new();
@@ -312,43 +313,14 @@ impl Hopper {
312313
Ok(())
313314
}
314315

315-
pub fn hop_names(&self) -> anyhow::Result<Vec<String>> {
316-
let query = format!("SELECT name FROM named_hops");
317-
let mut query_result = self.db.prepare(&query)?;
318-
let mut hops: Vec<String> = Vec::new();
319-
while let Ok(sqlite::State::Row) = query_result.next() {
320-
let name = query_result.read::<String, _>("name")?;
321-
hops.push(name);
322-
}
323-
Ok(hops)
324-
}
325-
326-
pub fn brb<T: AsRef<Path>>(&mut self, path: T) -> anyhow::Result<()> {
316+
fn brb<T: AsRef<Path>>(&mut self, path: T) -> anyhow::Result<()> {
327317
self.add_hop(path.as_ref(), "back")?;
328318
Ok(())
329319
}
330320

331-
pub fn print_help() -> anyhow::Result<()> {
321+
fn print_help() -> anyhow::Result<()> {
332322
println!(
333-
r#"
334-
{} {} {}
335-
1) First argument is required.
336-
2) Second argument is optional.
337-
338-
Valid first argument commands are:
339-
1) {}: command to add a shortcut to the current directory.
340-
If a second argument is given, that argument is the name that will
341-
be used to refer to the shortcut for future use.
342-
If no second argument is given, the high level name will be used.
343-
2) {} or {}: command to list the current shortcuts and their names.
344-
3) {} or {}: both commands to show current hop version info.
345-
4) {}: command to create a temporary shortcut to the current directory
346-
that can be jumped back to using the {} {} command.
347-
5) {} or {}: command to remove the shortcut specified by {}.
348-
6) {}: Any other first arguments given will be checked to see if it
349-
represents a valid directory/file to hop to. This input can be a named
350-
shortcut, a file/directory in the current directory, or a file/directory
351-
from previous {} commands."#,
323+
include!("defaults/help.txt"),
352324
"hp".bold(),
353325
"arg1".italic().dimmed(),
354326
"arg2".italic().dimmed(),
@@ -369,7 +341,7 @@ Valid first argument commands are:
369341
Ok(())
370342
}
371343

372-
pub fn runner(&self, cmd: String) -> anyhow::Result<()> {
344+
fn runner(&self, cmd: String) -> anyhow::Result<()> {
373345
let bunnyhop_exe = current_exe()
374346
.expect("[error] Unable to extract current bunnyhop executable name.")
375347
.into_os_string()

tests/test_hopper.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn test_initializing_resources() {
2020
let config_dir = PathBuf::from(&temp_dir.path());
2121
let _ = get_test_hopper(&config_dir);
2222
let mut new_toml = config_dir.clone();
23-
new_toml.push("hop.toml");
23+
new_toml.push("bunnyhop.toml");
2424
println!("{}", fs::read_to_string(&new_toml).unwrap());
2525
assert!(new_toml.exists(), "TOML wasn't created.");
2626
assert!(new_toml.is_file(), "TOML isn't a file.");
@@ -30,7 +30,7 @@ fn test_initializing_resources() {
3030
assert!(new_db.exists(), "DB directory wasn't created.");
3131
assert!(new_db.is_dir(), "DB directory isn't a directory.");
3232

33-
new_db.push("hop.sqlite");
33+
new_db.push("bunnyhop.db");
3434
assert!(new_db.exists(), "DB file wasn't created.");
3535
assert!(new_db.is_file(), "DB file isn't a file.");
3636
}
@@ -45,11 +45,11 @@ fn test_read_default_configs() {
4545
println!("{:?}", hopper.config);
4646
let default_config = bunnyhop::Config {
4747
settings: Settings {
48-
default_editor: "nvim".to_string(),
49-
max_history: 0,
48+
default_editor: "vi".to_string(),
49+
max_history: 300,
5050
ls_display_block: 0,
5151
},
52-
editors: None,
52+
editors: Some(HashMap::new()),
5353
};
5454

5555
assert_eq!(hopper.config, default_config);
@@ -62,9 +62,9 @@ fn test_read_configs_with_alt_editors() {
6262
TempDir::new("tmp_test_alt_editors").expect("Unable to create temp directory for test.");
6363
let config_dir = PathBuf::from(&temp_dir.path());
6464
let mut alt_toml = config_dir.clone();
65-
alt_toml.push("hop.toml");
65+
alt_toml.push("bunnyhop.toml");
6666
let mut alt_toml_file =
67-
fs::File::create(&alt_toml).expect("Unable to create alternate hop.toml.");
67+
fs::File::create(&alt_toml).expect("Unable to create alternate bunnyhop.toml.");
6868
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(

0 commit comments

Comments
 (0)