From fd21ae43c01881ae78736d4985e89b8afcdbc3a7 Mon Sep 17 00:00:00 2001 From: Kohlbern Jary Date: Tue, 23 Aug 2022 23:15:45 -0400 Subject: [PATCH] Added .taskrc weekstart value support --- src/app.rs | 3 ++- src/calendar.rs | 21 +++++++++++++++++++-- src/config.rs | 12 ++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index a759dda..3087f1a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -558,7 +558,8 @@ impl TaskwarriorTui { .today_style(self.config.uda_style_calendar_today) .year(self.calendar_year) .date_style(self.get_dates_with_styles()) - .months_per_row(self.config.uda_calendar_months_per_row); + .months_per_row(self.config.uda_calendar_months_per_row) + .start_on_monday(self.config.weekstart); c.title_background_color = self.config.uda_style_calendar_title.bg.unwrap_or(Color::Reset); f.render_widget(c, layout); } diff --git a/src/calendar.rs b/src/calendar.rs index 6e54076..82648f6 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -26,6 +26,7 @@ pub struct Calendar<'a> { pub months_per_row: usize, pub date_style: Vec<(Date, Style)>, pub today_style: Style, + pub start_on_monday: bool, pub title_background_color: Color, } @@ -41,6 +42,7 @@ impl<'a> Default for Calendar<'a> { month, date_style: vec![], today_style: Style::default(), + start_on_monday: false, title_background_color: Color::Reset, } } @@ -84,6 +86,11 @@ impl<'a> Calendar<'a> { self.months_per_row = months_per_row; self } + + pub fn start_on_monday(mut self, start_on_monday: bool) -> Self { + self.start_on_monday = start_on_monday; + self + } } impl<'a> Widget for Calendar<'a> { @@ -116,9 +123,14 @@ impl<'a> Widget for Calendar<'a> { .iter() .map(|i| { let first = Date::from_utc(NaiveDate::from_ymd(year, i + 1, 1), *Local::now().offset()); + let num_days = if self.start_on_monday { + first.weekday().num_days_from_monday() + } else { + first.weekday().num_days_from_sunday() + }; ( first, - first - Duration::days(i64::from(first.weekday().num_days_from_sunday())), + first - Duration::days(i64::from(num_days)), ) }) .collect(); @@ -165,10 +177,15 @@ impl<'a> Widget for Calendar<'a> { for d in days.iter_mut().take(endm).skip(start_m) { let m = d.0.month() as usize; let style = Style::default().bg(self.title_background_color); + let days_string = if self.start_on_monday { + "Mo Tu We Th Fr Sa Su" + } else { + "Su Mo Tu We Th Fr Sa" + }; buf.set_string( x as u16, y, - "Su Mo Tu We Th Fr Sa", + days_string, style.add_modifier(Modifier::UNDERLINED), ); x += 21 + 1; diff --git a/src/config.rs b/src/config.rs index c587984..351c152 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,6 +55,7 @@ pub struct Config { pub obfuscate: bool, pub print_empty_columns: bool, pub due: usize, + pub weekstart: bool, pub rule_precedence_color: Vec, pub uda_priority_values: Vec, pub uda_tick_rate: u64, @@ -118,6 +119,7 @@ impl Config { }; let data_location = Self::get_data_location(data); let due = Self::get_due(data); + let weekstart = Self::get_weekstart(data); let rule_precedence_color = Self::get_rule_precedence_color(data); let uda_priority_values = Self::get_uda_priority_values(data); let uda_tick_rate = Self::get_uda_tick_rate(data); @@ -184,6 +186,7 @@ impl Config { obfuscate, print_empty_columns, due, + weekstart, rule_precedence_color, uda_priority_values, uda_tick_rate, @@ -461,6 +464,15 @@ impl Config { .unwrap_or(7) } + fn get_weekstart(data: &str) -> bool { + let data = Self::get_config("weekstart", data).unwrap_or_default(); + if data.eq_ignore_ascii_case("Monday") { + return true; + } else { + return false; + } + } + fn get_rule_precedence_color(data: &str) -> Vec { let data = Self::get_config("rule.precedence.color", data) .context("Unable to parse `task show rule.precedence.color`.")