aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Pollan2018-12-10 16:16:28 +0100
committerRuben Pollan2018-12-10 16:16:28 +0100
commitd0bb1ac385d1e81c07317f3148c4e2e6ddae3cfd (patch)
treeb8aace05922c3d426b132fe477654cda89ff9c80
parent0afdb0b34eeefff033819f4f0930c960af8e17f8 (diff)
Pretty print events
Diffstat (limited to '')
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml1
-rw-r--r--src/date.rs18
-rw-r--r--src/main.rs54
4 files changed, 82 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6335f17..e9f2f77 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12,6 +12,7 @@ version = "0.1.0"
dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono-tz 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "colored 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ical 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -67,6 +68,14 @@ dependencies = [
]
[[package]]
+name = "colored"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "either"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -226,6 +235,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
"checksum chrono-tz 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44420a821d3075c6b4dcdba557104274a240b5b6e323dc17136507e96ca2db59"
+"checksum colored 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc0a60679001b62fb628c4da80e574b9645ab4646056d7c9018885efffe45533"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8"
"checksum ical 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a985f921bf84e6d00286f0f41363e2d39978370d8d8868c114002d863c27094f"
diff --git a/Cargo.toml b/Cargo.toml
index 6f5bdb3..4efc2cd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,7 @@ authors = ["meskio <meskio@sindominio.net>"]
chrono = "0.4"
chrono-tz = "0.5"
itertools = "0.7"
+colored = "1.6"
[dependencies.ical]
version = "0.5.*"
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());
+ }
+}