Print dates in a nicer format

This commit is contained in:
Joscha 2021-12-06 13:54:58 +00:00
parent 531a140c45
commit 3e70b2e2c9
5 changed files with 67 additions and 5 deletions

View file

@ -1,4 +1,6 @@
use chrono::NaiveDate; use std::fmt;
use chrono::{Duration, NaiveDate};
use crate::files::commands::DoneDate; use crate::files::commands::DoneDate;
use crate::files::primitives::Time; use crate::files::primitives::Time;
@ -16,6 +18,25 @@ pub struct Dates {
times: Option<Times>, times: Option<Times>,
} }
impl fmt::Display for Dates {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (start, end) = self.start_end();
match self.start_end_time() {
Some((start_time, end_time)) if start == end && start_time == end_time => {
write!(f, "{} {}", start, start_time)
}
Some((start_time, end_time)) if start == end => {
write!(f, "{} {} -- {}", start, start_time, end_time)
}
Some((start_time, end_time)) => {
write!(f, "{} {} -- {} {}", start, start_time, end, end_time)
}
None if start == end => write!(f, "{}", start),
None => write!(f, "{} -- {}", start, end),
}
}
}
impl Dates { impl Dates {
pub fn new(root: NaiveDate, other: NaiveDate) -> Self { pub fn new(root: NaiveDate, other: NaiveDate) -> Self {
Self { Self {
@ -92,6 +113,14 @@ impl Dates {
pub fn end_time(&self) -> Option<Time> { pub fn end_time(&self) -> Option<Time> {
self.start_end_time().map(|times| times.1) self.start_end_time().map(|times| times.1)
} }
pub fn move_by(&self, delta: Duration) -> Self {
Self {
root: self.root + delta,
other: self.other + delta,
times: self.times,
}
}
} }
impl From<DoneDate> for Dates { impl From<DoneDate> for Dates {

View file

@ -4,9 +4,10 @@ use chrono::NaiveDate;
use crate::files::primitives::{Span, Time}; use crate::files::primitives::{Span, Time};
#[derive(Debug)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
/// A delta step resulted in an invalid date. /// A delta step resulted in an invalid date.
#[error("delta step resulted in invalid date")]
DeltaInvalidStep { DeltaInvalidStep {
span: Span, span: Span,
start: NaiveDate, start: NaiveDate,
@ -15,6 +16,7 @@ pub enum Error {
prev_time: Option<Time>, prev_time: Option<Time>,
}, },
/// A time-based delta step was applied to a date without time. /// A time-based delta step was applied to a date without time.
#[error("time-based delta step applied to date without time")]
DeltaNoTime { DeltaNoTime {
span: Span, span: Span,
start: NaiveDate, start: NaiveDate,
@ -23,6 +25,7 @@ pub enum Error {
/// A `DATE`'s repeat delta did not move the date forwards in time. Instead, /// A `DATE`'s repeat delta did not move the date forwards in time. Instead,
/// it either remained at the current date (`to == from`) or moved backwards /// it either remained at the current date (`to == from`) or moved backwards
/// in time (`to < from`). /// in time (`to < from`).
#[error("repeat delta did not move forwards")]
RepeatDidNotMoveForwards { RepeatDidNotMoveForwards {
span: Span, span: Span,
from: NaiveDate, from: NaiveDate,
@ -30,12 +33,16 @@ pub enum Error {
}, },
/// A `MOVE a TO b` statement was executed, but there was no entry at the /// A `MOVE a TO b` statement was executed, but there was no entry at the
/// date `a`. /// date `a`.
#[error("tried to move nonexisting entry")]
MoveWithoutSource { span: Span }, MoveWithoutSource { span: Span },
/// A division by zero has occurred. /// A division by zero has occurred.
#[error("tried to divide by zero")]
DivByZero { span: Span, date: NaiveDate }, DivByZero { span: Span, date: NaiveDate },
/// A modulo operation by zero has occurred. /// A modulo operation by zero has occurred.
#[error("tried to modulo by zero")]
ModByZero { span: Span, date: NaiveDate }, ModByZero { span: Span, date: NaiveDate },
/// Easter calculation failed. /// Easter calculation failed.
#[error("easter calculation failed")]
Easter { Easter {
span: Span, span: Span,
date: NaiveDate, date: NaiveDate,

View file

@ -154,6 +154,10 @@ impl Files {
} }
} }
pub fn command(&self, source: Source) -> &Command {
&self.files[source.file].file.commands[source.command]
}
pub fn commands(&self) -> Vec<SourcedCommand<'_>> { pub fn commands(&self) -> Vec<SourcedCommand<'_>> {
let mut result = vec![]; let mut result = vec![];
for (file_index, file) in self.files.iter().enumerate() { for (file_index, file) in self.files.iter().enumerate() {

View file

@ -325,6 +325,22 @@ pub enum Command {
Note(Note), Note(Note),
} }
impl Command {
pub fn title(&self) -> &str {
match self {
Self::Task(task) => &task.title,
Self::Note(note) => &note.title,
}
}
pub fn desc(&self) -> &[String] {
match self {
Self::Task(task) => &task.desc,
Self::Note(note) => &note.desc,
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct File { pub struct File {
pub contents: String, pub contents: String,

View file

@ -28,13 +28,19 @@ fn main() -> anyhow::Result<()> {
let range = DateRange::new( let range = DateRange::new(
NaiveDate::from_ymd(2021, 1, 1), NaiveDate::from_ymd(2021, 1, 1),
NaiveDate::from_ymd(2021, 12, 31), NaiveDate::from_ymd(2022, 12, 31),
) )
.unwrap(); .unwrap();
println!("{:#?}", files.eval(EntryMode::Relevant, range)); for entry in files.eval(EntryMode::Relevant, range)? {
let title = files.command(entry.source).title();
if let Some(dates) = entry.dates {
println!("{:36} - {}", format!("{}", dates), title);
} else {
println!("{:36} - {}", "undated", title);
}
}
files.mark_all_dirty(); files.mark_all_dirty();
files.save()?; files.save()?;
Ok(()) Ok(())
} }