do print the running entries in ical format

This commit is contained in:
Abraham Toriz 2021-07-30 12:08:22 -05:00
parent 744d07b174
commit fc90f76390
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 27 additions and 15 deletions

View File

@ -43,7 +43,7 @@ impl Formatter {
Formatter::Csv => csv::print_formatted(entries, out, ids)?,
Formatter::Json => json::print_formatted(entries, out)?,
Formatter::Ids => ids::print_formatted(entries, out)?,
Formatter::Ical => ical::print_formatted(entries, out)?,
Formatter::Ical => ical::print_formatted(entries, out, now)?,
Formatter::Custom(name) => {
panic!("attempted custom formatter with name {} which is not implemented", name);
}

View File

@ -1,11 +1,13 @@
use std::io::Write;
use chrono::{DateTime, Utc};
use crate::models::Entry;
use crate::error::Result;
const ICAL_TIME_FORMAT: &'static str = "%Y%m%dT%H%M%SZ";
pub fn print_formatted<W: Write>(entries: Vec<Entry>, out: &mut W) -> Result<()> {
pub fn print_formatted<W: Write>(entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>) -> Result<()> {
writeln!(out, "BEGIN:VCALENDAR")?;
writeln!(out, "CALSCALE:GREGORIAN")?;
writeln!(out, "METHOD:PUBLISH")?;
@ -15,13 +17,13 @@ pub fn print_formatted<W: Write>(entries: Vec<Entry>, out: &mut W) -> Result<()>
let hostname = hostname::get()?;
let host = hostname.to_string_lossy();
for entry in entries.into_iter().filter(|e| e.end.is_some()) {
for entry in entries.into_iter() {
let uid = format!("tiempo-{id}", id=entry.id);
let note = entry.note.unwrap_or("".into());
writeln!(out, "BEGIN:VEVENT")?;
writeln!(out, "DESCRIPTION:{note}", note=note)?;
writeln!(out, "DTEND:{end}", end=entry.end.unwrap().format(ICAL_TIME_FORMAT).to_string())?;
writeln!(out, "DTEND:{end}", end=entry.end.unwrap_or(now).format(ICAL_TIME_FORMAT).to_string())?;
writeln!(out, "DTSTAMP:{start}", start=entry.start.format(ICAL_TIME_FORMAT).to_string())?;
writeln!(out, "DTSTART:{start}", start=entry.start.format(ICAL_TIME_FORMAT).to_string())?;
writeln!(out, "SEQUENCE:0")?;
@ -48,14 +50,15 @@ mod tests {
fn ical_format() {
let now = Utc::now();
let an_hour_ago = now - Duration::hours(1);
let half_an_hour_ago = now - Duration::minutes(30);
let entries = vec![
Entry::new_sample(1, an_hour_ago, Some(now)),
Entry::new_sample(2, an_hour_ago, None),
Entry::new_sample(1, an_hour_ago, Some(half_an_hour_ago)),
Entry::new_sample(2, half_an_hour_ago, None),
];
let mut out = Vec::new();
print_formatted(entries, &mut out).unwrap();
print_formatted(entries, &mut out, now).unwrap();
let host = hostname::get().unwrap();
let host = host.to_string_lossy();
@ -66,16 +69,25 @@ METHOD:PUBLISH
PRODID:tiempo-rs
VERSION:2.0
BEGIN:VEVENT
DESCRIPTION:{note}
DTEND:{end}
DTSTAMP:{start}
DTSTART:{start}
DESCRIPTION:entry 1
DTEND:{half_an_hour_ago}
DTSTAMP:{an_hour_ago}
DTSTART:{an_hour_ago}
SEQUENCE:0
SUMMARY:{note}
UID:tiempo-{id}@{host}
SUMMARY:entry 1
UID:tiempo-1@{host}
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:entry 2
DTEND:{now}
DTSTAMP:{half_an_hour_ago}
DTSTART:{half_an_hour_ago}
SEQUENCE:0
SUMMARY:entry 2
UID:tiempo-2@{host}
END:VEVENT
END:VCALENDAR
", note="entry 1", end=now.format(ICAL_TIME_FORMAT).to_string(),
start=an_hour_ago.format(ICAL_TIME_FORMAT).to_string(), id=1, host=host)));
", an_hour_ago=an_hour_ago.format(ICAL_TIME_FORMAT), half_an_hour_ago=half_an_hour_ago.format(ICAL_TIME_FORMAT), now=now.format(ICAL_TIME_FORMAT),
host=host)));
}
}