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 support for Preferences #47

Merged
merged 1 commit 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
96 changes: 89 additions & 7 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 @@ -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"
37 changes: 23 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <shaishavgandhi05@gmail.com>")
.version("0.1.0")
.subcommand(SubCommand::with_name("diffs")
.version("0.1.0")
.version(version)
.subcommand(App::new("diffs")
.version(version)
.author("Shaishav <shaishavgandhi05@gmail.com>")
.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 <shaishavgandhi05@gmail.com>")
.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 <shaishavgandhi05@gmail.com>"))
.get_matches();

Expand All @@ -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);
}
}
32 changes: 32 additions & 0 deletions src/preferences.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use serde::{Deserialize, Serialize};

/// Get user's preferences
pub fn get_preferences() -> Result<Preferences, String> {
let prefs = confy::load::<Preferences>("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<String>,
pub default_task_priority: Vec<String>,
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
}
}
}
12 changes: 9 additions & 3 deletions src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> = 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!();
Expand Down
13 changes: 8 additions & 5 deletions src/tasks.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<Maniphest>, String> {
Expand Down Expand Up @@ -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();
Expand Down