diff --git a/Cargo.lock b/Cargo.lock index cfeed37..e3c3f11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,9 +2,9 @@ # It is not intended for manual editing. [[package]] name = "ansi_term" -version = "0.11.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ "winapi 0.3.8", ] @@ -134,19 +134,33 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "clap" -version = "2.33.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +version = "3.0.0-beta.1" +source = "git+https://github.com/clap-rs/clap/#4b5d7722bfbd7c6c5f78d0fe1d0aaae46c98e1a8" dependencies = [ "ansi_term", "atty", "bitflags", + "clap_derive", + "indexmap", + "lazy_static", "strsim", "textwrap", "unicode-width", "vec_map", ] +[[package]] +name = "clap_derive" +version = "3.0.0-beta.1" +source = "git+https://github.com/clap-rs/clap/#4b5d7722bfbd7c6c5f78d0fe1d0aaae46c98e1a8" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "clicolors-control" version = "1.0.1" @@ -181,6 +195,17 @@ dependencies = [ "strum_macros", ] +[[package]] +name = "confy" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2913470204e9e8498a0f31f17f90a0de801ae92c8c5ac18c49af4819e6786697" +dependencies = [ + "directories", + "serde", + "toml", +] + [[package]] name = "console" version = "0.10.0" @@ -255,6 +280,16 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "directories" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" +dependencies = [ + "cfg-if", + "dirs-sys", +] + [[package]] name = "dirs" version = "2.0.2" @@ -314,6 +349,7 @@ version = "0.1.0" dependencies = [ "clap", "comfy-table", + "confy", "console", "dirs", "reqwest", @@ -818,6 +854,32 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" +[[package]] +name = "proc-macro-error" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "syn-mid", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.9" @@ -1181,9 +1243,9 @@ checksum = "5c2fb2ec9bcd216a5b0d0ccf31ab17b5ed1d627960edff65bbe95d3ce221cefc" [[package]] name = "strsim" -version = "0.8.0" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" [[package]] name = "strum" @@ -1214,6 +1276,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tempdir" version = "0.3.7" @@ -1308,6 +1381,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 896b14b..4a06ab0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,11 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = "2.33.0" +clap = { git = "https://github.com/clap-rs/clap/" } reqwest = {version = "0.10.4", features = ["blocking","json"] } serde_json = "1.0.48" 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 diff --git a/src/main.rs b/src/main.rs index 719b1ee..2b7cbdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,49 +3,58 @@ extern crate serde_json; use clap; -use clap::{App, SubCommand, Arg}; +use clap::{App, Arg}; mod structs; mod diffs; mod tasks; mod network; mod auth; mod summary; +mod preferences; const WHO_AM_I: &str = "api/user.whoami"; /// Preset for comfy-table so that it styles the table for no borders const NO_BORDER_PRESET: &str = " "; fn main() { + let version = "0.2.0"; + let preferences = preferences::get_preferences().unwrap(); + + let default_task_priority: &Vec<&str> = &preferences.default_task_priority + .iter() + .map(std::ops::Deref::deref) + .collect(); + let matches = App::new("Fab") .author("Shaishav ") - .version("0.1.0") - .subcommand(SubCommand::with_name("diffs") - .version("0.1.0") + .version(version) + .subcommand(App::new("diffs") + .version(version) .author("Shaishav ") .about("Commands related to your differential revisions") .arg(Arg::with_name("needs-review") - .short("n") + .short('n') .long("needs-review") .help("Show diffs that need your review"))) - .subcommand(SubCommand::with_name("tasks") + .subcommand(App::new("tasks") .about("Commands related to maniphest tasks") - .version("0.1.0") + .version(version) .author("Shaishav ") .arg(Arg::with_name("priority") - .short("p") + .short('p') .long("priority") .possible_values(&["unbreak-now", "needs-triage", "high", "normal", "low", "wishlist"]) .help("Specify the priority of the task") - .default_value("high") + .default_values(default_task_priority) .multiple(true)) .arg(Arg::with_name("limit") - .short("l") + .short('l') .long("limit") .help("limit results by a value") .default_value("20"))) - .subcommand(SubCommand::with_name("summary") + .subcommand(App::new("summary") .about("Gives a snapshot of what is relevant to you in the moment") - .version("0.1.0") + .version(version) .author("Shaishav ")) .get_matches(); @@ -59,8 +68,8 @@ fn main() { if let Some(matches) = matches.subcommand_matches("diffs") { diffs::process_diff_command(matches, &config) } else if let Some(matches) = matches.subcommand_matches("tasks") { - tasks::process_task_command(matches, &config) + tasks::process_task_command(matches, &config, &preferences) } else if let Some(matches) = matches.subcommand_matches("summary") { - summary::process_summary(matches, &config); + summary::process_summary(matches, &config, &preferences); } } diff --git a/src/preferences.rs b/src/preferences.rs new file mode 100644 index 0000000..b2cb4f0 --- /dev/null +++ b/src/preferences.rs @@ -0,0 +1,32 @@ +use serde::{Deserialize, Serialize}; + +/// Get user's preferences +pub fn get_preferences() -> Result { + let prefs = confy::load::("fab"); + match prefs { + Ok(pref) => Result::Ok(pref), + Err(_err) => Result::Err(String::from("Couldn't load preferences")) + } +} + +/// Stores new preferences to disk +pub fn set_preferences(preferences: &Preferences) { + confy::store("fab", preferences).expect("Failed to set preferences"); +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Preferences { + pub summary_task_priority: Vec, + pub default_task_priority: Vec, + pub default_limit: i32 +} + +impl ::std::default::Default for Preferences { + fn default() -> Self { + Self { + summary_task_priority: vec![String::from("high"), String::from("needs-triage")], + default_task_priority: vec![String::from("high")], + default_limit: 20 + } + } +} diff --git a/src/summary.rs b/src/summary.rs index 99cbcbc..d97e267 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -3,12 +3,18 @@ use crate::structs::FabConfig; use crate::diffs::{get_needs_review_diffs, render_diffs, get_authored_diffs}; use crate::tasks::{get_tasks, Priority, render_tasks}; use console::style; +use crate::preferences::Preferences; -pub fn process_summary(_matches: &ArgMatches, config: &FabConfig) { +pub fn process_summary(_matches: &ArgMatches, config: &FabConfig, preferences: &Preferences) { let needs_review_diffs = get_needs_review_diffs(config).unwrap(); let authored_diffs = get_authored_diffs(config).unwrap(); - let tasks = get_tasks("10", &[Priority::get_value_for_name("high").unwrap()], config) - .expect("Couldn't fetch tasks"); + + let priorities: Vec = preferences.summary_task_priority.iter() + .map(|priority| Priority::get_value_for_name(&priority).unwrap()) + .collect(); + + let tasks = get_tasks(preferences.default_limit.to_string().as_str(), + &priorities, config).expect("Couldn't fetch tasks"); println!("{}", style("Diffs that need your review").bold().underlined()); println!(); diff --git a/src/tasks.rs b/src/tasks.rs index c398bdf..3c191e6 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,14 +1,15 @@ -use clap::ArgMatches; +use clap::{ArgMatches}; use serde::{Deserialize}; use crate::structs::FabConfig; use comfy_table::{Table, ContentArrangement, Cell, CellAlignment, Attribute, Color}; use crate::{NO_BORDER_PRESET, auth}; use serde_json::{Map, Value}; +use crate::preferences::Preferences; const MANIPHEST_SEARCH: &str = "api/maniphest.search"; -pub fn process_task_command(matches: &ArgMatches, config: &FabConfig) { - process_list_tasks(matches, config) +pub fn process_task_command(matches: &ArgMatches, config: &FabConfig, preferences: &Preferences) { + process_list_tasks(matches, config, preferences) } pub fn get_tasks(limit: &str, priorities: &[i32], config: &FabConfig) -> Result, String> { @@ -61,8 +62,10 @@ pub fn render_tasks(tasks: &[Maniphest], config: &FabConfig) { println!("{}", table) } -fn process_list_tasks(matches: &ArgMatches, config: &FabConfig) { - let limit = matches.value_of("limit").expect("No limit specified for query"); +fn process_list_tasks(matches: &ArgMatches, config: &FabConfig, preferences: &Preferences) { + let pref_limit: &str = &preferences.default_limit.to_string(); + let limit = matches.value_of("limit").unwrap_or(pref_limit); + let priorities: Vec<_> = matches.values_of("priority") .expect("Couldn't parse priority. Must be one of ['unbreak-now', 'needs-triage', 'high', 'normal', 'low', 'wishlist']") .collect();