Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configure command for preferences #50

Merged
merged 3 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}