Print dates in a nicer format
This commit is contained in:
parent
531a140c45
commit
3e70b2e2c9
5 changed files with 67 additions and 5 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use chrono::NaiveDate;
|
||||
use std::fmt;
|
||||
|
||||
use chrono::{Duration, NaiveDate};
|
||||
|
||||
use crate::files::commands::DoneDate;
|
||||
use crate::files::primitives::Time;
|
||||
|
|
@ -16,6 +18,25 @@ pub struct Dates {
|
|||
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 {
|
||||
pub fn new(root: NaiveDate, other: NaiveDate) -> Self {
|
||||
Self {
|
||||
|
|
@ -92,6 +113,14 @@ impl Dates {
|
|||
pub fn end_time(&self) -> Option<Time> {
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -4,9 +4,10 @@ use chrono::NaiveDate;
|
|||
|
||||
use crate::files::primitives::{Span, Time};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// A delta step resulted in an invalid date.
|
||||
#[error("delta step resulted in invalid date")]
|
||||
DeltaInvalidStep {
|
||||
span: Span,
|
||||
start: NaiveDate,
|
||||
|
|
@ -15,6 +16,7 @@ pub enum Error {
|
|||
prev_time: Option<Time>,
|
||||
},
|
||||
/// A time-based delta step was applied to a date without time.
|
||||
#[error("time-based delta step applied to date without time")]
|
||||
DeltaNoTime {
|
||||
span: Span,
|
||||
start: NaiveDate,
|
||||
|
|
@ -23,6 +25,7 @@ pub enum Error {
|
|||
/// 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
|
||||
/// in time (`to < from`).
|
||||
#[error("repeat delta did not move forwards")]
|
||||
RepeatDidNotMoveForwards {
|
||||
span: Span,
|
||||
from: NaiveDate,
|
||||
|
|
@ -30,12 +33,16 @@ pub enum Error {
|
|||
},
|
||||
/// A `MOVE a TO b` statement was executed, but there was no entry at the
|
||||
/// date `a`.
|
||||
#[error("tried to move nonexisting entry")]
|
||||
MoveWithoutSource { span: Span },
|
||||
/// A division by zero has occurred.
|
||||
#[error("tried to divide by zero")]
|
||||
DivByZero { span: Span, date: NaiveDate },
|
||||
/// A modulo operation by zero has occurred.
|
||||
#[error("tried to modulo by zero")]
|
||||
ModByZero { span: Span, date: NaiveDate },
|
||||
/// Easter calculation failed.
|
||||
#[error("easter calculation failed")]
|
||||
Easter {
|
||||
span: Span,
|
||||
date: NaiveDate,
|
||||
|
|
|
|||
|
|
@ -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<'_>> {
|
||||
let mut result = vec![];
|
||||
for (file_index, file) in self.files.iter().enumerate() {
|
||||
|
|
|
|||
|
|
@ -325,6 +325,22 @@ pub enum Command {
|
|||
Note(Note),
|
||||
}
|
||||
|
||||
impl Command {
|
||||
pub fn title(&self) -> &str {
|
||||
match self {
|
||||
Self::Task(task) => &task.title,
|
||||
Self::Note(note) => ¬e.title,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn desc(&self) -> &[String] {
|
||||
match self {
|
||||
Self::Task(task) => &task.desc,
|
||||
Self::Note(note) => ¬e.desc,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct File {
|
||||
pub contents: String,
|
||||
|
|
|
|||
12
src/main.rs
12
src/main.rs
|
|
@ -28,13 +28,19 @@ fn main() -> anyhow::Result<()> {
|
|||
|
||||
let range = DateRange::new(
|
||||
NaiveDate::from_ymd(2021, 1, 1),
|
||||
NaiveDate::from_ymd(2021, 12, 31),
|
||||
NaiveDate::from_ymd(2022, 12, 31),
|
||||
)
|
||||
.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.save()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue