Initial conversion of Calendar into widget

This commit is contained in:
Dheepak Krishnamurthy 2020-10-21 09:42:03 -06:00
parent c189e74067
commit 714a6a8e61
2 changed files with 106 additions and 6 deletions

View file

@ -438,7 +438,7 @@ impl TTApp {
.constraints([Constraint::Min(0)].as_ref()) .constraints([Constraint::Min(0)].as_ref())
.split(f.size()); .split(f.size());
let today = Local::today(); 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)) let p = Paragraph::new(Text::from(c))
.alignment(Alignment::Left) .alignment(Alignment::Left)
.block(Block::default().borders(Borders::ALL).title("Calendar")); .block(Block::default().borders(Borders::ALL).title("Calendar"));

View file

@ -8,6 +8,19 @@ const COL_WIDTH: usize = 21;
use Day::*; use Day::*;
use Month::*; 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)] #[derive(Clone, Copy, Debug)]
#[repr(u8)] #[repr(u8)]
pub enum Day { 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<Block<'a>>,
pub year: i64, pub year: i64,
pub style: Style,
} }
impl Calendar { impl<'a> Default for Calendar<'a> {
pub fn new(year: i64) -> Self { fn default() -> Calendar<'a> {
Self { year } 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 (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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let today = Local::today();
let cols = f.width().unwrap_or(3); let cols = f.width().unwrap_or(3);
let year = self.year; let year = self.year;
let leap_year = is_leap_year(year); let leap_year = is_leap_year(year);