Revamp layouting
This commit is contained in:
parent
6e6a696ca4
commit
ee2e5f4e97
2 changed files with 187 additions and 99 deletions
|
|
@ -20,8 +20,7 @@ pub fn run() -> anyhow::Result<()> {
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
|
|
||||||
let files = Files::load(&opt.file)?;
|
let files = Files::load(&opt.file)?;
|
||||||
let now = files.now();
|
let now = files.now().naive_local();
|
||||||
let today = now.date().naive_local();
|
|
||||||
|
|
||||||
let range = DateRange::new(
|
let range = DateRange::new(
|
||||||
NaiveDate::from_ymd(2021, 1, 1),
|
NaiveDate::from_ymd(2021, 1, 1),
|
||||||
|
|
@ -32,7 +31,7 @@ pub fn run() -> anyhow::Result<()> {
|
||||||
let entries = files.eval(EntryMode::Relevant, range)?;
|
let entries = files.eval(EntryMode::Relevant, range)?;
|
||||||
println!("{:#?}", entries);
|
println!("{:#?}", entries);
|
||||||
|
|
||||||
let mut layout = Layout::new(range, today);
|
let mut layout = Layout::new(range, now);
|
||||||
layout.layout(&files, &entries);
|
layout.layout(&files, &entries);
|
||||||
println!("{:#?}", layout);
|
println!("{:#?}", layout);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,147 +1,236 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use chrono::NaiveDate;
|
use chrono::{NaiveDate, NaiveDateTime};
|
||||||
|
|
||||||
use crate::eval::{DateRange, Dates, Entry, EntryKind};
|
use crate::eval::{DateRange, Dates, Entry, EntryKind};
|
||||||
|
use crate::files::commands::Command;
|
||||||
use crate::files::primitives::Time;
|
use crate::files::primitives::Time;
|
||||||
use crate::files::Files;
|
use crate::files::Files;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TimedLayout {
|
pub enum LayoutEntry {
|
||||||
pub ending: Vec<usize>,
|
End(usize),
|
||||||
pub at: Vec<usize>,
|
Now(Time),
|
||||||
pub starting: Vec<usize>,
|
TimedEnd(usize, Time),
|
||||||
}
|
TimedAt(usize, Time),
|
||||||
|
TimedStart(usize, Time),
|
||||||
impl TimedLayout {
|
ReminderSince(usize, i64),
|
||||||
pub fn new() -> Self {
|
At(usize),
|
||||||
Self {
|
ReminderWhile(usize, i64),
|
||||||
ending: vec![],
|
Undated(usize),
|
||||||
at: vec![],
|
Start(usize),
|
||||||
starting: vec![],
|
ReminderUntil(usize, i64),
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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)]
|
#[derive(Debug)]
|
||||||
pub struct Layout {
|
pub struct Layout {
|
||||||
pub range: DateRange,
|
pub range: DateRange,
|
||||||
pub today: NaiveDate,
|
pub today: NaiveDate,
|
||||||
pub days: HashMap<NaiveDate, DayLayout>,
|
pub time: Time,
|
||||||
|
pub earlier: Vec<LayoutEntry>,
|
||||||
|
pub days: HashMap<NaiveDate, Vec<LayoutEntry>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Layout {
|
impl Layout {
|
||||||
pub fn new(range: DateRange, today: NaiveDate) -> Self {
|
pub fn new(range: DateRange, now: NaiveDateTime) -> Self {
|
||||||
let mut days = HashMap::new();
|
Self {
|
||||||
for day in range.days() {
|
range,
|
||||||
days.insert(day, DayLayout::new());
|
today: now.date(),
|
||||||
|
time: now.time().into(),
|
||||||
|
earlier: vec![],
|
||||||
|
days: range.days().map(|d| (d, vec![])).collect(),
|
||||||
}
|
}
|
||||||
Self { range, today, days }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn layout(&mut self, files: &Files, entries: &[Entry]) {
|
pub fn layout(&mut self, files: &Files, entries: &[Entry]) {
|
||||||
|
self.insert(self.today, LayoutEntry::Now(self.time));
|
||||||
|
|
||||||
let mut commands = entries
|
let mut commands = entries
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, e)| (i, e, files.command(e.source)))
|
.map(|(i, e)| (i, e, files.command(e.source)))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// Sort entries (maintaining the keys) so the output is more deterministic
|
Self::sort_entries(&mut commands);
|
||||||
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 {
|
for (index, entry, _) in commands {
|
||||||
self.insert(index, entry);
|
self.layout_entry(index, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (_, day) in self.days.iter_mut() {
|
||||||
|
Self::sort_day(day);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert(&mut self, index: usize, entry: &Entry) {
|
fn layout_entry(&mut self, index: usize, entry: &Entry) {
|
||||||
|
match entry.kind {
|
||||||
|
EntryKind::Task => self.layout_task(index, entry),
|
||||||
|
EntryKind::TaskDone(at) => self.layout_task_done(index, entry, at),
|
||||||
|
EntryKind::Note | EntryKind::Birthday(_) => self.layout_note(index, entry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_task(&mut self, index: usize, entry: &Entry) {
|
||||||
if let Some(dates) = entry.dates {
|
if let Some(dates) = entry.dates {
|
||||||
self.insert_dated(index, dates);
|
let (start, end) = dates.start_end();
|
||||||
if let EntryKind::TaskDone(at) = entry.kind {
|
if (start - self.today).num_days() < 7 {
|
||||||
self.insert_other(at, index);
|
// TODO Make this adjustable, maybe even per-command
|
||||||
|
let days = (start - self.today).num_days();
|
||||||
|
self.insert(self.today, LayoutEntry::ReminderUntil(index, days));
|
||||||
|
} else if start < self.today && self.today < end {
|
||||||
|
let days = (end - self.today).num_days();
|
||||||
|
self.insert(self.today, LayoutEntry::ReminderWhile(index, days));
|
||||||
|
} else if end < self.today {
|
||||||
|
let days = (self.today - end).num_days();
|
||||||
|
self.insert(self.today, LayoutEntry::ReminderSince(index, days));
|
||||||
}
|
}
|
||||||
|
self.layout_dated_entry(index, dates);
|
||||||
} else {
|
} else {
|
||||||
self.insert_other(self.today, index);
|
self.insert(self.today, LayoutEntry::Undated(index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_dated(&mut self, index: usize, dates: Dates) {
|
fn layout_task_done(&mut self, index: usize, entry: &Entry, at: NaiveDate) {
|
||||||
|
if let Some(dates) = entry.dates {
|
||||||
|
if at > dates.end() {
|
||||||
|
let days = (at - dates.end()).num_days();
|
||||||
|
self.insert(at, LayoutEntry::ReminderSince(index, days));
|
||||||
|
}
|
||||||
|
self.layout_dated_entry(index, dates);
|
||||||
|
} else {
|
||||||
|
// Treat the task as if its date was its completion time
|
||||||
|
self.layout_dated_entry(index, Dates::new(at, at));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_note(&mut self, index: usize, entry: &Entry) {
|
||||||
|
if let Some(dates) = entry.dates {
|
||||||
let (start, end) = dates.start_end();
|
let (start, end) = dates.start_end();
|
||||||
if start < self.range.from() && self.range.until() < end {
|
if start < self.range.from() && self.range.until() < end {
|
||||||
self.insert_other(self.today, index);
|
// This note applies to the current day, but it won't appear if
|
||||||
} else if let Some((date, time)) = dates.point_in_time() {
|
// we just layout it as a dated entry, so instead we add it as a
|
||||||
self.insert_at(date, time, index);
|
// reminder. Since we are usually more interested in when
|
||||||
|
// something ends than when it starts, we count the days until
|
||||||
|
// the end.
|
||||||
|
let days = (end - self.today).num_days();
|
||||||
|
self.insert(self.today, LayoutEntry::ReminderWhile(index, days));
|
||||||
} else {
|
} else {
|
||||||
let (start_time, end_time) = match dates.start_end_time() {
|
self.layout_dated_entry(index, dates);
|
||||||
Some((s, e)) => (Some(s), Some(e)),
|
}
|
||||||
None => (None, None),
|
} else {
|
||||||
|
self.insert(self.today, LayoutEntry::Undated(index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_dated_entry(&mut self, index: usize, dates: Dates) {
|
||||||
|
let (start, end) = dates.start_end();
|
||||||
|
if let Some((date, time)) = dates.point_in_time() {
|
||||||
|
let entry = match time {
|
||||||
|
Some(time) => LayoutEntry::TimedAt(index, time),
|
||||||
|
None => LayoutEntry::At(index),
|
||||||
};
|
};
|
||||||
self.insert_start(start, start_time, index);
|
self.insert(date, entry);
|
||||||
self.insert_end(end, end_time, index);
|
} else if start < self.range.from() && self.range.until() < end {
|
||||||
}
|
// Neither the start nor end layout entries would be visible
|
||||||
}
|
// directly. However, the start layout entry would be added to
|
||||||
|
// [`self.earlier`]. Since [`self.earlier`] only exists so that
|
||||||
fn insert_f(&mut self, date: NaiveDate, f: impl FnOnce(&mut DayLayout)) {
|
// every end entry has a corresponding start entry (for rendering),
|
||||||
if let Some(l) = self.days.get_mut(&date) {
|
// this would be pointless, so we don't add any entries.
|
||||||
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 {
|
} else {
|
||||||
self.insert_f(date, |dl| dl.starting.push(index));
|
let (start_entry, end_entry) = match dates.start_end_time() {
|
||||||
|
Some((start_time, end_time)) => (
|
||||||
|
LayoutEntry::TimedStart(index, start_time),
|
||||||
|
LayoutEntry::TimedEnd(index, end_time),
|
||||||
|
),
|
||||||
|
None => (LayoutEntry::Start(index), LayoutEntry::End(index)),
|
||||||
|
};
|
||||||
|
self.insert(start, start_entry);
|
||||||
|
self.insert(end, end_entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_at(&mut self, date: NaiveDate, time: Option<Time>, index: usize) {
|
fn insert(&mut self, date: NaiveDate, e: LayoutEntry) {
|
||||||
if let Some(time) = time {
|
if date < self.range.from() {
|
||||||
self.insert_timed_f(date, time, |tl| tl.at.push(index));
|
self.earlier.push(e);
|
||||||
} else {
|
} else if let Some(es) = self.days.get_mut(&date) {
|
||||||
self.insert_f(date, |dl| dl.at.push(index));
|
es.push(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_end(&mut self, date: NaiveDate, time: Option<Time>, index: usize) {
|
fn sort_entries(entries: &mut Vec<(usize, &Entry, &Command)>) {
|
||||||
if let Some(time) = time {
|
// Entries should be sorted by these factors, in descending order of
|
||||||
self.insert_timed_f(date, time, |tl| tl.ending.push(index));
|
// significance:
|
||||||
} else {
|
// 1. Their start date, if any
|
||||||
self.insert_f(date, |dl| dl.ending.push(index));
|
// 2. Their end date, if any
|
||||||
}
|
// 3. Their kind
|
||||||
|
// 4. Their title
|
||||||
|
|
||||||
|
// 4.
|
||||||
|
entries.sort_by_key(|(_, _, c)| c.title());
|
||||||
|
|
||||||
|
// 3.
|
||||||
|
entries.sort_by_key(|(_, e, _)| match e.kind {
|
||||||
|
EntryKind::Task => 0,
|
||||||
|
EntryKind::TaskDone(_) => 1,
|
||||||
|
EntryKind::Birthday(_) => 2,
|
||||||
|
EntryKind::Note => 3,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2.
|
||||||
|
entries.sort_by_key(|(_, e, _)| e.dates.map(|d| (d.end(), d.end_time())));
|
||||||
|
|
||||||
|
// 1.
|
||||||
|
entries.sort_by_key(|(_, e, _)| e.dates.map(|d| (d.start(), d.start_time())));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_other(&mut self, date: NaiveDate, index: usize) {
|
fn sort_day(day: &mut Vec<LayoutEntry>) {
|
||||||
self.insert_f(date, |dl| dl.other.push(index));
|
// In a day, entries should be sorted into these categories:
|
||||||
|
// 1. Untimed entries that end at the current day
|
||||||
|
// 2. Timed entries, based on
|
||||||
|
// 2.1. Their time
|
||||||
|
// 2.2. Their type (ending, at, starting)
|
||||||
|
// 3. Reminders for overdue entries
|
||||||
|
// 4. Untimed entries occurring today
|
||||||
|
// 5. Reminders for entries ending soon
|
||||||
|
// 6. Undated entries occurring today
|
||||||
|
// 7. Untimed entries starting today
|
||||||
|
// 8. Reminders for entries starting soon
|
||||||
|
//
|
||||||
|
// Entries within a single category should already be ordered based on
|
||||||
|
// their kind and title since the order they are layouted in takes these
|
||||||
|
// into account.
|
||||||
|
|
||||||
|
// Ensure timed entries for a single time occur in the correct order
|
||||||
|
day.sort_by_key(|e| match e {
|
||||||
|
LayoutEntry::Now(_) => 1,
|
||||||
|
LayoutEntry::TimedEnd(_, _) => 2,
|
||||||
|
LayoutEntry::TimedAt(_, _) => 3,
|
||||||
|
LayoutEntry::TimedStart(_, _) => 4,
|
||||||
|
_ => 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ensure timed entries for different times occur in the correct order
|
||||||
|
day.sort_by_key(|e| match e {
|
||||||
|
LayoutEntry::Now(t) => Some(*t),
|
||||||
|
LayoutEntry::TimedEnd(_, t) => Some(*t),
|
||||||
|
LayoutEntry::TimedAt(_, t) => Some(*t),
|
||||||
|
LayoutEntry::TimedStart(_, t) => Some(*t),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ensure categories occur in the correct order
|
||||||
|
day.sort_by_key(|e| match e {
|
||||||
|
LayoutEntry::End(_) => 0,
|
||||||
|
LayoutEntry::Now(_) => 1,
|
||||||
|
LayoutEntry::TimedEnd(_, _) => 1,
|
||||||
|
LayoutEntry::TimedAt(_, _) => 1,
|
||||||
|
LayoutEntry::TimedStart(_, _) => 1,
|
||||||
|
LayoutEntry::ReminderSince(_, _) => 2,
|
||||||
|
LayoutEntry::At(_) => 3,
|
||||||
|
LayoutEntry::ReminderWhile(_, _) => 4,
|
||||||
|
LayoutEntry::Undated(_) => 5,
|
||||||
|
LayoutEntry::Start(_) => 6,
|
||||||
|
LayoutEntry::ReminderUntil(_, _) => 7,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue