diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cli/args.rs | 4 | ||||
-rw-r--r-- | src/cli/mod.rs | 9 | ||||
-rw-r--r-- | src/conf/conf.rs | 23 | ||||
-rw-r--r-- | src/conf/default.rs | 34 | ||||
-rw-r--r-- | src/conf/mod.rs | 8 | ||||
-rw-r--r-- | src/pattern/search_mode.rs | 2 |
6 files changed, 55 insertions, 25 deletions
diff --git a/src/cli/args.rs b/src/cli/args.rs index 56ec4e5..a81d25e 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -146,6 +146,10 @@ pub struct Args { #[clap(long, action)] pub get_root: bool, + /// Write default conf files in given directory + #[clap(long, value_parser)] + pub write_default_conf: Option<PathBuf>, + /// A socket that broot sends commands to before quitting #[cfg(unix)] #[clap(long, value_parser)] diff --git a/src/cli/mod.rs b/src/cli/mod.rs index c261be1..bd18a89 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -14,7 +14,7 @@ pub use { use { crate::{ app::{App, AppContext}, - conf::Conf, + conf::{Conf, write_default_conf_in}, display, errors::ProgramError, launchable::Launchable, @@ -41,12 +41,17 @@ pub fn run() -> Result<Option<Launchable>, ProgramError> { // parse the launch arguments we got from cli let args = Args::parse(); + let mut must_quit = false; + + if let Some(dir) = &args.write_default_conf { + write_default_conf_in(dir)?; + must_quit = true; + } // read the install related arguments let install_args = InstallLaunchArgs::from(&args)?; // execute installation things required by launch args - let mut must_quit = false; if let Some(state) = install_args.set_install_state { write_state(state)?; must_quit = true; diff --git a/src/conf/conf.rs b/src/conf/conf.rs index 1fe2df0..4ccd302 100644 --- a/src/conf/conf.rs +++ b/src/conf/conf.rs @@ -19,10 +19,7 @@ use { crossterm::style::Attribute, fnv::FnvHashMap, serde::Deserialize, - std::{ - fs, io, - path::{Path, PathBuf}, - }, + std::path::PathBuf, }; macro_rules! overwrite { @@ -127,29 +124,23 @@ impl Conf { /// read the configuration file from the default OS specific location. /// Create it if it doesn't exist pub fn from_default_location() -> Result<Conf, ProgramError> { + let conf_dir = super::dir(); let conf_filepath = Conf::default_location(); if !conf_filepath.exists() { - Conf::write_sample(&conf_filepath)?; + write_default_conf_in(conf_dir)?; println!( - "New Configuration file written in {}{:?}{}.", + "New Configuration files written in {}{:?}{}.", Attribute::Bold, - &conf_filepath, + &conf_dir, Attribute::Reset, ); - println!("You should have a look at it."); + println!("You should have a look at them."); } let mut conf = Conf::default(); - conf.read_file(conf_filepath.clone())?; + conf.read_file(conf_filepath)?; Ok(conf) } - /// assume the file doesn't yet exist - pub fn write_sample(filepath: &Path) -> Result<(), io::Error> { - fs::create_dir_all(filepath.parent().unwrap())?; - fs::write(filepath, DEFAULT_CONF_FILE)?; - Ok(()) - } - pub fn solve_conf_path(&self, path: &str) -> Option<PathBuf> { if path.ends_with(".toml") || path.ends_with(".hjson") { for conf_file in self.files.iter().rev() { diff --git a/src/conf/default.rs b/src/conf/default.rs new file mode 100644 index 0000000..93a87e2 --- /dev/null +++ b/src/conf/default.rs @@ -0,0 +1,34 @@ +use { + include_dir::{Dir, include_dir}, + std::{ + fs, + io, + path::Path, + }, +}; + +static DEFAULT_CONF_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources/default-conf"); + +pub fn write_default_conf_in(dir: &Path) -> Result<(), io::Error> { + info!("writing default conf in {:?}", dir); + if dir.exists() { + if !dir.is_dir() { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("{:?} isn't a directory", dir), + )); + } + } else { + fs::create_dir_all(dir)?; + } + for file in DEFAULT_CONF_DIR.files() { + let dest_path = dir.join(file.path()); + if dest_path.exists() { + warn!("not overwriting {:?}", dest_path); + } else { + info!("writing file {:?}", file.path()); + fs::write(dest_path, file.contents())?; + } + } + Ok(()) +} diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 8a9f1f6..1b8db40 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -4,12 +4,14 @@ use { }; mod conf; +mod default; mod format; mod import; mod verb_conf; pub use { conf::Conf, + default::write_default_conf_in, format::*, import::*, once_cell::sync::Lazy, @@ -17,12 +19,6 @@ pub use { }; -/// the content of the conf.hjson file broot creates when there's none. -/// -/// It features some default configuration and many sections the user -/// can uncomment then edit. -pub const DEFAULT_CONF_FILE: &str = include_str!("../../resources/default-conf.hjson"); - /// return the instance of ProjectDirs holding broot's specific paths pub fn app_dirs() -> directories::ProjectDirs { directories::ProjectDirs::from("org", "dystroy", "broot") diff --git a/src/pattern/search_mode.rs b/src/pattern/search_mode.rs index 8cadc2d..d903e84 100644 --- a/src/pattern/search_mode.rs +++ b/src/pattern/search_mode.rs @@ -129,7 +129,7 @@ impl SearchModeMapEntry { let mut search_objects = Vec::new(); let s = conf_mode.to_lowercase(); - for t in s.trim().split_whitespace() { + for t in s.split_whitespace() { match t { "exact" => search_kinds.push(SearchKind::Exact), "fuzzy" => search_kinds.push(SearchKind::Fuzzy), |