diff --git a/Cargo.lock b/Cargo.lock index ab0c7c3..6b97c5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -356,7 +356,7 @@ dependencies = [ [[package]] name = "fab" -version = "0.4.0" +version = "0.4.1" dependencies = [ "clap", "clap_generate", diff --git a/Cargo.toml b/Cargo.toml index 43d7504..bf2a65d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fab" -version = "0.4.0" +version = "0.4.1" authors = ["Shaishav Gandhi "] edition = "2018" diff --git a/src/cli.rs b/src/cli.rs index dee2cee..657051a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,25 +1,25 @@ use crate::preferences::Preferences; use clap::{App, Arg}; +pub const VERSION: &str = "0.4.1"; + /// Builds the App with commands and defaults. pub fn build_cli(preferences: &Preferences) -> App { - let version = "0.4.0"; - let default_task_priority: &Vec<&str> = &preferences .default_task_priority .iter() .map(std::ops::Deref::deref) .collect(); - let default_limit = preferences.default_limit.as_str(); + let default_limit = preferences.default_limit_str.as_str(); let default_sort = preferences.default_sort.as_ref(); App::new("Fab") .author("Shaishav ") - .version(version) + .version(VERSION) .subcommand( App::new("diffs") - .version(version) + .version(VERSION) .author("Shaishav ") .about("Commands related to your differential revisions") .arg( @@ -32,7 +32,7 @@ pub fn build_cli(preferences: &Preferences) -> App { .subcommand( App::new("tasks") .about("Commands related to maniphest tasks") - .version(version) + .version(VERSION) .author("Shaishav ") .arg( Arg::with_name("priority") @@ -78,7 +78,7 @@ pub fn build_cli(preferences: &Preferences) -> App { .subcommand( App::new("summary") .about("Gives a snapshot of what is relevant to you in the moment") - .version(version) + .version(VERSION) .author("Shaishav "), ) .subcommand( @@ -90,19 +90,19 @@ pub fn build_cli(preferences: &Preferences) -> App { .long("reset") .help("Reset preferences to their default value"), ) - .version(version) + .version(VERSION) .author("Shaishav "), ) .subcommand( App::new("autocomplete") .about("Add autocomplete suggestions for vim") - .version(version) + .version(VERSION) .author("Shaishav "), ) .subcommand( App::new("generate-bash-completions") .about("Generate the bash completion files for fab") - .version(version) + .version(VERSION) .author("Shaishav "), ) } diff --git a/src/main.rs b/src/main.rs index 7a6a9fa..3cb9a6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #[macro_use] extern crate serde_json; +use crate::preferences::Preferences; use clap_generate::generate; use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh}; use failure::Error; @@ -20,6 +21,9 @@ const NO_BORDER_PRESET: &str = " "; fn main() -> Result<(), Error> { let preferences = preferences::get_preferences()?; + // Migrate any newer fields, assign them values and store them + let preferences = migrate_preferences(preferences)?; + let app = cli::build_cli(&preferences); let matches = &app.get_matches(); @@ -46,3 +50,19 @@ fn main() -> Result<(), Error> { } Ok(()) } + +/// Migrates any missing fields that were added as the application progresses. +/// +/// It's important to remember to add any default values otherwise confy will blow +/// up with a BadTomlError +fn migrate_preferences(preferences: Preferences) -> Result { + let preferences = Preferences { + default_limit_str: preferences.default_limit.to_string(), + default_limit: preferences.default_limit, + default_sort: preferences.default_sort, + default_task_priority: preferences.default_task_priority, + summary_task_priority: preferences.summary_task_priority, + }; + preferences::set_preferences(&preferences)?; + Ok(preferences) +} diff --git a/src/preferences.rs b/src/preferences.rs index ca84f60..349fb5a 100644 --- a/src/preferences.rs +++ b/src/preferences.rs @@ -22,8 +22,14 @@ pub fn set_preferences(preferences: &Preferences) -> Result<(), Error> { pub struct Preferences { pub summary_task_priority: Vec, pub default_task_priority: Vec, - pub default_limit: String, + pub default_limit: i32, pub default_sort: String, + #[serde(default = "default_limit")] + pub default_limit_str: String, +} + +fn default_limit() -> String { + String::from("20") } impl ::std::default::Default for Preferences { @@ -31,7 +37,8 @@ impl ::std::default::Default for Preferences { Self { summary_task_priority: vec![String::from("high"), String::from("needs-triage")], default_task_priority: vec![String::from("high")], - default_limit: "20".to_string(), + default_limit: 20, + default_limit_str: "20".to_string(), default_sort: "updated".to_string(), } } @@ -88,7 +95,7 @@ pub fn process_configuration(matches: &ArgMatches) -> Result<(), Error> { ); let default_limit = Input::with_theme(&ColorfulTheme::default()) - .with_initial_text(¤t_preferences.default_limit.as_str()) + .with_initial_text(¤t_preferences.default_limit_str.as_str()) .interact()?; println!( @@ -110,6 +117,7 @@ pub fn process_configuration(matches: &ArgMatches) -> Result<(), Error> { summary_task_priority: summary_priorities, default_task_priority: default_task_priorities, default_limit, + default_limit_str: format!("{}", default_limit), default_sort: default_sort.to_string(), }; @@ -118,7 +126,8 @@ pub fn process_configuration(matches: &ArgMatches) -> Result<(), Error> { fn reset_preferences() -> Result<(), Error> { let default_preferences = Preferences { - default_limit: "20".to_string(), + default_limit: 20, + default_limit_str: "20".to_string(), default_task_priority: vec![String::from("high")], summary_task_priority: vec![String::from("high")], default_sort: "updated".to_string(), diff --git a/src/summary.rs b/src/summary.rs index b76a560..8cbaa24 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -24,7 +24,7 @@ pub fn process_summary( get_needs_review_diffs(config), get_authored_diffs(config), get_tasks( - preferences.default_limit.as_str(), + preferences.default_limit_str.as_str(), &priorities, &preferences.default_sort, &status, diff --git a/src/tasks.rs b/src/tasks.rs index c5d30d7..2f9e471 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -81,7 +81,7 @@ fn process_list_tasks( config: &FabConfig, preferences: &Preferences, ) -> Result<(), Error> { - let pref_limit = preferences.default_limit.as_str(); + let pref_limit = preferences.default_limit_str.as_str(); let limit = matches.value_of("limit").unwrap_or(pref_limit); let priorities: Vec<_> = matches.values_of("priority")