Implement 'today new'

This commit is contained in:
Joscha 2022-01-14 22:37:40 +01:00
parent 0dcb75b103
commit 3befb7c773
8 changed files with 174 additions and 5 deletions

View file

@ -19,6 +19,7 @@ mod done;
mod error;
mod layout;
mod log;
mod new;
mod print;
mod show;
mod util;
@ -47,6 +48,12 @@ pub enum Command {
#[structopt(required = true)]
identifiers: Vec<String>,
},
/// Create a new entry based on a template
#[structopt(alias = "n")]
New {
#[structopt(subcommand)]
template: Template,
},
/// Marks one or more entries as done
#[structopt(alias = "d")]
Done {
@ -71,6 +78,14 @@ pub enum Command {
Fmt,
}
// TODO Add templates for tasks and notes
#[derive(Debug, StructOpt)]
pub enum Template {
/// An undated task marked as done today
#[structopt(alias = "d")]
Done,
}
fn default_file() -> PathBuf {
ProjectDirs::from("", "", "today")
.expect("could not determine config dir")
@ -136,6 +151,9 @@ fn run_command(opt: &Opt, files: &mut Files, range: DateRange, now: NaiveDateTim
let idents = parse_show_idents(identifiers, now.date())?;
show::show(files, &entries, &layout, &idents);
}
Some(Command::New { template }) => match template {
Template::Done => new::done(files, now)?,
},
Some(Command::Done { entries: ns }) => {
let entries = find_entries(files, range)?;
let layout = find_layout(files, &entries, range, now);

View file

@ -28,6 +28,8 @@ pub enum Error {
NoSuchLog(NaiveDate),
#[error("Not a task")]
NotATask(Vec<usize>),
#[error("No capture file found")]
NoCaptureFile,
#[error("Error editing: {0}")]
EditingIo(io::Error),
}
@ -55,6 +57,7 @@ where
eprintln!("{} are not tasks.", ns.join(", "));
}
}
Error::NoCaptureFile => eprintln!("No capture file found"),
Error::EditingIo(error) => {
eprintln!("Error while editing:");
eprintln!(" {error}");

46
src/cli/new.rs Normal file
View file

@ -0,0 +1,46 @@
use std::str::FromStr;
use chrono::NaiveDateTime;
use codespan_reporting::files::SimpleFile;
use crate::files::cli::CliCommand;
use crate::files::commands::{Done, DoneKind, Task};
use crate::files::Files;
use super::error::{Error, Result};
use super::util;
pub fn done(files: &mut Files, now: NaiveDateTime) -> Result<()> {
let capture = files.capture().ok_or(Error::NoCaptureFile)?;
let command = Task {
title: String::new(),
statements: vec![],
done: vec![Done {
kind: DoneKind::Done,
date: None,
done_at: now.date(),
}],
desc: vec![],
};
let mut text = format!("{command}");
let command = loop {
text = util::edit(&text)?;
match CliCommand::from_str(&text) {
Ok(command) => break command.0,
Err(e) => crate::error::eprint_error(&SimpleFile::new("new command", &text), &e),
}
if !matches!(
promptly::prompt_default("Continue editing?", true),
Ok(true)
) {
println!("Aborting");
return Ok(());
}
};
files.insert(capture, command);
Ok(())
}

View file

@ -424,7 +424,7 @@ impl Files {
file.dirty = true;
}
fn insert(&mut self, file: FileSource, command: Command) {
pub fn insert(&mut self, file: FileSource, command: Command) {
let file = &mut self.files[file.0];
file.file.commands.push(Spanned::dummy(command));
file.dirty = true;

View file

@ -156,4 +156,4 @@ cli_ident = { SOI ~ (cli_date | number) ~ EOI }
cli_range_start = { cli_datum ~ delta? }
cli_range_end = { cli_datum ~ delta? | delta }
cli_range = { SOI ~ cli_range_start ~ ("--" ~ cli_range_end)? ~ EOI }
cli_command = { SOI ~ command ~ EOI }
cli_command = ${ SOI ~ empty_line* ~ command ~ empty_line* ~ WHITESPACE* ~ EOI }