Add option to edit after creating

This commit is contained in:
Matthias Beyer 2016-03-26 16:06:12 +01:00
parent 272243a125
commit 15dc163504
2 changed files with 29 additions and 12 deletions

View file

@ -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(&note.err().unwrap());
warn!("Cannot edit nonexistent Note");
} else {
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) {

View file

@ -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")