Layout entries
This commit is contained in:
parent
d6d225c098
commit
44bcd5712d
6 changed files with 204 additions and 50 deletions
41
src/cli.rs
Normal file
41
src/cli.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::eval::{DateRange, EntryMode};
|
||||
use crate::files::Files;
|
||||
|
||||
use self::layout::Layout;
|
||||
|
||||
mod layout;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct Opt {
|
||||
#[structopt(parse(from_os_str))]
|
||||
file: PathBuf,
|
||||
}
|
||||
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let files = Files::load(&opt.file)?;
|
||||
let now = files.now();
|
||||
let today = now.date().naive_local();
|
||||
|
||||
let range = DateRange::new(
|
||||
NaiveDate::from_ymd(2021, 1, 1),
|
||||
NaiveDate::from_ymd(2022, 12, 31),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let entries = files.eval(EntryMode::Relevant, range)?;
|
||||
println!("{:#?}", entries);
|
||||
|
||||
let mut layout = Layout::new(range, today);
|
||||
layout.layout(&files, &entries);
|
||||
println!("{:#?}", layout);
|
||||
|
||||
files.save()?;
|
||||
Ok(())
|
||||
}
|
||||
147
src/cli/layout.rs
Normal file
147
src/cli/layout.rs
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::eval::{DateRange, Dates, Entry, EntryKind};
|
||||
use crate::files::primitives::Time;
|
||||
use crate::files::Files;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TimedLayout {
|
||||
pub ending: Vec<usize>,
|
||||
pub at: Vec<usize>,
|
||||
pub starting: Vec<usize>,
|
||||
}
|
||||
|
||||
impl TimedLayout {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
ending: vec![],
|
||||
at: vec![],
|
||||
starting: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DayLayout {
|
||||
pub ending: Vec<usize>,
|
||||
pub timed: HashMap<Time, TimedLayout>,
|
||||
pub at: Vec<usize>,
|
||||
pub other: Vec<usize>,
|
||||
pub starting: Vec<usize>,
|
||||
}
|
||||
|
||||
impl DayLayout {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
ending: vec![],
|
||||
timed: HashMap::new(),
|
||||
at: vec![],
|
||||
other: vec![],
|
||||
starting: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Layout {
|
||||
pub range: DateRange,
|
||||
pub today: NaiveDate,
|
||||
pub days: HashMap<NaiveDate, DayLayout>,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
pub fn new(range: DateRange, today: NaiveDate) -> Self {
|
||||
let mut days = HashMap::new();
|
||||
for day in range.days() {
|
||||
days.insert(day, DayLayout::new());
|
||||
}
|
||||
Self { range, today, days }
|
||||
}
|
||||
|
||||
pub fn layout(&mut self, files: &Files, entries: &[Entry]) {
|
||||
let mut commands = entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, e)| (i, e, files.command(e.source)))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Sort entries (maintaining the keys) so the output is more deterministic
|
||||
commands.sort_by_key(|(_, _, c)| c.title());
|
||||
commands.sort_by_key(|(_, e, _)| e.dates.map(|d| (d.end(), d.end_time())));
|
||||
commands.sort_by_key(|(_, e, _)| e.dates.map(|d| (d.start(), d.start_time())));
|
||||
|
||||
for (index, entry, _) in commands {
|
||||
self.insert(index, entry);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, index: usize, entry: &Entry) {
|
||||
if let Some(dates) = entry.dates {
|
||||
self.insert_dated(index, dates);
|
||||
if let EntryKind::TaskDone(at) = entry.kind {
|
||||
self.insert_other(at, index);
|
||||
}
|
||||
} else {
|
||||
self.insert_other(self.today, index);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_dated(&mut self, index: usize, dates: Dates) {
|
||||
let (start, end) = dates.start_end();
|
||||
if start < self.range.from() && self.range.until() < end {
|
||||
self.insert_other(self.today, index);
|
||||
} else if let Some((date, time)) = dates.point_in_time() {
|
||||
self.insert_at(date, time, index);
|
||||
} else {
|
||||
let (start_time, end_time) = match dates.start_end_time() {
|
||||
Some((s, e)) => (Some(s), Some(e)),
|
||||
None => (None, None),
|
||||
};
|
||||
self.insert_start(start, start_time, index);
|
||||
self.insert_end(end, end_time, index);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_f(&mut self, date: NaiveDate, f: impl FnOnce(&mut DayLayout)) {
|
||||
if let Some(l) = self.days.get_mut(&date) {
|
||||
f(l);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_timed_f(&mut self, date: NaiveDate, time: Time, f: impl FnOnce(&mut TimedLayout)) {
|
||||
if let Some(l) = self.days.get_mut(&date) {
|
||||
let tl = l.timed.entry(time).or_insert_with(TimedLayout::new);
|
||||
f(tl);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_start(&mut self, date: NaiveDate, time: Option<Time>, index: usize) {
|
||||
if let Some(time) = time {
|
||||
self.insert_timed_f(date, time, |tl| tl.starting.push(index));
|
||||
} else {
|
||||
self.insert_f(date, |dl| dl.starting.push(index));
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_at(&mut self, date: NaiveDate, time: Option<Time>, index: usize) {
|
||||
if let Some(time) = time {
|
||||
self.insert_timed_f(date, time, |tl| tl.at.push(index));
|
||||
} else {
|
||||
self.insert_f(date, |dl| dl.at.push(index));
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_end(&mut self, date: NaiveDate, time: Option<Time>, index: usize) {
|
||||
if let Some(time) = time {
|
||||
self.insert_timed_f(date, time, |tl| tl.ending.push(index));
|
||||
} else {
|
||||
self.insert_f(date, |dl| dl.ending.push(index));
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_other(&mut self, date: NaiveDate, index: usize) {
|
||||
self.insert_f(date, |dl| dl.other.push(index));
|
||||
}
|
||||
}
|
||||
15
src/eval.rs
15
src/eval.rs
|
|
@ -1,7 +1,7 @@
|
|||
use crate::files::Files;
|
||||
|
||||
use self::command::CommandState;
|
||||
use self::date::Dates;
|
||||
pub use self::date::Dates;
|
||||
use self::entry::Entries;
|
||||
pub use self::entry::{Entry, EntryKind, EntryMode};
|
||||
pub use self::error::{Error, Result};
|
||||
|
|
@ -23,17 +23,6 @@ impl Files {
|
|||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
let mut entries = entries.entries();
|
||||
entries.sort_by_key(|e| {
|
||||
e.dates.map(|d| {
|
||||
let (start, end) = d.start_end();
|
||||
if let Some((start_time, end_time)) = d.start_end_time() {
|
||||
(start, Some(start_time), end, Some(end_time))
|
||||
} else {
|
||||
(start, None, end, None)
|
||||
}
|
||||
})
|
||||
});
|
||||
Ok(entries)
|
||||
Ok(entries.entries())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,6 +114,17 @@ impl Dates {
|
|||
self.start_end_time().map(|times| times.1)
|
||||
}
|
||||
|
||||
pub fn point_in_time(&self) -> Option<(NaiveDate, Option<Time>)> {
|
||||
if self.root != self.other {
|
||||
return None;
|
||||
}
|
||||
match self.times {
|
||||
Some(times) if times.root == times.other => Some((self.root, Some(times.root))),
|
||||
Some(_) => None,
|
||||
None => Some((self.root, None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_by(&self, delta: Duration) -> Self {
|
||||
Self {
|
||||
root: self.root + delta,
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ impl<T> Spanned<T> {
|
|||
// }
|
||||
// }
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Time {
|
||||
pub hour: u8,
|
||||
pub min: u8,
|
||||
|
|
|
|||
38
src/main.rs
38
src/main.rs
|
|
@ -3,44 +3,10 @@
|
|||
#![warn(clippy::all)]
|
||||
#![warn(clippy::use_self)]
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::eval::{DateRange, EntryMode};
|
||||
use crate::files::Files;
|
||||
|
||||
mod cli;
|
||||
mod eval;
|
||||
mod files;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct Opt {
|
||||
#[structopt(parse(from_os_str))]
|
||||
file: PathBuf,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let mut files = Files::load(&opt.file)?;
|
||||
println!("{}", files.now().format("%F %T %Z"));
|
||||
|
||||
let range = DateRange::new(
|
||||
NaiveDate::from_ymd(2021, 1, 1),
|
||||
NaiveDate::from_ymd(2022, 12, 31),
|
||||
)
|
||||
.unwrap();
|
||||
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(())
|
||||
cli::run()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue