diff options
author | rabite <rabite@posteo.de> | 2019-05-22 18:54:37 +0200 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-05-22 18:54:37 +0200 |
commit | d1e9c1645bfc203ce62370cfe126268425e00360 (patch) | |
tree | 1389fcba56737772b82965db1b8a3d3e240676d3 /src | |
parent | 3085d0ec0e3b4400a0137f2208dfafa8f6c296d7 (diff) |
add startup options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 77 | ||||
-rw-r--r-- | src/main.rs | 49 |
2 files changed, 125 insertions, 1 deletions
diff --git a/src/config.rs b/src/config.rs index 8938025..2803e3d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,72 @@ +use lazy_static; +use clap; + +use std::sync::RwLock; + use crate::paths; + use crate::fail::{HError, HResult, ErrorLog}; +#[derive(Clone)] +// These are options, so we know if they have been set or not +struct ArgvConfig { + animation: Option<bool>, + show_hidden: Option<bool>, + icons: Option<bool> +} + +impl ArgvConfig { + fn new() -> Self { + ArgvConfig { + animation: None, + show_hidden: None, + icons: None + } + } +} + +lazy_static! { + static ref ARGV_CONFIG: RwLock<ArgvConfig> = RwLock::new(ArgvConfig::new()); +} + + +pub fn set_argv_config(args: clap::ArgMatches) -> HResult<()> { + let animation = args.is_present("animation-off"); + let show_hidden = args.is_present("show-hidden"); + let icons = args.is_present("icons"); + + let mut config = ArgvConfig::new(); + + if animation == true { + config.animation = Some(false); + } + + if show_hidden == true { + config.show_hidden = Some(true); + } + + if icons == true { + config.icons = Some(true) + } + + *ARGV_CONFIG.write()? = config; + Ok(()) +} + +fn get_argv_config() -> HResult<ArgvConfig> { + Ok(ARGV_CONFIG.try_read()?.clone()) +} + +fn infuse_argv_config(mut config: Config) -> Config { + let argv_config = get_argv_config().unwrap_or(ArgvConfig::new()); + + argv_config.animation.map(|val| config.animation = val); + argv_config.show_hidden.map(|val| config.show_hidden = val); + argv_config.icons.map(|val| config.icons = val); + + config +} + #[derive(Debug, Clone)] pub struct Config { pub animation: bool, @@ -15,6 +81,12 @@ pub struct Config { impl Config { pub fn new() -> Config { + let config = Config::default(); + + infuse_argv_config(config) + } + + pub fn default() -> Config { Config { animation: true, show_hidden: false, @@ -30,7 +102,7 @@ impl Config { let config_path = paths::config_path()?; if !config_path.exists() { - return Ok(Config::new()); + return Ok(infuse_argv_config(Config::new())); } let config_string = std::fs::read_to_string(config_path)?; @@ -59,6 +131,9 @@ impl Config { } config }); + + let config = infuse_argv_config(config); + Ok(config) } diff --git a/src/main.rs b/src/main.rs index bd94046..0c81349 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,12 +22,14 @@ extern crate signal_notify; extern crate tree_magic; extern crate systemstat; extern crate mime_guess; +extern crate clap; extern crate osstrtools; extern crate pathbuftools; extern crate async_value; use failure::Fail; +use clap::{App, Arg}; use std::panic; @@ -90,6 +92,8 @@ fn main() -> HResult<()> { // do this early so it might be ready when needed crate::files::load_tags().ok(); + parse_args().ok(); + let mut core = WidgetCore::new().expect("Can't create WidgetCore!"); // Resets terminal when hunter crashes :( @@ -119,3 +123,48 @@ fn run(mut core: WidgetCore) -> HResult<()> { Ok(()) } + + +fn parse_args() -> HResult<()> { + let args = App::new("Lag-free terminal file browser") + .version(clap::crate_version!()) + .author(clap::crate_authors!("\n")) + .about("Hunt your files at light-speed, armed with full $SHELL integration") + .arg( + Arg::with_name("animation-off") + .short("a") + .long("animation-off") + .help("Turn off animations") + .takes_value(false), + ) + .arg( + Arg::with_name("show-hidden") + .short("h") + .long("show-hidden") + .help("Show hidden files") + .takes_value(false), + ) + .arg( + Arg::with_name("icons") + .short("i") + .long("icons") + .help("Show icons for different file types") + .takes_value(false), + ) + .arg( + Arg::with_name("path") + .index(1) + .help("Start in <path>") + ) + .get_matches(); + + + let path = args.value_of("path"); + + if let Some(path) = path { + std::env::set_current_dir(&path).ok(); + } + + crate::config::set_argv_config(args).ok(); + Ok(()) +} |