diff --git a/src/app.rs b/src/app.rs index 5af9252..acbd6e7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -438,7 +438,7 @@ impl TTApp { .constraints([Constraint::Min(0)].as_ref()) .split(f.size()); let today = Local::today(); - let c = &Calendar::new(today.year().into()).to_string()[..]; + let c = &Calendar::default().to_string()[..]; let p = Paragraph::new(Text::from(c)) .alignment(Alignment::Left) .block(Block::default().borders(Borders::ALL).title("Calendar")); diff --git a/src/calendar.rs b/src/calendar.rs index 5ffa51e..893462a 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -8,6 +8,19 @@ const COL_WIDTH: usize = 21; use Day::*; use Month::*; +use chrono::{Datelike, Local, NaiveDateTime, TimeZone}; + +use tui::{ + buffer::Buffer, + layout::Rect, + style::Style, + symbols, + widgets::{Block, Widget}, +}; + +use std::cmp::min; +use unicode_width::UnicodeWidthStr; + #[derive(Clone, Copy, Debug)] #[repr(u8)] pub enum Day { @@ -132,13 +145,21 @@ impl fmt::Display for Month { } } -pub struct Calendar { +#[derive(Debug, Clone)] +pub struct Calendar<'a> { + pub block: Option>, pub year: i64, + pub style: Style, } -impl Calendar { - pub fn new(year: i64) -> Self { - Self { year } +impl<'a> Default for Calendar<'a> { + fn default() -> Calendar<'a> { + let today = Local::today(); + Calendar { + block: None, + year: today.year().into(), + style: Default::default(), + } } } @@ -146,8 +167,87 @@ fn is_leap_year(year: i64) -> bool { (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 } -impl fmt::Display for Calendar { +// impl <'a> Widget for Calendar<'a> { +// fn render(mut self, area: Rect, buf: &mut Buffer) { +// buf.set_style(area, self.style); + +// let area = match self.block.take() { +// Some(b) => { +// let inner_area = b.inner(area); +// b.render(area, buf); +// inner_area +// } +// None => area, +// }; + +// if area.height < 7 { +// return +// } + +// let style = self.style; +// let today = Local::today(); +// let cols = area.width; + +// let year = self.year; +// let leap_year = is_leap_year(year); +// let months = [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]; +// let mut dates = [ +// 0..Jan.len(), +// 0..Feb.leap_len(leap_year), +// 0..Mar.len(), +// 0..Apr.len(), +// 0..May.len(), +// 0..Jun.len(), +// 0..Jul.len(), +// 0..Aug.len(), +// 0..Sep.len(), +// 0..Oct.len(), +// 0..Nov.len(), +// 0..Dec.len(), +// ]; +// let chunks = dates.chunks_mut(cols as usize).zip(months.chunks(cols as usize)); +// let mut y = 0; +// for (days_chunk, months) in chunks { +// for month in months { +// write!(f, "{:>1$} ", month, COL_WIDTH)?; +// } +// y += 1; +// for month in months { +// write!(f, "{:>1$} ", " S M T W T F S", COL_WIDTH)?; +// } +// y += 1; +// for (days, mon) in days_chunk.iter_mut().zip(months.iter()) { +// let first_day = mon.first_day(year) as u8; +// for _ in 0..(first_day) { +// f.write_str(" ")?; +// } +// for _ in 0..(7 - first_day) { +// write!(f, "{:>3}", days.next().unwrap() + 1)?; +// } +// f.write_str(" ")?; +// } +// y += 1; +// while !days_chunk.iter().all(|r| r.start == r.end) { +// for days in days_chunk.iter_mut() { +// for _ in 0..7 { +// match days.next() { +// Some(s) => write!(f, "{:>3}", s + 1)?, +// None => f.write_str(" ")?, +// } +// } +// f.write_str(" ")?; +// } +// y += 1; +// } +// y += 1; +// } + +// } +// } + +impl<'a> fmt::Display for Calendar<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let today = Local::today(); let cols = f.width().unwrap_or(3); let year = self.year; let leap_year = is_leap_year(year);