diff options
author | Ruben Pollan | 2018-12-10 16:16:28 +0100 |
---|---|---|
committer | Ruben Pollan | 2018-12-10 16:16:28 +0100 |
commit | d0bb1ac385d1e81c07317f3148c4e2e6ddae3cfd (patch) | |
tree | b8aace05922c3d426b132fe477654cda89ff9c80 /src | |
parent | 0afdb0b34eeefff033819f4f0930c960af8e17f8 (diff) |
Pretty print events
Diffstat (limited to 'src')
-rw-r--r-- | src/date.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 54 |
2 files changed, 71 insertions, 1 deletions
diff --git a/src/date.rs b/src/date.rs index 45a0c28..a4559e7 100644 --- a/src/date.rs +++ b/src/date.rs @@ -61,6 +61,24 @@ impl Date { Ok(date) } + pub fn format(&self, fmt: &str) -> String { + match *self { + Date::Time(t) => t.format(fmt).to_string(), + Date::AllDay(d) => d.format(fmt).to_string(), + } + } + + pub fn same_day(&self, other: &Date) -> bool { + self.day() == other.day() && self.month() == other.month() && self.year() == self.year() + } + + pub fn day(&self) -> u32 { + match *self { + Date::Time(t) => t.day(), + Date::AllDay(d) => d.day(), + } + } + pub fn month(&self) -> u32 { match *self { Date::Time(t) => t.month(), diff --git a/src/main.rs b/src/main.rs index 013d744..707b814 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,17 @@ extern crate almanac; extern crate itertools; +extern crate colored; use std::env; use std::io::BufReader; use std::fs::File; use itertools::Itertools; +use colored::*; use almanac::Calendar; use almanac::Date; use almanac::Duration; +use almanac::Event; fn main() { let mut args = env::args().skip(1); @@ -22,8 +25,30 @@ fn main() { .skip_while(|e| e.end_date() < first) .take_while(|e| e.start <= last); + let mut day = Date::new(); + let mut unfinish = vec![]; for event in events { - println!("{}", event); + if !day.same_day(&event.start) { + if !unfinish.is_empty() { + while !day.same_day(&event.start) { + day = day + Duration::days(1); + print_day(day); + for (i, event) in unfinish.clone().iter().enumerate() { + print_event(event); + if event.end_date() <= day + Duration::days(1) { + unfinish.remove(i); + } + } + } + } else { + day = event.start.clone(); + print_day(day); + } + } + print_event(&event); + if event.end_date() > event.start + Duration::days(1) { + unfinish.push(event); + } } } @@ -44,3 +69,30 @@ fn ics_calendar(file_path: &str) -> Calendar { let buf = BufReader::new(file); Calendar::parse(buf).unwrap() } + +fn print_day(date: Date) { + println!("\n{}", date.format("%a %b %e %Y").green().bold()) +} + +fn print_event(event: &Event) { + let start = match event.start { + Date::Time(_) => event.start.format("%R"), + Date::AllDay(_) => "-----".to_string(), + }; + let end = match event.end_date() { + Date::Time(_) => event.end_date().format("%R"), + Date::AllDay(_) => "-----".to_string(), + }; + + println!( + " {}-{} {} {}", + start.yellow(), + end.yellow(), + event.summary, + event.location.purple() + ); + + if !event.description.is_empty() { + println!(" {} {}", " ".repeat(11), event.description.cyan()); + } +} |