Reorganize layout and rendering code
This commit is contained in:
parent
e2aa7c1a29
commit
8239a535f6
5 changed files with 447 additions and 387 deletions
243
src/cli/layout/day.rs
Normal file
243
src/cli/layout/day.rs
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
//! Organize a list of entries into a specified range of days.
|
||||
//!
|
||||
//! This includes adding reminders and ordering everything so it will be nicer
|
||||
//! to display later.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
use crate::eval::{DateRange, Dates, Entry, EntryKind};
|
||||
use crate::files::commands::Command;
|
||||
use crate::files::primitives::Time;
|
||||
use crate::files::Files;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DayEntry {
|
||||
End(usize),
|
||||
Now(Time),
|
||||
TimedEnd(usize, Time),
|
||||
TimedAt(usize, Time),
|
||||
TimedStart(usize, Time),
|
||||
ReminderSince(usize, i64),
|
||||
At(usize),
|
||||
ReminderWhile(usize, i64),
|
||||
Undated(usize),
|
||||
Start(usize),
|
||||
ReminderUntil(usize, i64),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DayLayout {
|
||||
pub range: DateRange,
|
||||
pub today: NaiveDate,
|
||||
pub time: Time,
|
||||
pub earlier: Vec<DayEntry>,
|
||||
pub days: HashMap<NaiveDate, Vec<DayEntry>>,
|
||||
}
|
||||
|
||||
impl DayLayout {
|
||||
pub fn new(range: DateRange, now: NaiveDateTime) -> Self {
|
||||
Self {
|
||||
range,
|
||||
today: now.date(),
|
||||
time: now.time().into(),
|
||||
earlier: vec![],
|
||||
days: range.days().map(|d| (d, vec![])).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layout(&mut self, files: &Files, entries: &[Entry]) {
|
||||
self.insert(self.today, DayEntry::Now(self.time));
|
||||
|
||||
let mut commands = entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, e)| (i, e, files.command(e.source)))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Self::sort_entries(&mut commands);
|
||||
|
||||
for (index, entry, _) in commands {
|
||||
self.layout_entry(index, entry);
|
||||
}
|
||||
|
||||
for (_, day) in self.days.iter_mut() {
|
||||
Self::sort_day(day);
|
||||
}
|
||||
|
||||
// TODO Combine TimedStart and TimedEnd if there is nothing in-between
|
||||
}
|
||||
|
||||
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 {
|
||||
let (start, end) = dates.start_end();
|
||||
if self.today < start && (start - self.today).num_days() < 7 {
|
||||
// TODO Make this adjustable, maybe even per-command
|
||||
let days = (start - self.today).num_days();
|
||||
self.insert(self.today, DayEntry::ReminderUntil(index, days));
|
||||
} else if start < self.today && self.today < end {
|
||||
let days = (end - self.today).num_days();
|
||||
self.insert(self.today, DayEntry::ReminderWhile(index, days));
|
||||
} else if end < self.today {
|
||||
let days = (self.today - end).num_days();
|
||||
self.insert(self.today, DayEntry::ReminderSince(index, days));
|
||||
}
|
||||
self.layout_dated_entry(index, dates);
|
||||
} else {
|
||||
self.insert(self.today, DayEntry::Undated(index));
|
||||
}
|
||||
}
|
||||
|
||||
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, DayEntry::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();
|
||||
if start < self.range.from() && self.range.until() < end {
|
||||
// This note applies to the current day, but it won't appear if
|
||||
// we just layout it as a dated entry, so instead we add it as a
|
||||
// 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, DayEntry::ReminderWhile(index, days));
|
||||
} else {
|
||||
self.layout_dated_entry(index, dates);
|
||||
}
|
||||
} else {
|
||||
self.insert(self.today, DayEntry::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) => DayEntry::TimedAt(index, time),
|
||||
None => DayEntry::At(index),
|
||||
};
|
||||
self.insert(date, entry);
|
||||
} 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
|
||||
// every end entry has a corresponding start entry (for rendering),
|
||||
// this would be pointless, so we don't add any entries.
|
||||
} else {
|
||||
let (start_entry, end_entry) = match dates.start_end_time() {
|
||||
Some((start_time, end_time)) => (
|
||||
DayEntry::TimedStart(index, start_time),
|
||||
DayEntry::TimedEnd(index, end_time),
|
||||
),
|
||||
None => (DayEntry::Start(index), DayEntry::End(index)),
|
||||
};
|
||||
self.insert(start, start_entry);
|
||||
self.insert(end, end_entry);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, date: NaiveDate, e: DayEntry) {
|
||||
if date < self.range.from() {
|
||||
self.earlier.push(e);
|
||||
} else if let Some(es) = self.days.get_mut(&date) {
|
||||
es.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
fn sort_entries(entries: &mut Vec<(usize, &Entry, &Command)>) {
|
||||
// Entries should be sorted by these factors, in descending order of
|
||||
// significance:
|
||||
// 1. Their start date, if any
|
||||
// 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 sort_day(day: &mut Vec<DayEntry>) {
|
||||
// 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 {
|
||||
DayEntry::Now(_) => 1,
|
||||
DayEntry::TimedEnd(_, _) => 2,
|
||||
DayEntry::TimedAt(_, _) => 3,
|
||||
DayEntry::TimedStart(_, _) => 4,
|
||||
_ => 0,
|
||||
});
|
||||
|
||||
// Ensure timed entries for different times occur in the correct order
|
||||
day.sort_by_key(|e| match e {
|
||||
DayEntry::Now(t) => Some(*t),
|
||||
DayEntry::TimedEnd(_, t) => Some(*t),
|
||||
DayEntry::TimedAt(_, t) => Some(*t),
|
||||
DayEntry::TimedStart(_, t) => Some(*t),
|
||||
_ => None,
|
||||
});
|
||||
|
||||
// Ensure categories occur in the correct order
|
||||
day.sort_by_key(|e| match e {
|
||||
DayEntry::End(_) => 0,
|
||||
DayEntry::Now(_) => 1,
|
||||
DayEntry::TimedEnd(_, _) => 1,
|
||||
DayEntry::TimedAt(_, _) => 1,
|
||||
DayEntry::TimedStart(_, _) => 1,
|
||||
DayEntry::ReminderSince(_, _) => 2,
|
||||
DayEntry::At(_) => 3,
|
||||
DayEntry::ReminderWhile(_, _) => 4,
|
||||
DayEntry::Undated(_) => 5,
|
||||
DayEntry::Start(_) => 6,
|
||||
DayEntry::ReminderUntil(_, _) => 7,
|
||||
})
|
||||
}
|
||||
}
|
||||
245
src/cli/layout/line.rs
Normal file
245
src/cli/layout/line.rs
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
//! Organize layouted entries into a list of lines to display.
|
||||
//!
|
||||
//! Additional information, such as the mapping from numbers to entries, are
|
||||
//! collected along the way. The lines are not yet rendered into strings.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::eval::{Entry, EntryKind};
|
||||
use crate::files::primitives::Time;
|
||||
use crate::files::Files;
|
||||
|
||||
use super::day::{DayEntry, DayLayout};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum SpanSegment {
|
||||
Start,
|
||||
Middle,
|
||||
End,
|
||||
}
|
||||
|
||||
pub enum LineEntry {
|
||||
Day {
|
||||
spans: Vec<Option<SpanSegment>>,
|
||||
date: NaiveDate,
|
||||
},
|
||||
Now {
|
||||
spans: Vec<Option<SpanSegment>>,
|
||||
time: Time,
|
||||
},
|
||||
Entry {
|
||||
number: Option<usize>,
|
||||
spans: Vec<Option<SpanSegment>>,
|
||||
time: Option<Time>,
|
||||
text: String,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct LineLayout {
|
||||
/// Map from entry indices to their corresponding display numbers.
|
||||
///
|
||||
/// Display numbers start at 1, not 0.
|
||||
numbers: HashMap<usize, usize>,
|
||||
/// The last number that was used as a display number.
|
||||
///
|
||||
/// Is set to 0 initially, which is fine since display numbers start at 1.
|
||||
last_number: usize,
|
||||
spans: Vec<Option<(usize, SpanSegment)>>,
|
||||
lines: Vec<LineEntry>,
|
||||
}
|
||||
|
||||
impl LineLayout {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
numbers: HashMap::new(),
|
||||
last_number: 0,
|
||||
spans: vec![],
|
||||
lines: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self, files: &Files, entries: &[Entry], layout: &DayLayout) {
|
||||
// Make sure spans for visible `*End`s are drawn
|
||||
for entry in &layout.earlier {
|
||||
match entry {
|
||||
DayEntry::TimedStart(i, _) | DayEntry::Start(i) => self.start_span(*i),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.step_spans();
|
||||
|
||||
for day in layout.range.days() {
|
||||
let spans = self.spans_for_line();
|
||||
self.line(LineEntry::Day { spans, date: day });
|
||||
|
||||
let layout_entries = layout.days.get(&day).expect("got nonexisting day");
|
||||
for layout_entry in layout_entries {
|
||||
self.render_layout_entry(files, entries, layout_entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn num_width(&self) -> usize {
|
||||
format!("{}", self.last_number).len()
|
||||
}
|
||||
|
||||
pub fn span_width(&self) -> usize {
|
||||
self.spans.len()
|
||||
}
|
||||
|
||||
pub fn lines(&self) -> &[LineEntry] {
|
||||
&self.lines
|
||||
}
|
||||
|
||||
/// Return a map from entry indices to their corresponding display numbers.
|
||||
///
|
||||
/// If you need to resolve a display number into an entry index, use
|
||||
/// [`look_up_number`] instead.
|
||||
pub fn numbers(&self) -> &HashMap<usize, usize> {
|
||||
&self.numbers
|
||||
}
|
||||
|
||||
pub fn look_up_number(&self, number: usize) -> Option<usize> {
|
||||
self.numbers
|
||||
.iter()
|
||||
.filter(|(_, n)| **n == number)
|
||||
.map(|(i, _)| *i)
|
||||
.next()
|
||||
}
|
||||
|
||||
fn render_layout_entry(&mut self, files: &Files, entries: &[Entry], l_entry: &DayEntry) {
|
||||
match l_entry {
|
||||
DayEntry::End(i) => {
|
||||
self.stop_span(*i);
|
||||
self.line_entry(Some(*i), None, Self::format_entry(files, entries, *i));
|
||||
}
|
||||
DayEntry::Now(t) => self.line(LineEntry::Now {
|
||||
spans: self.spans_for_line(),
|
||||
time: *t,
|
||||
}),
|
||||
DayEntry::TimedEnd(i, t) => {
|
||||
self.stop_span(*i);
|
||||
self.line_entry(Some(*i), Some(*t), Self::format_entry(files, entries, *i));
|
||||
}
|
||||
DayEntry::TimedAt(i, t) => {
|
||||
self.line_entry(Some(*i), Some(*t), Self::format_entry(files, entries, *i));
|
||||
}
|
||||
DayEntry::TimedStart(i, t) => {
|
||||
self.start_span(*i);
|
||||
self.line_entry(Some(*i), Some(*t), Self::format_entry(files, entries, *i));
|
||||
}
|
||||
DayEntry::ReminderSince(i, d) => {
|
||||
let text = Self::format_entry(files, entries, *i);
|
||||
let text = if *d == 1 {
|
||||
format!("{} (yesterday)", text)
|
||||
} else {
|
||||
format!("{} ({} days ago)", text, d)
|
||||
};
|
||||
self.line_entry(Some(*i), None, text);
|
||||
}
|
||||
DayEntry::At(i) => {
|
||||
self.line_entry(Some(*i), None, Self::format_entry(files, entries, *i))
|
||||
}
|
||||
DayEntry::ReminderWhile(i, d) => {
|
||||
let text = Self::format_entry(files, entries, *i);
|
||||
let plural = if *d == 1 { "" } else { "s" };
|
||||
let text = format!("{} ({} day{} left)", text, i, plural);
|
||||
self.line_entry(Some(*i), None, text);
|
||||
}
|
||||
DayEntry::Undated(i) => {
|
||||
self.line_entry(Some(*i), None, Self::format_entry(files, entries, *i));
|
||||
}
|
||||
DayEntry::Start(i) => {
|
||||
self.start_span(*i);
|
||||
self.line_entry(Some(*i), None, Self::format_entry(files, entries, *i));
|
||||
}
|
||||
DayEntry::ReminderUntil(i, d) => {
|
||||
let text = Self::format_entry(files, entries, *i);
|
||||
let text = if *d == 1 {
|
||||
format!("{} (tomorrow)", text)
|
||||
} else {
|
||||
format!("{} (in {} days)", text, d)
|
||||
};
|
||||
self.line_entry(Some(*i), None, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_entry(files: &Files, entries: &[Entry], index: usize) -> String {
|
||||
let entry = entries[index];
|
||||
let command = files.command(entry.source);
|
||||
match entry.kind {
|
||||
EntryKind::Task => format!("T {}", command.title()),
|
||||
EntryKind::TaskDone(_) => format!("D {}", command.title()),
|
||||
EntryKind::Note => format!("N {}", command.title()),
|
||||
EntryKind::Birthday(Some(age)) => format!("B {} ({})", command.title(), age),
|
||||
EntryKind::Birthday(None) => format!("B {}", command.title()),
|
||||
}
|
||||
}
|
||||
|
||||
fn start_span(&mut self, index: usize) {
|
||||
for span in self.spans.iter_mut() {
|
||||
if span.is_none() {
|
||||
*span = Some((index, SpanSegment::Start));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Not enough space, we need another column
|
||||
self.spans.push(Some((index, SpanSegment::Start)));
|
||||
}
|
||||
|
||||
fn stop_span(&mut self, index: usize) {
|
||||
for span in self.spans.iter_mut() {
|
||||
match span {
|
||||
Some((i, s)) if *i == index => *s = SpanSegment::End,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn step_spans(&mut self) {
|
||||
for span in self.spans.iter_mut() {
|
||||
match span {
|
||||
Some((_, s @ SpanSegment::Start)) => *s = SpanSegment::Middle,
|
||||
Some((_, SpanSegment::End)) => *span = None,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn spans_for_line(&self) -> Vec<Option<SpanSegment>> {
|
||||
self.spans
|
||||
.iter()
|
||||
.map(|span| span.as_ref().map(|(_, s)| *s))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn line(&mut self, line: LineEntry) {
|
||||
self.lines.push(line);
|
||||
self.step_spans();
|
||||
}
|
||||
|
||||
fn line_entry(&mut self, index: Option<usize>, time: Option<Time>, text: String) {
|
||||
let number = match index {
|
||||
Some(index) => Some(match self.numbers.get(&index) {
|
||||
Some(number) => *number,
|
||||
None => {
|
||||
self.last_number += 1;
|
||||
self.numbers.insert(index, self.last_number);
|
||||
self.last_number
|
||||
}
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
|
||||
self.line(LineEntry::Entry {
|
||||
number,
|
||||
spans: self.spans_for_line(),
|
||||
time,
|
||||
text,
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue