From 26479ac58d91c608783368968c446b8d65ad4065 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 9 Jan 2022 16:04:34 +0100 Subject: [PATCH] Improve format of show command --- src/cli.rs | 1 + src/cli/layout/line.rs | 2 +- src/cli/print.rs | 13 ++------ src/cli/show.rs | 73 ++++++++++++++++++++++++++++-------------- src/cli/util.rs | 13 ++++++++ 5 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 src/cli/util.rs diff --git a/src/cli.rs b/src/cli.rs index 7a42c93..5b093e2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -21,6 +21,7 @@ mod layout; mod log; mod print; mod show; +mod util; #[derive(Debug, StructOpt)] pub struct Opt { diff --git a/src/cli/layout/line.rs b/src/cli/layout/line.rs index 1236f8e..0d1c360 100644 --- a/src/cli/layout/line.rs +++ b/src/cli/layout/line.rs @@ -215,7 +215,7 @@ impl LineLayout { } } - fn entry_kind(entry: &Entry) -> LineKind { + pub fn entry_kind(entry: &Entry) -> LineKind { match entry.kind { EntryKind::Task => LineKind::Task, EntryKind::TaskDone(_) => LineKind::Done, diff --git a/src/cli/print.rs b/src/cli/print.rs index 3243e37..1cdfe4d 100644 --- a/src/cli/print.rs +++ b/src/cli/print.rs @@ -6,6 +6,7 @@ use colored::{ColoredString, Colorize}; use crate::files::primitives::{Time, Weekday}; use super::layout::line::{LineEntry, LineKind, LineLayout, SpanSegment, SpanStyle, Times}; +use super::util; struct ShowLines { num_width: usize, @@ -116,7 +117,7 @@ impl ShowLines { "{:>nw$} {} {}{} {}{}{}\n", num.bright_black(), self.display_spans(spans, " ".into()), - Self::display_kind(kind), + util::display_kind(kind), Self::display_time(time), text, Self::display_marker(has_desc, ""), @@ -152,16 +153,6 @@ impl ShowLines { } } - fn display_kind(kind: LineKind) -> ColoredString { - match kind { - LineKind::Task => "T".magenta().bold(), - LineKind::Done => "D".green().bold(), - LineKind::Canceled => "C".red().bold(), - LineKind::Note => "N".blue().bold(), - LineKind::Birthday => "B".yellow().bold(), - } - } - fn display_marker(marker: bool, otherwise: &str) -> ColoredString { if marker { "*".bright_yellow() diff --git a/src/cli/show.rs b/src/cli/show.rs index b0c399a..3eefd9c 100644 --- a/src/cli/show.rs +++ b/src/cli/show.rs @@ -1,52 +1,75 @@ use chrono::NaiveDate; +use codespan_reporting::files::Files as CsFiles; +use colored::Colorize; use crate::eval::{Entry, EntryKind}; use crate::files::commands::{Command, Log}; +use crate::files::primitives::Spanned; use crate::files::{Files, Sourced}; use super::error::Error; use super::layout::line::LineLayout; +use super::util; -fn show_command(command: &Command) { - for line in format!("{}", command).lines() { - println!("| {}", line); +fn fmt_where(files: &Files, command: &Sourced<'_, Spanned>) -> String { + let name = files.name(command.source.file()).expect("file exists"); + let line = files + .line_number(command.source.file(), command.value.span.start) + .expect("file exists and line is valid"); + format!("Line {} in {}", line, name) +} + +fn print_desc(command: &Sourced<'_, Spanned>) { + let desc: &[String] = match &command.value.value { + Command::Task(task) => &task.desc, + Command::Note(note) => ¬e.desc, + Command::Log(log) => &log.desc, + _ => &[], + }; + if !desc.is_empty() { + println!(); + for line in desc { + println!("{}", line); + } } } fn show_entry(files: &Files, entry: &Entry) { let command = files.command(entry.source); - let kind = match entry.kind { - EntryKind::Task | EntryKind::TaskDone(_) | EntryKind::TaskCanceled(_) => "TASK", - EntryKind::Note => "NOTE", - EntryKind::Birthday(_) => "BIRTHDAY", + let kind = util::display_kind(LineLayout::entry_kind(entry)); + println!("{} {} {}", "Title:".bright_black(), kind, entry.title); + + let what = match entry.kind { + EntryKind::Task => "Task".to_string(), + EntryKind::TaskDone(date) => format!("Task, done {}", date), + EntryKind::TaskCanceled(date) => format!("Task, canceled {}", date), + EntryKind::Note => "Note".to_string(), + EntryKind::Birthday(None) => "Birthday, age unknown".to_string(), + EntryKind::Birthday(Some(age)) => format!("Birthday, age {}", age), }; - println!("{} {}", kind, entry.title); + println!("{} {}", "What:".bright_black(), what); - if let Some(dates) = entry.dates { - println!("DATE {}", dates.sorted()); - } else { - println!("NO DATE"); - } + let when = match entry.dates { + None => "no date".to_string(), + Some(date) => format!("{}", date.sorted()), + }; + println!("{} {}", "When:".bright_black(), when); - match entry.kind { - EntryKind::TaskDone(when) => println!("DONE {}", when), - EntryKind::TaskCanceled(when) => println!("CANCELED {}", when), - EntryKind::Birthday(Some(age)) => println!("AGE {}", age), - _ => {} - } + println!("{} {}", "Where:".bright_black(), fmt_where(files, &command)); - println!("FROM COMMAND"); - show_command(&command.value.value); + print_desc(&command); } fn show_log(files: &Files, log: Sourced<'_, Log>) { let command = files.command(log.source); - println!("LOG {}", log.value.date); + println!("{} Log entry", "What:".bright_black()); + println!("{} {}", "When:".bright_black(), log.value.date); - println!("FROM COMMAND"); - show_command(&command.value.value); + println!("{} {}", "Where:".bright_black(), fmt_where(files, &command)); + + print_desc(&command); } fn show_ident(files: &Files, entries: &[Entry], layout: &LineLayout, ident: Ident) { @@ -76,6 +99,8 @@ pub fn show(files: &Files, entries: &[Entry], layout: &LineLayout, idents: &[Ide show_ident(files, entries, layout, idents[0]); for &ident in idents.iter().skip(1) { + println!(); + println!(); println!(); show_ident(files, entries, layout, ident); } diff --git a/src/cli/util.rs b/src/cli/util.rs new file mode 100644 index 0000000..bad291a --- /dev/null +++ b/src/cli/util.rs @@ -0,0 +1,13 @@ +use colored::{ColoredString, Colorize}; + +use super::layout::line::LineKind; + +pub fn display_kind(kind: LineKind) -> ColoredString { + match kind { + LineKind::Task => "T".magenta().bold(), + LineKind::Done => "D".green().bold(), + LineKind::Canceled => "C".red().bold(), + LineKind::Note => "N".blue().bold(), + LineKind::Birthday => "B".yellow().bold(), + } +}