summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2018-07-18 13:30:30 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2018-07-18 13:30:30 -0400
commit0e450e0c3edebb9f2745339f4d59c6a4bf500cbc (patch)
tree2d811dc4d3599ddc65d73edece0255b0994feedf /src/main.rs
parent8215dc24b6a591995cd4ad2c38387d8720190de2 (diff)
convert more functions to follow configs
add intermediate configs to reduce option and matching remove unused functions
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs72
1 files changed, 62 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs
index 6fe1df9..35f9a2c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,6 @@ extern crate ncurses;
extern crate toml;
extern crate xdg;
-use std::char;
use std::env;
use std::fs;
@@ -26,7 +25,7 @@ pub struct joshuto_win {
*/
#[derive(Debug, Deserialize)]
-pub struct JoshutoConfig {
+pub struct IntermediateConfig {
show_hidden: Option<bool>,
color_scheme: Option<String>,
sort_method: Option<String>,
@@ -38,19 +37,26 @@ pub struct JoshutoKeymaps {
up : i32,
}
+pub struct JoshutoConfig {
+ show_hidden: bool,
+ color_scheme: String,
+ sort_method: String,
+ keymaps: JoshutoKeymaps,
+}
+
fn generate_default_config() -> JoshutoConfig
{
JoshutoConfig {
- show_hidden: Some(false),
- color_scheme: None,
- sort_method: Some("Natural".to_string()),
- keymaps: Some(JoshutoKeymaps {
+ show_hidden: false,
+ color_scheme: "default".to_string(),
+ sort_method: "Natural".to_string(),
+ keymaps: JoshutoKeymaps {
up : 3,
- }),
+ },
}
}
-fn get_config() -> JoshutoConfig
+fn read_config() -> Option<IntermediateConfig>
{
let dirs = xdg::BaseDirectories::with_profile(PROGRAM_NAME, "").unwrap();
match dirs.find_config_file(CONFIG_FILE) {
@@ -63,14 +69,61 @@ fn get_config() -> JoshutoConfig
},
Err(e) => {
println!("{}", e);
- generate_default_config()
+ None
}
}
},
None => {
+ None
+ }
+ }
+}
+
+fn convert_config(config : IntermediateConfig) -> JoshutoConfig
+{
+ let show_hidden : bool =
+ match config.show_hidden {
+ Some(s) => s,
+ None => false,
+ };
+ let color_scheme : String =
+ match config.color_scheme {
+ Some(s) => s,
+ None => "default".to_string(),
+ };
+ let sort_method : String =
+ match config.sort_method {
+ Some(s) => s,
+ None => "natural".to_string(),
+ };
+ let keymaps : JoshutoKeymaps =
+ match config.keymaps {
+ Some(s) => s,
+ None => JoshutoKeymaps {
+ up : 3,
+ },
+ };
+
+ JoshutoConfig {
+ show_hidden: show_hidden,
+ color_scheme: color_scheme,
+ sort_method: sort_method,
+ keymaps: keymaps,
+ }
+
+}
+
+fn get_config() -> JoshutoConfig
+{
+ match read_config() {
+ Some(inter_config) => {
+ convert_config(inter_config)
+ }
+ None => {
generate_default_config()
}
}
+
}
fn main()
@@ -79,7 +132,6 @@ fn main()
println!("{:?}", args);
let config = get_config();
- println!("{:#?}", config);
joshuto::run(&config);
}