diff --git a/Cargo.lock b/Cargo.lock index e3c3f11..0354daf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,6 +280,17 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "dialoguer" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94616e25d2c04fc97253d145f6ca33ad84a584258dc70c4e621cc79a57f903b6" +dependencies = [ + "console", + "lazy_static", + "tempfile", +] + [[package]] name = "directories" version = "2.0.2" @@ -351,6 +362,7 @@ dependencies = [ "comfy-table", "confy", "console", + "dialoguer", "dirs", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 4a06ab0..82950e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ serde = {version = "1.0.104", features = ["derive"] } dirs = "2.0.2" comfy-table = "0.1.0" console="0.10.0" -confy = "0.4.0" \ No newline at end of file +confy = "0.4.0" +dialoguer = "0.5.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7074809..0ff3e9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,12 @@ fn main() { .version(version) .author("Shaishav "), ) + .subcommand( + App::new("configure") + .about("Configure settings ") + .version(version) + .author("Shaishav "), + ) .get_matches(); let result = auth::init(); @@ -89,5 +95,8 @@ fn main() { tasks::process_task_command(matches, &config, &preferences) } else if let Some(matches) = matches.subcommand_matches("summary") { summary::process_summary(matches, &config, &preferences); + } else if let Some(matches) = matches.subcommand_matches("configure") { + preferences::process_configuration(matches) + .expect("Failed to process configuration command"); } } diff --git a/src/preferences.rs b/src/preferences.rs index 8ded4bd..11b8c62 100644 --- a/src/preferences.rs +++ b/src/preferences.rs @@ -1,4 +1,9 @@ +use clap::ArgMatches; +use console::style; +use dialoguer::theme::ColorfulTheme; +use dialoguer::{Checkboxes, Input}; use serde::{Deserialize, Serialize}; +use std::io; /// Get user's preferences pub fn get_preferences() -> Result { @@ -30,3 +35,86 @@ impl ::std::default::Default for Preferences { } } } + +pub fn process_configuration(_matches: &ArgMatches) -> io::Result<()> { + let current_preferences = get_preferences().expect("Couldn't get current preferences"); + + let possible_priorities = vec![ + "unbreak-now", + "needs-triage", + "high", + "normal", + "low", + "wishlist", + ]; + + println!( + "{}", + style("Choose the task priorities to include in your summary") + .bold() + .underlined() + ); + println!("(Press space to select a priority)"); + + let summary_priorities = get_chosen_priorities( + &possible_priorities, + ¤t_preferences.summary_task_priority, + )?; + + println!( + "{}", + style("Choose the task priorities as your default for `fab tasks`") + .bold() + .underlined() + ); + println!("(Press space to select a priority)"); + + let default_task_priorities = get_chosen_priorities( + &possible_priorities, + ¤t_preferences.default_task_priority, + )?; + + println!( + "{}", + style("Choose default limit for Fab results") + .bold() + .underlined() + ); + + let default_limit = Input::with_theme(&ColorfulTheme::default()) + .with_initial_text(¤t_preferences.default_limit.to_string()) + .interact()?; + + let new_preferences = Preferences { + summary_task_priority: summary_priorities, + default_task_priority: default_task_priorities, + default_limit, + }; + + set_preferences(&new_preferences); + + Ok(()) +} + +fn get_chosen_priorities( + possible_priorities: &Vec<&str>, + current_priorities: &Vec, +) -> io::Result> { + let theme = &ColorfulTheme::default(); + let checked_priorities: Vec<(&str, bool)> = possible_priorities + .iter() + .map(|&priority| (priority, current_priorities.contains(&priority.to_string()))) + .collect(); + + let result = Checkboxes::with_theme(theme) + .items_checked(&checked_priorities) + .interact()?; + + let mut chosen_priorites: Vec = Vec::with_capacity(result.len()); + + for i in result { + chosen_priorites.push(possible_priorities[i].to_string()) + } + + Ok(chosen_priorites) +}