Notes: Add command_edit()

This commit is contained in:
Matthias Beyer 2016-01-02 19:44:14 +01:00
parent 8ac798687a
commit 9ac388c2b6
2 changed files with 95 additions and 0 deletions

View file

@ -282,6 +282,38 @@ subcommands:
required: false
takes_value: true
- edit:
about: Edit Note
version: 0.1
author: Matthias Beyer <mail@beyermatthias.de>
args:
- id:
long: id
help: Edit Note with this ID
required: false
takes_value: true
- namegrep:
short: n
long: name
help: Edit notes where the name matches this regex
required: false
takes_value: true
- grep:
short: g
long: grep
help: Edit notes where the content matches this regex
required: false
takes_value: true
- tags:
short: t
long: tags
help: Edit notes with these tags
required: false
takes_value: true
- list:
about: List notes
version: 0.1

View file

@ -63,6 +63,65 @@ impl<'a> Notes<'a> {
.unwrap_or(false)
}
fn command_edit(&self, matches: &ArgMatches) -> bool {
use ui::external::editor::edit_content;
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = {
let hash_filter = create_hash_filter(matches, "id", false);
let head_filter = create_text_header_field_grep_filter(matches, "namematch", "NAME", false);
let text_filter = create_content_grep_filter(matches, "match", false);
let tags_filter = create_tag_filter(matches, "tags", false);
hash_filter.or(Box::new(head_filter)).or(Box::new(text_filter)).or(Box::new(tags_filter))
};
let result = self.rt
.store()
.load_for_module(self, &parser)
.into_iter()
.filter(|f| filter.filter_file(f))
.map(|file| {
debug!("File loaded, can edit now: {:?}", file);
let old_content = {
let f = file.deref().borrow();
f.data().clone()
};
debug!("Editing content now...");
let (new_content, editing_worked) = edit_content(self.runtime(), old_content);
debug!("... ready with editing");
if editing_worked {
debug!("Editing worked");
{
let mut f = file.deref().borrow_mut();
f.set_data(new_content);
}
self.runtime().store().persist(&parser, file)
} else {
debug!("Editing didn't work");
false
}
})
.fold((0, 0), |acc, succeeded| {
let (worked, failed) = acc;
if succeeded {
(worked + 1, failed)
} else {
(worked, failed + 1)
}
});
let (worked, failed) = result;
info!("Editing succeeded for {} files", worked);
info!("Editing failed for {} files", failed);
return failed == 0;
}
fn command_list(&self, matches: &ArgMatches) -> bool {
use ui::file::{FilePrinter, TablePrinter};
use self::header::get_name_from_header;
@ -184,6 +243,10 @@ impl<'a> Module<'a> for Notes<'a> {
self.command_add(matches.subcommand_matches("add").unwrap())
},
Some("edit") => {
self.command_edit(matches.subcommand_matches("edit").unwrap())
},
Some("list") => {
self.command_list(matches.subcommand_matches("list").unwrap())
},