Show single entries

This commit is contained in:
Joscha 2021-12-13 18:59:44 +00:00
parent 6365d2621b
commit 34a053cbe4
5 changed files with 81 additions and 5 deletions

View file

@ -8,6 +8,9 @@ use structopt::StructOpt;
use crate::eval::{DateRange, EntryMode};
use crate::files::Files;
use self::error::Result;
mod error;
mod layout;
mod show;
@ -26,6 +29,7 @@ pub struct Opt {
#[structopt(short, long, default_value = "13")]
after: u32,
/// Number of the entry to view or edit
// TODO Select multiple entries at once
entry: Option<usize>,
#[structopt(subcommand)]
command: Option<Command>,
@ -49,7 +53,7 @@ fn default_file() -> PathBuf {
.join("main.today")
}
pub fn run() -> anyhow::Result<()> {
pub fn run() -> Result<()> {
let opt = Opt::from_args();
let file = opt.file.unwrap_or_else(default_file);
@ -69,8 +73,7 @@ pub fn run() -> anyhow::Result<()> {
match opt.command {
None | Some(Command::Show) => match opt.entry {
None => print!("{}", show::show_all(&layout)),
// Some(i) => print!("{}", render::render_entry(&files, &entries, &layout, i)),
Some(i) => todo!(),
Some(n) => show::show_entry(&files, &entries, &layout, n)?,
},
Some(Command::Done) => match opt.entry {
None => {

15
src/cli/error.rs Normal file
View file

@ -0,0 +1,15 @@
use std::result;
use crate::{eval, files};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
Files(#[from] files::Error),
#[error("{0}")]
Eval(#[from] eval::Error),
#[error("No entry with number {0}")]
NoSuchEntry(usize),
}
pub type Result<T> = result::Result<T, Error>;

View file

@ -11,6 +11,7 @@ use crate::eval::{Entry, EntryKind};
use crate::files::primitives::Time;
use crate::files::Files;
use super::super::error::{Error, Result};
use super::day::{DayEntry, DayLayout};
#[derive(Debug, Clone, Copy)]
@ -101,12 +102,13 @@ impl LineLayout {
&self.numbers
}
pub fn look_up_number(&self, number: usize) -> Option<usize> {
pub fn look_up_number(&self, number: usize) -> Result<usize> {
self.numbers
.iter()
.filter(|(_, n)| **n == number)
.map(|(i, _)| *i)
.next()
.ok_or_else(|| Error::NoSuchEntry(number))
}
fn render_layout_entry(&mut self, files: &Files, entries: &[Entry], l_entry: &DayEntry) {

View file

@ -2,8 +2,11 @@ use std::cmp;
use chrono::{Datelike, NaiveDate};
use crate::eval::{Entry, EntryKind};
use crate::files::primitives::{Time, Weekday};
use crate::files::Files;
use super::error::{Error, Result};
use super::layout::line::{LineEntry, LineLayout, SpanSegment};
struct ShowLines {
@ -119,3 +122,55 @@ pub fn show_all(layout: &LineLayout) -> String {
}
show_lines.result()
}
pub fn show_entry(
files: &Files,
entries: &[Entry],
layout: &LineLayout,
number: usize,
) -> Result<()> {
let index = layout.look_up_number(number)?;
let entry = &entries[index];
let command = files.command(entry.source);
match entry.kind {
EntryKind::Task => println!("TASK {}", command.title()),
EntryKind::TaskDone(when) => {
println!("DONE {}", command.title());
println!("DONE AT {}", when);
}
EntryKind::Note => println!("NOTE {}", command.title()),
EntryKind::Birthday(Some(age)) => {
println!("BIRTHDAY {}", command.title());
println!("AGE {}", age);
}
EntryKind::Birthday(None) => {
println!("BIRTHDAY {}", command.title());
println!("AGE UNKNOWN");
}
}
if let Some(dates) = entry.dates {
let (start, end) = dates.start_end();
if start == end {
match dates.start_end_time() {
Some((s, e)) if s == e => println!("DATE {} {}", start, s),
Some((s, e)) => println!("DATE {} {} -- {}", start, s, e),
None => println!("DATE {}", start),
}
} else {
match dates.start_end_time() {
Some((s, e)) => println!("DATE {} {} -- {} {}", start, s, end, e),
None => println!("DATE {} -- {}", start, end),
}
}
} else {
println!("NO DATE");
};
for line in command.desc() {
println!("# {}", line);
}
Ok(())
}

View file

@ -8,5 +8,6 @@ mod eval;
mod files;
fn main() -> anyhow::Result<()> {
cli::run()
cli::run()?;
Ok(())
}