diff options
author | quentin konieczko <konieczko@gmail.com> | 2022-09-28 09:11:00 +0200 |
---|---|---|
committer | quentin konieczko <konieczko@gmail.com> | 2022-09-28 09:11:00 +0200 |
commit | fd421f98df3d485e08ec43e874bf49dc11b818c6 (patch) | |
tree | 66eac90cac66159564837dd7b089fdd4a76ebacc | |
parent | 9fff5085310199c38efd588aaab9de0cf8b8a80d (diff) |
rename config to args and config_file to config
-rw-r--r-- | src/args.rs | 63 | ||||
-rw-r--r-- | src/config.rs | 283 | ||||
-rw-r--r-- | src/config_file.rs | 248 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 12 |
5 files changed, 304 insertions, 304 deletions
diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 00000000..85b7e812 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; +use std::env; + +#[derive(Debug)] +pub struct Args { + pub hidden: bool, // "-a" + pub path: String, // "/usr/bin" + pub help: bool, +} +impl Default for Args { + fn default() -> Self { + Args { + hidden: false, + path: String::from("."), + help: false, + } + } +} + +impl Args { + pub fn new(args: env::Args) -> Result<Args, &'static str> { + match args.len() { + 1 => Ok(Args { + ..Default::default() + }), + _ => { + let (path, map_options) = parse_args(args); + Ok(Args { + hidden: map_options[&String::from("a")], + path, + help: map_options[&String::from("h")], + }) + } + } + } +} + +pub fn default_map_options() -> ([String; 2], HashMap<String, bool>) { + let arr_options: [String; 2] = [String::from("a"), String::from("h")]; + let mut map_options = HashMap::new(); + for opt in arr_options.iter() { + map_options.insert(String::from(opt), false); + } + (arr_options, map_options) +} + +pub fn parse_args(mut args: env::Args) -> (String, HashMap<String, bool>) { + let mut path = String::from("."); + let (arr_options, mut map_options) = default_map_options(); + args.next(); + for arg in args { + if arg.starts_with('-') { + for opt in &arr_options { + if arg.contains(opt) { + map_options.insert(String::from(opt), true); + } + } + } else { + path = arg; + } + } + (path, map_options) +} diff --git a/src/config.rs b/src/config.rs index 7c65a709..be05c26a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,63 +1,248 @@ -use std::collections::HashMap; -use std::env; +use std::fs::File; -#[derive(Debug)] -pub struct Config { - pub hidden: bool, // "-a" - pub path: String, // "/usr/bin" - pub help: bool, +use serde_yaml; +use tuikit::attr::Color; + +#[derive(Debug, Clone)] +pub struct Colors { + pub file: String, + pub directory: String, + pub block: String, + pub char: String, + pub fifo: String, + pub socket: String, + pub symlink: String, } -impl Default for Config { - fn default() -> Self { - Config { - hidden: false, - path: String::from("."), - help: false, + +impl Colors { + pub fn from_config(yaml: &serde_yaml::value::Value) -> Self { + let file = yaml["file"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + let directory = yaml["directory"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + let block = yaml["block"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + let char = yaml["char"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + let fifo = yaml["fifo"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + let socket = yaml["socket"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + let symlink = yaml["symlink"] + .as_str() + .map(|s| s.to_string()) + .expect("Couldn't parse config file"); + Self { + file, + directory, + block, + char, + fifo, + socket, + symlink, } } } -impl Config { - pub fn new(args: env::Args) -> Result<Config, &'static str> { - match args.len() { - 1 => Ok(Config { - ..Default::default() - }), - _ => { - let (path, map_options) = parse_args(args); - Ok(Config { - hidden: map_options[&String::from("a")], - path, - help: map_options[&String::from("h")], - }) - } +#[derive(Debug, Clone)] +pub struct Keybindings { + pub toggle_hidden: char, + pub copy_paste: char, + pub cut_paste: char, + pub delete: char, + pub chmod: char, + pub exec: char, + pub newdir: char, + pub newfile: char, + pub rename: char, + pub clear_flags: char, + pub toggle_flag: char, + pub shell: char, + pub open_file: char, + pub help: char, + pub search: char, + pub quit: char, + pub goto: char, +} + +impl Keybindings { + pub fn new(yaml: &serde_yaml::value::Value) -> Self { + let toggle_hidden = yaml["toggle_hidden"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "a".to_string()) + .chars() + .next() + .unwrap_or('a'); + let copy_paste = yaml["copy_paste"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "c".to_string()) + .chars() + .next() + .unwrap_or('c'); + let cut_paste = yaml["cut_paste"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "p".to_string()) + .chars() + .next() + .unwrap_or('p'); + let delete = yaml["delete"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "x".to_string()) + .chars() + .next() + .unwrap_or('x'); + let chmod = yaml["chmod"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "m".to_string()) + .chars() + .next() + .unwrap_or('m'); + let exec = yaml["exec"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "e".to_string()) + .chars() + .next() + .unwrap_or('e'); + let newdir = yaml["newdir"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "d".to_string()) + .chars() + .next() + .unwrap_or('d'); + let newfile = yaml["newfile"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "n".to_string()) + .chars() + .next() + .unwrap_or('n'); + let rename = yaml["rename"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "r".to_string()) + .chars() + .next() + .unwrap_or('r'); + let clear_flags = yaml["clear_flags"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "u".to_string()) + .chars() + .next() + .unwrap_or('u'); + let toggle_flag = yaml["toggle_flag"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| " ".to_string()) + .chars() + .next() + .unwrap_or(' '); + let shell = yaml["shell"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "s".to_string()) + .chars() + .next() + .unwrap_or('s'); + let open_file = yaml["open_file"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "o".to_string()) + .chars() + .next() + .unwrap_or('o'); + let help = yaml["help"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "h".to_string()) + .chars() + .next() + .unwrap_or('h'); + let search = yaml["search"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "/".to_string()) + .chars() + .next() + .unwrap_or('/'); + let quit = yaml["quit"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "q".to_string()) + .chars() + .next() + .unwrap_or('q'); + let goto = yaml["goto"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or_else(|| "g".to_string()) + .chars() + .next() + .unwrap_or('g'); + Self { + toggle_hidden, + copy_paste, + cut_paste, + delete, + chmod, + exec, + newdir, + newfile, + rename, + clear_flags, + toggle_flag, + shell, + open_file, + help, + search, + quit, + goto, } } } -pub fn default_map_options() -> ([String; 2], HashMap<String, bool>) { - let arr_options: [String; 2] = [String::from("a"), String::from("h")]; - let mut map_options = HashMap::new(); - for opt in arr_options.iter() { - map_options.insert(String::from(opt), false); +pub fn str_to_tuikit(color: &str) -> Color { + match color { + "white" => Color::WHITE, + "red" => Color::RED, + "green" => Color::GREEN, + "blue" => Color::BLUE, + "yellow" => Color::YELLOW, + "cyan" => Color::CYAN, + "magenta" => Color::MAGENTA, + "black" => Color::BLACK, + "light_white" => Color::LIGHT_WHITE, + "light_red" => Color::LIGHT_RED, + "light_green" => Color::LIGHT_GREEN, + "light_blue" => Color::LIGHT_BLUE, + "light_yellow" => Color::LIGHT_YELLOW, + "light_cyan" => Color::LIGHT_CYAN, + "light_magenta" => Color::LIGHT_MAGENTA, + "light_black" => Color::LIGHT_BLACK, + _ => Color::default(), } - (arr_options, map_options) } -pub fn parse_args(mut args: env::Args) -> (String, HashMap<String, bool>) { - let mut path = String::from("."); - let (arr_options, mut map_options) = default_map_options(); - args.next(); - for arg in args { - if arg.starts_with('-') { - for opt in &arr_options { - if arg.contains(opt) { - map_options.insert(String::from(opt), true); - } - } - } else { - path = arg; - } - } - (path, map_options) +pub fn load_file(file: &str) -> serde_yaml::Value { + let file = File::open(file).expect("Unable to open file"); + serde_yaml::from_reader(file).expect("Couldn't read yaml file") } diff --git a/src/config_file.rs b/src/config_file.rs deleted file mode 100644 index be05c26a..00000000 --- a/src/config_file.rs +++ /dev/null @@ -1,248 +0,0 @@ -use std::fs::File; - -use serde_yaml; -use tuikit::attr::Color; - -#[derive(Debug, Clone)] -pub struct Colors { - pub file: String, - pub directory: String, - pub block: String, - pub char: String, - pub fifo: String, - pub socket: String, - pub symlink: String, -} - -impl Colors { - pub fn from_config(yaml: &serde_yaml::value::Value) -> Self { - let file = yaml["file"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - let directory = yaml["directory"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - let block = yaml["block"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - let char = yaml["char"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - let fifo = yaml["fifo"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - let socket = yaml["socket"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - let symlink = yaml["symlink"] - .as_str() - .map(|s| s.to_string()) - .expect("Couldn't parse config file"); - Self { - file, - directory, - block, - char, - fifo, - socket, - symlink, - } - } -} - -#[derive(Debug, Clone)] -pub struct Keybindings { - pub toggle_hidden: char, - pub copy_paste: char, - pub cut_paste: char, - pub delete: char, - pub chmod: char, - pub exec: char, - pub newdir: char, - pub newfile: char, - pub rename: char, - pub clear_flags: char, - pub toggle_flag: char, - pub shell: char, - pub open_file: char, - pub help: char, - pub search: char, - pub quit: char, - pub goto: char, -} - -impl Keybindings { - pub fn new(yaml: &serde_yaml::value::Value) -> Self { - let toggle_hidden = yaml["toggle_hidden"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "a".to_string()) - .chars() - .next() - .unwrap_or('a'); - let copy_paste = yaml["copy_paste"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "c".to_string()) - .chars() - .next() - .unwrap_or('c'); - let cut_paste = yaml["cut_paste"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "p".to_string()) - .chars() - .next() - .unwrap_or('p'); - let delete = yaml["delete"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "x".to_string()) - .chars() - .next() - .unwrap_or('x'); - let chmod = yaml["chmod"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "m".to_string()) - .chars() - .next() - .unwrap_or('m'); - let exec = yaml["exec"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "e".to_string()) - .chars() - .next() - .unwrap_or('e'); - let newdir = yaml["newdir"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "d".to_string()) - .chars() - .next() - .unwrap_or('d'); - let newfile = yaml["newfile"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "n".to_string()) - .chars() - .next() - .unwrap_or('n'); - let rename = yaml["rename"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "r".to_string()) - .chars() - .next() - .unwrap_or('r'); - let clear_flags = yaml["clear_flags"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "u".to_string()) - .chars() - .next() - .unwrap_or('u'); - let toggle_flag = yaml["toggle_flag"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| " ".to_string()) - .chars() - .next() - .unwrap_or(' '); - let shell = yaml["shell"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "s".to_string()) - .chars() - .next() - .unwrap_or('s'); - let open_file = yaml["open_file"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "o".to_string()) - .chars() - .next() - .unwrap_or('o'); - let help = yaml["help"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "h".to_string()) - .chars() - .next() - .unwrap_or('h'); - let search = yaml["search"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "/".to_string()) - .chars() - .next() - .unwrap_or('/'); - let quit = yaml["quit"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "q".to_string()) - .chars() - .next() - .unwrap_or('q'); - let goto = yaml["goto"] - .as_str() - .map(|s| s.to_string()) - .unwrap_or_else(|| "g".to_string()) - .chars() - .next() - .unwrap_or('g'); - Self { - toggle_hidden, - copy_paste, - cut_paste, - delete, - chmod, - exec, - newdir, - newfile, - rename, - clear_flags, - toggle_flag, - shell, - open_file, - help, - search, - quit, - goto, - } - } -} - -pub fn str_to_tuikit(color: &str) -> Color { - match color { - "white" => Color::WHITE, - "red" => Color::RED, - "green" => Color::GREEN, - "blue" => Color::BLUE, - "yellow" => Color::YELLOW, - "cyan" => Color::CYAN, - "magenta" => Color::MAGENTA, - "black" => Color::BLACK, - "light_white" => Color::LIGHT_WHITE, - "light_red" => Color::LIGHT_RED, - "light_green" => Color::LIGHT_GREEN, - "light_blue" => Color::LIGHT_BLUE, - "light_yellow" => Color::LIGHT_YELLOW, - "light_cyan" => Color::LIGHT_CYAN, - "light_magenta" => Color::LIGHT_MAGENTA, - "light_black" => Color::LIGHT_BLACK, - _ => Color::default(), - } -} - -pub fn load_file(file: &str) -> serde_yaml::Value { - let file = File::open(file).expect("Unable to open file"); - serde_yaml::from_reader(file).expect("Couldn't read yaml file") -} @@ -1,3 +1,3 @@ +pub mod args; pub mod config; -pub mod config_file; pub mod fileinfo; diff --git a/src/main.rs b/src/main.rs index c81a1bed..cd2999a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,8 @@ use tuikit::event::{Event, Key}; use tuikit::key::MouseButton; use tuikit::term::{Term, TermHeight}; -use fm::config::Config; -use fm::config_file::{load_file, str_to_tuikit, Colors, Keybindings}; +use fm::args::Args; +use fm::config::{load_file, str_to_tuikit, Colors, Keybindings}; use fm::fileinfo::{FileInfo, PathContent}; pub mod fileinfo; @@ -189,7 +189,7 @@ struct Status { col: usize, path_content: PathContent, height: usize, - args: Config, + args: Args, colors: Colors, terminal: String, opener: String, @@ -197,7 +197,7 @@ struct Status { } impl Status { - fn new(args: Config, height: usize) -> Self { + fn new(args: Args, height: usize) -> Self { let path = std::fs::canonicalize(path::Path::new(&args.path)).unwrap_or_else(|_| { eprintln!("File does not exists {}", args.path); std::process::exit(2) @@ -736,8 +736,8 @@ impl Display { } } -fn read_args() -> Config { - let args = Config::new(env::args()).unwrap_or_else(|err| { +fn read_args() -> Args { + let args = Args::new(env::args()).unwrap_or_else(|err| { eprintln!("Problem parsing arguments: {}", err); help(); process::exit(1); |