Skip to content

Commit

Permalink
Add configure command for preferences (#50)
Browse files Browse the repository at this point in the history
* Add initial configuration command

* Finish configuration

* FMT
  • Loading branch information
ShaishavGandhi authored Apr 4, 2020
1 parent ac45fbc commit eefb63e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
confy = "0.4.0"
dialoguer = "0.5.0"
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ fn main() {
.version(version)
.author("Shaishav <[email protected]>"),
)
.subcommand(
App::new("configure")
.about("Configure settings ")
.version(version)
.author("Shaishav <[email protected]>"),
)
.get_matches();

let result = auth::init();
Expand All @@ -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");
}
}
88 changes: 88 additions & 0 deletions src/preferences.rs
Original file line number Diff line number Diff line change
@@ -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<Preferences, String> {
Expand Down Expand Up @@ -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,
&current_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,
&current_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(&current_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<String>,
) -> io::Result<Vec<String>> {
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<String> = Vec::with_capacity(result.len());

for i in result {
chosen_priorites.push(possible_priorities[i].to_string())
}

Ok(chosen_priorites)
}

0 comments on commit eefb63e

Please sign in to comment.