From 00989a0eb5982efd660b4e1a7148a6f712d3e6f0 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Wed, 28 Apr 2021 12:25:16 -0600 Subject: [PATCH] Add default report command line argument --- src/app.rs | 10 ++++++---- src/config.rs | 10 +++++----- src/main.rs | 15 ++++++++++++--- src/task_report.rs | 20 +++++++++++++------- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index 1a588f3..6796a95 100644 --- a/src/app.rs +++ b/src/app.rs @@ -199,10 +199,11 @@ pub struct TaskwarriorTuiApp { pub command_history_context: HistoryContext, pub completion_list: CompletionList, pub show_completion_pane: bool, + pub report: String, } impl TaskwarriorTuiApp { - pub fn new() -> Result { + pub fn new(report: &str) -> Result { let output = std::process::Command::new("task") .arg("rc.color=off") .arg("show") @@ -224,7 +225,7 @@ impl TaskwarriorTuiApp { } let data = String::from_utf8_lossy(&output.stdout); - let c = Config::new(&data)?; + let c = Config::new(&data, report)?; let kc = KeyConfig::new(&data)?; let (w, h) = crossterm::terminal::size()?; @@ -251,7 +252,7 @@ impl TaskwarriorTuiApp { task_details_scroll: 0, task_report_show_info: c.uda_task_report_show_info, config: c, - task_report_table: TaskReportTable::new(&data)?, + task_report_table: TaskReportTable::new(&data, report)?, calendar_year: Local::today().year(), help_popup: Help::new(), contexts: vec![], @@ -263,6 +264,7 @@ impl TaskwarriorTuiApp { command_history_context: HistoryContext::new("command.history"), completion_list: CompletionList::with_items(vec![]), show_completion_pane: false, + report: report.to_string(), }; for c in app.config.filter.chars() { @@ -998,7 +1000,7 @@ impl TaskwarriorTuiApp { pub fn update(&mut self, force: bool) -> Result<()> { if force || self.dirty || self.tasks_changed_since(self.last_export)? { self.last_export = Some(std::time::SystemTime::now()); - self.task_report_table.export_headers(None)?; + self.task_report_table.export_headers(None, &self.report)?; let _ = self.export_tasks(); self.export_contexts()?; self.update_tags(); diff --git a/src/config.rs b/src/config.rs index f93aba7..97c964e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -64,7 +64,7 @@ pub struct Config { } impl Config { - pub fn new(data: &str) -> Result { + pub fn new(data: &str, report: &str) -> Result { let bool_collection = Self::get_bool_collection(); let enabled = true; @@ -72,7 +72,7 @@ impl Config { let print_empty_columns = bool_collection.get("print_empty_columns").cloned().unwrap_or(false); let color = Self::get_color_collection(data); - let filter = Self::get_filter(data); + let filter = Self::get_filter(data, report); let data_location = Self::get_data_location(data); let due = Self::get_due(data); let rule_precedence_color = Self::get_rule_precedence_color(data); @@ -339,9 +339,9 @@ impl Config { data.split(',').map(|s| s.to_string()).collect::>() } - fn get_filter(data: &str) -> String { - let filter = Self::get_config("report.next.filter", data) - .context("Unable to parse `task show report.next.filter`.") + fn get_filter(data: &str, report: &str) -> String { + let filter = Self::get_config(format!("report.{}.filter", report).as_str(), data) + .context(format!("Unable to parse `task show report.{}.filter`.", report)) .unwrap(); format!("{} ", filter) } diff --git a/src/main.rs b/src/main.rs index ecba1b6..f57b65a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,21 +70,30 @@ fn main() { .help("Sets a custom config file") .takes_value(true), ) + .arg( + Arg::with_name("report") + .short("r") + .long("report") + .value_name("STRING") + .help("Sets default report") + .takes_value(true), + ) .get_matches(); let config = matches.value_of("config").unwrap_or("~/.taskrc"); - task::block_on(tui_main(config)).expect( + let report = matches.value_of("report").unwrap_or("next"); + task::block_on(tui_main(config, report)).expect( "[taskwarrior-tui error]. Please report as a github issue on https://github.com/kdheepak/taskwarrior-tui", ); } -async fn tui_main(_config: &str) -> Result<()> { +async fn tui_main(_config: &str, report: &str) -> Result<()> { panic::set_hook(Box::new(|panic_info| { destruct_terminal(); better_panic::Settings::auto().create_panic_handler()(panic_info); })); - let maybeapp = TaskwarriorTuiApp::new(); + let maybeapp = TaskwarriorTuiApp::new(report); if maybeapp.is_err() { destruct_terminal(); return Err(maybeapp.err().unwrap()); diff --git a/src/task_report.rs b/src/task_report.rs index e929c8c..dbb1be9 100644 --- a/src/task_report.rs +++ b/src/task_report.rs @@ -43,7 +43,7 @@ pub struct TaskReportTable { } impl TaskReportTable { - pub fn new(data: &str) -> Result { + pub fn new(data: &str, report: &str) -> Result { let virtual_tags = vec![ "PROJECT", "BLOCKED", @@ -86,24 +86,27 @@ impl TaskReportTable { virtual_tags: virtual_tags.iter().map(|s| s.to_string()).collect::>(), description_width: 100, }; - task_report_table.export_headers(Some(data))?; + task_report_table.export_headers(Some(data), report)?; Ok(task_report_table) } - pub fn export_headers(&mut self, data: Option<&str>) -> Result<()> { + pub fn export_headers(&mut self, data: Option<&str>, report: &str) -> Result<()> { self.columns = vec![]; self.labels = vec![]; let data = match data { Some(s) => s.to_string(), None => { - let output = Command::new("task").arg("show").arg("report.next.columns").output()?; + let output = Command::new("task") + .arg("show") + .arg(format!("report.{}.columns", report)) + .output()?; String::from_utf8_lossy(&output.stdout).into_owned() } }; for line in data.split('\n') { - if line.starts_with("report.next.columns") { + if line.starts_with(format!("report.{}.columns", report).as_str()) { let column_names = line.split(' ').collect::>()[1]; for column in column_names.split(',') { self.columns.push(column.to_string()); @@ -111,11 +114,14 @@ impl TaskReportTable { } } - let output = Command::new("task").arg("show").arg("report.next.labels").output()?; + let output = Command::new("task") + .arg("show") + .arg(format!("report.{}.labels", report)) + .output()?; let data = String::from_utf8_lossy(&output.stdout); for line in data.split('\n') { - if line.starts_with("report.next.labels") { + if line.starts_with(format!("report.{}.labels", report).as_str()) { let label_names = line.split(' ').collect::>()[1]; for label in label_names.split(',') { self.labels.push(label.to_string());