Merge pull request #418 from kdheepak/fix-calendar-highlighting

This commit is contained in:
Dheepak Krishnamurthy 2022-03-20 00:53:52 -06:00 committed by GitHub
commit da49b3b22d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 17 deletions

View file

@ -33,7 +33,7 @@ use unicode_segmentation::Graphemes;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use chrono::{Datelike, Local, NaiveDate, NaiveDateTime, TimeZone, Timelike}; use chrono::{Datelike, FixedOffset, Local, NaiveDate, NaiveDateTime, TimeZone, Timelike};
use anyhow::Context as AnyhowContext; use anyhow::Context as AnyhowContext;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
@ -758,17 +758,21 @@ impl TaskwarriorTui {
} }
} }
pub fn get_dates_with_styles(&self) -> Vec<(NaiveDate, Style)> { pub fn get_dates_with_styles(&self) -> Vec<(chrono::Date<FixedOffset>, Style)> {
let mut tasks_with_styles = vec![];
if !self.tasks.is_empty() { if !self.tasks.is_empty() {
let tasks = &self.tasks; let tasks = &self.tasks;
let tasks_with_due_dates = tasks.iter().filter(|t| t.due().is_some()); tasks
.iter()
tasks_with_styles .filter_map(|t| t.due().map(|d| (d.clone(), self.style_for_task(t))))
.extend(tasks_with_due_dates.map(|t| (t.due().unwrap().clone().date(), self.style_for_task(t)))); .map(|(d, t)| {
let now = Local::now();
let reference = TimeZone::from_utc_datetime(now.offset(), &d);
(reference.date(), t)
})
.collect()
} else {
vec![]
} }
tasks_with_styles
} }
pub fn get_position(lb: &LineBuffer) -> usize { pub fn get_position(lb: &LineBuffer) -> usize {
@ -4749,7 +4753,9 @@ mod tests {
let backend = TestBackend::new(80, 20); let backend = TestBackend::new(80, 20);
let mut terminal = Terminal::new(backend).unwrap(); let mut terminal = Terminal::new(backend).unwrap();
app.render(&mut terminal).unwrap(); app.render(&mut terminal).unwrap();
dbg!(app.get_dates_with_styles());
println!("{}", buffer_view(terminal.backend().buffer())); println!("{}", buffer_view(terminal.backend().buffer()));
} }

View file

@ -5,7 +5,7 @@ use std::fmt;
const COL_WIDTH: usize = 21; const COL_WIDTH: usize = 21;
use chrono::{Datelike, Duration, Local, Month, NaiveDate, NaiveDateTime, TimeZone}; use chrono::{format::Fixed, Date, Datelike, Duration, FixedOffset, Local, Month, NaiveDate, NaiveDateTime, TimeZone};
use tui::{ use tui::{
buffer::Buffer, buffer::Buffer,
@ -24,7 +24,7 @@ pub struct Calendar<'a> {
pub month: u32, pub month: u32,
pub style: Style, pub style: Style,
pub months_per_row: usize, pub months_per_row: usize,
pub date_style: Vec<(NaiveDate, Style)>, pub date_style: Vec<(Date<FixedOffset>, Style)>,
pub today_style: Style, pub today_style: Style,
pub title_background_color: Color, pub title_background_color: Color,
} }
@ -70,7 +70,7 @@ impl<'a> Calendar<'a> {
self self
} }
pub fn date_style(mut self, date_style: Vec<(NaiveDate, Style)>) -> Self { pub fn date_style(mut self, date_style: Vec<(Date<FixedOffset>, Style)>) -> Self {
self.date_style = date_style; self.date_style = date_style;
self self
} }
@ -112,10 +112,10 @@ impl<'a> Widget for Calendar<'a> {
let months: Vec<_> = (0..12).collect(); let months: Vec<_> = (0..12).collect();
let mut days: Vec<_> = months let mut days: Vec<(Date<FixedOffset>, Date<FixedOffset>)> = months
.iter() .iter()
.map(|i| { .map(|i| {
let first = NaiveDate::from_ymd(year, i + 1, 1); let first = Date::from_utc(NaiveDate::from_ymd(year, i + 1, 1), *Local::now().offset());
( (
first, first,
first - Duration::days(i64::from(first.weekday().num_days_from_sunday())), first - Duration::days(i64::from(first.weekday().num_days_from_sunday())),
@ -193,13 +193,13 @@ impl<'a> Widget for Calendar<'a> {
if let Some(i) = index { if let Some(i) = index {
style = self.date_style[i].1; style = self.date_style[i].1;
} }
if d.1 == Local::today().naive_local() { if d.1 == Local::today() {
buf.set_string(x, y, s, self.today_style); buf.set_string(x, y, s, self.today_style);
} else { } else {
buf.set_string(x, y, s, style); buf.set_string(x, y, s, style);
} }
x += 3; x += 3;
d.1 += Duration::days(1); d.1 = Date::from_utc(d.1.naive_local() + Duration::days(1), *Local::now().offset());
} }
moredays |= d.0.month() == d.1.month() || d.1 < d.0; moredays |= d.0.month() == d.1.month() || d.1 < d.0;
} }
@ -219,7 +219,10 @@ impl<'a> Widget for Calendar<'a> {
&mut months &mut months
.iter() .iter()
.map(|i| { .map(|i| {
let first = NaiveDate::from_ymd(self.year + new_year as i32, i + 1, 1); let first = Date::from_utc(
NaiveDate::from_ymd(self.year + new_year as i32, i + 1, 1),
*Local::now().offset(),
);
( (
first, first,
first - Duration::days(i64::from(first.weekday().num_days_from_sunday())), first - Duration::days(i64::from(first.weekday().num_days_from_sunday())),