Improve format of show command

This commit is contained in:
Joscha 2022-01-09 16:04:34 +01:00
parent b430f1594f
commit 26479ac58d
5 changed files with 66 additions and 36 deletions

View file

@ -21,6 +21,7 @@ mod layout;
mod log;
mod print;
mod show;
mod util;
#[derive(Debug, StructOpt)]
pub struct Opt {

View file

@ -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,

View file

@ -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()

View file

@ -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<Command>>) -> 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<Command>>) {
let desc: &[String] = match &command.value.value {
Command::Task(task) => &task.desc,
Command::Note(note) => &note.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);
}

13
src/cli/util.rs Normal file
View file

@ -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(),
}
}