From 58d303be02eae3136cd6d4b5f74eec283ad354a2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Jan 2016 21:17:26 +0100 Subject: [PATCH 1/2] Add CLI specification --- etc/cli.yml | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/etc/cli.yml b/etc/cli.yml index a7b7de64..553a8f52 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -314,6 +314,45 @@ subcommands: required: false takes_value: true + - show: + about: Show note(s) + version: 0.1 + author: Matthias Beyer + args: + - plain: + long: plain + short: p + help: Show notes plain (like with 'cat') + required: false + takes_value: false + + - id: + long: id + help: Open note with this ID + required: false + takes_value: true + + - namegrep: + short: n + long: name + help: Open where name matches this regex + required: false + takes_value: true + + - grep: + short: g + long: grep + help: Open where grep with regex finds something + required: false + takes_value: true + + - tags: + short: t + long: tags + help: Open all notes with these tags + required: false + takes_value: true + - open: about: Open notes as HTML page in browser (via XDG-open) version: 0.1 From 3bee78273ec334d77ad9a4c1c696fa866e06f8f4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Jan 2016 21:34:03 +0100 Subject: [PATCH 2/2] Notes: Implement command_show() --- src/module/notes/mod.rs | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index 004f2ad5..07f10fdc 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -122,6 +122,49 @@ impl<'a> Notes<'a> { return failed == 0; } + fn command_show(&self, matches: &ArgMatches) -> bool { + use self::header::get_name_from_header; + use self::header::get_tags_from_header; + + let parser = Parser::new(JsonHeaderParser::new(None)); + + let filter = { + let hash_filter = create_hash_filter(matches, "id", true); + let head_filter = create_text_header_field_grep_filter(matches, "match", "NAME", true); + let text_filter = create_content_grep_filter(matches, "match", true); + let tags_filter = create_tag_filter(matches, "tags", true); + hash_filter.and(Box::new(head_filter)).and(Box::new(text_filter)).and(Box::new(tags_filter)) + }; + + self.rt + .store() + .load_for_module(self, &parser) + .into_iter() + .filter(|file| { + let res = filter.filter_file(file); + debug!("Filter: {} -> {}", file.deref().borrow().id(), res); + res + }) + .map(|file| { + let content = file.deref().borrow().data().clone(); + + let text = if matches.is_present("plain") { + parser.write((file.deref().borrow().header(), &content)) + .unwrap_or(format!("Parser error for file: {}", file.deref().borrow().id())) + } else { + let tags = get_tags_from_header(file.deref().borrow().header()); + let name = get_name_from_header(file.deref().borrow().header()); + format!("Name = '{}'\nTags = '{}'\n\n{}\n\n", + name, tags.join(", "), content) + }; + + println!("{:-<79}", "-"); + println!("{}", text); + true + }) + .all(|x| x) + } + fn command_open(&self, matches: &ArgMatches) -> bool { let parser = Parser::new(JsonHeaderParser::new(None)); @@ -455,6 +498,10 @@ impl<'a> Module<'a> for Notes<'a> { self.command_edit(matches.subcommand_matches("edit").unwrap()) }, + Some("show") => { + self.command_show(matches.subcommand_matches("show").unwrap()) + }, + Some("open") => { self.command_open(matches.subcommand_matches("open").unwrap()) },