Print markers for logs and entry descriptions

This commit is contained in:
Joscha 2022-01-06 19:15:38 +01:00
parent b3d81a8d0a
commit 4a46e70a73
5 changed files with 78 additions and 25 deletions

View file

@ -86,8 +86,13 @@ fn find_entries(files: &Files, range: DateRange) -> Result<Vec<Entry>, Error<Fil
Ok(files.eval(EntryMode::Relevant, range)?)
}
fn find_layout(entries: &[Entry], range: DateRange, now: NaiveDateTime) -> LineLayout {
layout::layout(entries, range, now)
fn find_layout(
files: &Files,
entries: &[Entry],
range: DateRange,
now: NaiveDateTime,
) -> LineLayout {
layout::layout(files, entries, range, now)
}
fn run_command(
@ -99,28 +104,28 @@ fn run_command(
match &opt.command {
None => {
let entries = find_entries(files, range)?;
let layout = find_layout(&entries, range, now);
let layout = find_layout(files, &entries, range, now);
print::print(&layout);
}
Some(Command::Show { entries: ns }) => {
let entries = find_entries(files, range)?;
let layout = find_layout(&entries, range, now);
let layout = find_layout(files, &entries, range, now);
show::show(files, &entries, &layout, ns)?;
}
Some(Command::Done { entries: ns }) => {
let entries = find_entries(files, range)?;
let layout = find_layout(&entries, range, now);
let layout = find_layout(files, &entries, range, now);
done::done(files, &entries, &layout, ns, now)?;
let entries = find_entries(files, range)?;
let layout = find_layout(&entries, range, now);
let layout = find_layout(files, &entries, range, now);
print::print(&layout);
}
Some(Command::Cancel { entries: ns }) => {
let entries = find_entries(files, range)?;
let layout = find_layout(&entries, range, now);
let layout = find_layout(files, &entries, range, now);
cancel::cancel(files, &entries, &layout, ns, now)?;
let entries = find_entries(files, range)?;
let layout = find_layout(&entries, range, now);
let layout = find_layout(files, &entries, range, now);
print::print(&layout);
}
Some(Command::Fmt) => files.mark_all_dirty(),

View file

@ -1,6 +1,7 @@
use chrono::NaiveDateTime;
use crate::eval::{DateRange, Entry};
use crate::files::Files;
use self::day::DayLayout;
use self::line::LineLayout;
@ -8,12 +9,17 @@ use self::line::LineLayout;
mod day;
pub mod line;
pub fn layout(entries: &[Entry], range: DateRange, now: NaiveDateTime) -> LineLayout {
pub fn layout(
files: &Files,
entries: &[Entry],
range: DateRange,
now: NaiveDateTime,
) -> LineLayout {
let mut day_layout = DayLayout::new(range, now);
day_layout.layout(entries);
let mut line_layout = LineLayout::new();
line_layout.render(entries, &day_layout);
line_layout.render(files, entries, &day_layout);
line_layout
}

View file

@ -9,6 +9,7 @@ use chrono::NaiveDate;
use crate::eval::{Entry, EntryKind};
use crate::files::primitives::Time;
use crate::files::Files;
use super::super::error::Error;
use super::day::{DayEntry, DayLayout};
@ -69,6 +70,7 @@ pub enum LineEntry {
spans: Vec<Option<SpanSegment>>,
date: NaiveDate,
today: bool,
has_log: bool,
},
Now {
spans: Vec<Option<SpanSegment>>,
@ -80,6 +82,7 @@ pub enum LineEntry {
time: Times,
kind: LineKind,
text: String,
has_desc: bool,
extra: Option<String>,
},
}
@ -107,7 +110,7 @@ impl LineLayout {
}
}
pub fn render(&mut self, entries: &[Entry], layout: &DayLayout) {
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 {
@ -123,6 +126,7 @@ impl LineLayout {
spans,
date: day,
today: day == layout.today,
has_log: files.log(day).is_some(),
});
let layout_entries = layout.days.get(&day).expect("got nonexisting day");
@ -291,6 +295,7 @@ impl LineLayout {
time,
kind: Self::entry_kind(entry),
text: Self::entry_title(entry),
has_desc: entry.has_description,
extra,
});
}

View file

@ -24,7 +24,12 @@ impl ShowLines {
fn display_line(&mut self, line: &LineEntry) {
match line {
LineEntry::Day { spans, date, today } => self.display_line_date(spans, *date, *today),
LineEntry::Day {
spans,
date,
today,
has_log,
} => self.display_line_date(spans, *date, *today, *has_log),
LineEntry::Now { spans, time } => self.display_line_now(spans, *time),
LineEntry::Entry {
number,
@ -32,12 +37,19 @@ impl ShowLines {
time,
kind,
text,
has_desc,
extra,
} => self.display_line_entry(*number, spans, *time, *kind, text, extra),
} => self.display_line_entry(*number, spans, *time, *kind, text, *has_desc, extra),
}
}
fn display_line_date(&mut self, spans: &[Option<SpanSegment>], date: NaiveDate, today: bool) {
fn display_line_date(
&mut self,
spans: &[Option<SpanSegment>],
date: NaiveDate,
today: bool,
has_log: bool,
) {
let weekday: Weekday = date.weekday().into();
let weekday = weekday.full_name();
@ -55,16 +67,23 @@ impl ShowLines {
// Spans and filler '=' symbols
let p2 = self.display_spans(spans, styled("="));
// The rest of the line
let p3 = format!(
"=== {:9} {} ===={:=<w$}",
weekday,
date,
"",
w = self.num_width + self.span_width
);
// The rest of the line until after the date
let p3 = format!("=== {:9} {}", weekday, date,);
self.push(&format!("{}{}{}\n", styled(&p1), p2, styled(&p3)));
// The "has log" marker (if any)
let p4 = Self::display_marker(has_log, " ");
// The rest of the line
let p5 = format!(" ===={:=<w$}", "", w = self.num_width + self.span_width);
self.push(&format!(
"{}{}{}{}{}\n",
styled(&p1),
p2,
styled(&p3),
p4,
styled(&p5)
));
}
fn display_line_now(&mut self, spans: &[Option<SpanSegment>], time: Time) {
@ -84,6 +103,7 @@ impl ShowLines {
time: Times,
kind: LineKind,
text: &str,
has_desc: bool,
extra: &Option<String>,
) {
let num = match number {
@ -92,12 +112,13 @@ impl ShowLines {
};
self.push(&format!(
"{:>nw$} {} {}{} {}{}\n",
"{:>nw$} {} {}{} {}{}{}\n",
num.bright_black(),
self.display_spans(spans, " ".into()),
Self::display_kind(kind),
Self::display_time(time),
text,
Self::display_marker(has_desc, ""),
Self::display_extra(extra),
nw = self.num_width,
))
@ -140,6 +161,14 @@ impl ShowLines {
}
}
fn display_marker(marker: bool, otherwise: &str) -> ColoredString {
if marker {
"*".bright_yellow()
} else {
otherwise.into()
}
}
fn display_extra(extra: &Option<String>) -> ColoredString {
match extra {
None => "".into(),

View file

@ -7,7 +7,7 @@ use chrono::{DateTime, NaiveDate, Utc};
use codespan_reporting::files::SimpleFiles;
use tzfile::Tz;
use self::commands::{Command, Done, File};
use self::commands::{Command, Done, File, Log};
pub use self::error::{Error, Result};
use self::primitives::Spanned;
@ -311,6 +311,14 @@ impl Files {
Self::command_of_files(&self.files, source)
}
pub fn log(&self, date: NaiveDate) -> Option<&Log> {
let source = *self.logs.get(&date)?;
match self.command(source).command {
Command::Log(log) => Some(log),
_ => unreachable!(),
}
}
pub fn now(&self) -> DateTime<&Tz> {
if let Some(tz) = &self.timezone {
Utc::now().with_timezone(&tz)