Prepare string editing for 'today new'

This commit is contained in:
Joscha 2022-01-14 22:05:52 +01:00
parent 82affe39a1
commit fe1bf309b8
4 changed files with 20 additions and 9 deletions

View file

@ -28,8 +28,8 @@ pub enum Error {
NoSuchLog(NaiveDate), NoSuchLog(NaiveDate),
#[error("Not a task")] #[error("Not a task")]
NotATask(Vec<usize>), NotATask(Vec<usize>),
#[error("Error editing log for {date}: {error}")] #[error("Error editing: {0}")]
EditingLog { date: NaiveDate, error: io::Error }, EditingIo(io::Error),
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -55,9 +55,9 @@ where
eprintln!("{} are not tasks.", ns.join(", ")); eprintln!("{} are not tasks.", ns.join(", "));
} }
} }
Error::EditingLog { date, error } => { Error::EditingIo(error) => {
eprintln!("Error editing log for {}", date); eprintln!("Error while editing:");
eprintln!(" {}", error); eprintln!(" {error}");
} }
} }
} }

View file

@ -3,6 +3,7 @@ use chrono::NaiveDate;
use crate::files::Files; use crate::files::Files;
use super::error::Error; use super::error::Error;
use super::util;
pub fn log(files: &mut Files, date: NaiveDate) -> Result<(), Error> { pub fn log(files: &mut Files, date: NaiveDate) -> Result<(), Error> {
let desc = files let desc = files
@ -10,10 +11,7 @@ pub fn log(files: &mut Files, date: NaiveDate) -> Result<(), Error> {
.map(|log| log.value.desc.join("\n")) .map(|log| log.value.desc.join("\n"))
.unwrap_or_default(); .unwrap_or_default();
let mut builder = edit::Builder::new(); let edited = util::edit_with_suffix(&desc, ".md")?;
builder.suffix(".md");
let edited = edit::edit_with_builder(desc, &builder)
.map_err(|error| Error::EditingLog { date, error })?;
let edited = edited let edited = edited
.lines() .lines()

View file

@ -1,5 +1,6 @@
use colored::{ColoredString, Colorize}; use colored::{ColoredString, Colorize};
use super::error::{Error, Result};
use super::layout::line::LineKind; use super::layout::line::LineKind;
pub fn display_kind(kind: LineKind) -> ColoredString { pub fn display_kind(kind: LineKind) -> ColoredString {
@ -11,3 +12,13 @@ pub fn display_kind(kind: LineKind) -> ColoredString {
LineKind::Birthday => "B".yellow().bold(), LineKind::Birthday => "B".yellow().bold(),
} }
} }
pub fn edit(input: &str) -> Result<String> {
edit::edit(input).map_err(Error::EditingIo)
}
pub fn edit_with_suffix(input: &str, suffix: &str) -> Result<String> {
let mut builder = edit::Builder::new();
builder.suffix(suffix);
edit::edit_with_builder(input, &builder).map_err(Error::EditingIo)
}

View file

@ -3,6 +3,8 @@
#![warn(clippy::all)] #![warn(clippy::all)]
#![warn(clippy::use_self)] #![warn(clippy::use_self)]
// TODO Switch to new format syntax project-wide
mod cli; mod cli;
mod error; mod error;
mod eval; mod eval;