Improve format of show command
This commit is contained in:
parent
b430f1594f
commit
26479ac58d
5 changed files with 66 additions and 36 deletions
|
|
@ -21,6 +21,7 @@ mod layout;
|
||||||
mod log;
|
mod log;
|
||||||
mod print;
|
mod print;
|
||||||
mod show;
|
mod show;
|
||||||
|
mod util;
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
pub struct Opt {
|
pub struct Opt {
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,7 @@ impl LineLayout {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entry_kind(entry: &Entry) -> LineKind {
|
pub fn entry_kind(entry: &Entry) -> LineKind {
|
||||||
match entry.kind {
|
match entry.kind {
|
||||||
EntryKind::Task => LineKind::Task,
|
EntryKind::Task => LineKind::Task,
|
||||||
EntryKind::TaskDone(_) => LineKind::Done,
|
EntryKind::TaskDone(_) => LineKind::Done,
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use colored::{ColoredString, Colorize};
|
||||||
use crate::files::primitives::{Time, Weekday};
|
use crate::files::primitives::{Time, Weekday};
|
||||||
|
|
||||||
use super::layout::line::{LineEntry, LineKind, LineLayout, SpanSegment, SpanStyle, Times};
|
use super::layout::line::{LineEntry, LineKind, LineLayout, SpanSegment, SpanStyle, Times};
|
||||||
|
use super::util;
|
||||||
|
|
||||||
struct ShowLines {
|
struct ShowLines {
|
||||||
num_width: usize,
|
num_width: usize,
|
||||||
|
|
@ -116,7 +117,7 @@ impl ShowLines {
|
||||||
"{:>nw$} {} {}{} {}{}{}\n",
|
"{:>nw$} {} {}{} {}{}{}\n",
|
||||||
num.bright_black(),
|
num.bright_black(),
|
||||||
self.display_spans(spans, " ".into()),
|
self.display_spans(spans, " ".into()),
|
||||||
Self::display_kind(kind),
|
util::display_kind(kind),
|
||||||
Self::display_time(time),
|
Self::display_time(time),
|
||||||
text,
|
text,
|
||||||
Self::display_marker(has_desc, ""),
|
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 {
|
fn display_marker(marker: bool, otherwise: &str) -> ColoredString {
|
||||||
if marker {
|
if marker {
|
||||||
"*".bright_yellow()
|
"*".bright_yellow()
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,75 @@
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
|
use codespan_reporting::files::Files as CsFiles;
|
||||||
|
use colored::Colorize;
|
||||||
|
|
||||||
use crate::eval::{Entry, EntryKind};
|
use crate::eval::{Entry, EntryKind};
|
||||||
use crate::files::commands::{Command, Log};
|
use crate::files::commands::{Command, Log};
|
||||||
|
use crate::files::primitives::Spanned;
|
||||||
use crate::files::{Files, Sourced};
|
use crate::files::{Files, Sourced};
|
||||||
|
|
||||||
use super::error::Error;
|
use super::error::Error;
|
||||||
use super::layout::line::LineLayout;
|
use super::layout::line::LineLayout;
|
||||||
|
use super::util;
|
||||||
|
|
||||||
fn show_command(command: &Command) {
|
fn fmt_where(files: &Files, command: &Sourced<'_, Spanned<Command>>) -> String {
|
||||||
for line in format!("{}", command).lines() {
|
let name = files.name(command.source.file()).expect("file exists");
|
||||||
println!("| {}", line);
|
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) => ¬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) {
|
fn show_entry(files: &Files, entry: &Entry) {
|
||||||
let command = files.command(entry.source);
|
let command = files.command(entry.source);
|
||||||
|
|
||||||
let kind = match entry.kind {
|
let kind = util::display_kind(LineLayout::entry_kind(entry));
|
||||||
EntryKind::Task | EntryKind::TaskDone(_) | EntryKind::TaskCanceled(_) => "TASK",
|
println!("{} {} {}", "Title:".bright_black(), kind, entry.title);
|
||||||
EntryKind::Note => "NOTE",
|
|
||||||
EntryKind::Birthday(_) => "BIRTHDAY",
|
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 {
|
let when = match entry.dates {
|
||||||
println!("DATE {}", dates.sorted());
|
None => "no date".to_string(),
|
||||||
} else {
|
Some(date) => format!("{}", date.sorted()),
|
||||||
println!("NO DATE");
|
};
|
||||||
}
|
println!("{} {}", "When:".bright_black(), when);
|
||||||
|
|
||||||
match entry.kind {
|
println!("{} {}", "Where:".bright_black(), fmt_where(files, &command));
|
||||||
EntryKind::TaskDone(when) => println!("DONE {}", when),
|
|
||||||
EntryKind::TaskCanceled(when) => println!("CANCELED {}", when),
|
|
||||||
EntryKind::Birthday(Some(age)) => println!("AGE {}", age),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("FROM COMMAND");
|
print_desc(&command);
|
||||||
show_command(&command.value.value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_log(files: &Files, log: Sourced<'_, Log>) {
|
fn show_log(files: &Files, log: Sourced<'_, Log>) {
|
||||||
let command = files.command(log.source);
|
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");
|
println!("{} {}", "Where:".bright_black(), fmt_where(files, &command));
|
||||||
show_command(&command.value.value);
|
|
||||||
|
print_desc(&command);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_ident(files: &Files, entries: &[Entry], layout: &LineLayout, ident: Ident) {
|
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]);
|
show_ident(files, entries, layout, idents[0]);
|
||||||
for &ident in idents.iter().skip(1) {
|
for &ident in idents.iter().skip(1) {
|
||||||
|
println!();
|
||||||
|
println!();
|
||||||
println!();
|
println!();
|
||||||
show_ident(files, entries, layout, ident);
|
show_ident(files, entries, layout, ident);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
src/cli/util.rs
Normal file
13
src/cli/util.rs
Normal 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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue