From 15dc1635043fcb456ad988e12089213f078d68fe Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Mar 2016 16:06:12 +0100 Subject: [PATCH] Add option to edit after creating --- imag-notes/src/main.rs | 32 +++++++++++++++++++++----------- imag-notes/src/ui.rs | 9 ++++++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/imag-notes/src/main.rs b/imag-notes/src/main.rs index 0e5fa67b..25d5fcf2 100644 --- a/imag-notes/src/main.rs +++ b/imag-notes/src/main.rs @@ -59,12 +59,16 @@ fn name_from_cli(rt: &Runtime, subcmd: &str) -> String { } fn create(rt: &Runtime) { - Note::new(rt.store(), String::from(name_from_cli(rt, "create")), String::new()) + let name = name_from_cli(rt, "create"); + Note::new(rt.store(), name.clone(), String::new()) .map_err(|e| trace_error(&e)) - .map(|_| { - // call editor now... - }) .ok(); + + if rt.cli().subcommand_matches("create").unwrap().is_present("edit") { + if !edit_entry(rt, name) { + exit(1); + } + } } fn delete(rt: &Runtime) { @@ -75,18 +79,24 @@ fn delete(rt: &Runtime) { } fn edit(rt: &Runtime) { - let note = Note::retrieve(rt.store(), name_from_cli(rt, "edit")); + edit_entry(rt, name_from_cli(rt, "edit")); +} + +fn edit_entry(rt: &Runtime, name: String) -> bool { + let note = Note::retrieve(rt.store(), name); if note.is_err() { trace_error(¬e.err().unwrap()); warn!("Cannot edit nonexistent Note"); - } else { - let mut note = note.unwrap(); - if let Err(e) = note.edit_content(rt) { - trace_error(&e); - warn!("Editing failed"); - } + return false } + let mut note = note.unwrap(); + if let Err(e) = note.edit_content(rt) { + trace_error(&e); + warn!("Editing failed"); + return false + } + true } fn list(rt: &Runtime) { diff --git a/imag-notes/src/ui.rs b/imag-notes/src/ui.rs index 5073bd7b..e68d5f88 100644 --- a/imag-notes/src/ui.rs +++ b/imag-notes/src/ui.rs @@ -13,7 +13,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .short("n") .takes_value(true) .required(true) - .help("Create Note with this name"))) + .help("Create Note with this name")) + .arg(Arg::with_name("edit") + .long("edit") + .short("e") + .takes_value(false) + .required(false) + .help("Edit after creating")) + ) .subcommand(SubCommand::with_name("delete") .about("Delete a Note")