Initial import for imag-notes

This commit is contained in:
Matthias Beyer 2016-03-19 16:47:56 +01:00
parent 115a8248a4
commit 8ee4a7ff50
3 changed files with 192 additions and 0 deletions

23
imag-notes/Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "imag-notes"
version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
semver = "0.2.1"
clap = "2.1.1"
log = "0.3.5"
version = "2.0.1"
[dependencies.libimagrt]
path = "../libimagrt"
[dependencies.libimagnotes]
path = "../libimagnotes"
[dependencies.libimagtag]
path = "../libimagtag"
[dependencies.libimagutil]
path = "../libimagutil"

119
imag-notes/src/main.rs Normal file
View file

@ -0,0 +1,119 @@
extern crate clap;
#[macro_use] extern crate log;
extern crate semver;
#[macro_use] extern crate version;
extern crate libimagnotes;
extern crate libimagrt;
extern crate libimagtag;
extern crate libimagutil;
use std::process::exit;
use libimagrt::runtime::Runtime;
use libimagnotes::note::Note;
use libimagutil::trace::trace_error;
mod ui;
use ui::build_ui;
fn main() {
let name = "imag-notes";
let version = &version!()[..];
let about = "Note taking helper";
let ui = build_ui(Runtime::get_default_cli_builder(name, version, about));
let rt = {
let rt = Runtime::new(ui);
if rt.is_ok() {
rt.unwrap()
} else {
println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap());
exit(1);
}
};
debug!("Hello. Logging was just enabled");
debug!("I already set up the Runtime object and build the commandline interface parser.");
debug!("Lets get rollin' ...");
rt.cli()
.subcommand_name()
.map(|name| {
debug!("Call: {}", name);
match name {
"create" => create(&rt),
"delete" => delete(&rt),
"edit" => edit(&rt),
"list" => list(&rt),
_ => {
debug!("Unknown command"); // More error handling
},
};
});
}
fn create(rt: &Runtime) {
let name = rt.cli()
.subcommand_matches("create")
.unwrap() // we already know it is there
.value_of("name")
.unwrap(); // enforced by clap
Note::new(rt.store(), String::from(name), String::new())
.map_err(|e| trace_error(&e))
.map(|note| {
// call editor now...
});
}
fn delete(rt: &Runtime) {
let name = rt.cli()
.subcommand_matches("delete")
.unwrap() // we already know it is there
.value_of("name")
.unwrap(); // enforced by clap
Note::delete(rt.store(), String::from(name))
.map_err(|e| trace_error(&e))
.map(|_| println!("Ok"));
}
fn edit(rt: &Runtime) {
unimplemented!()
}
fn list(rt: &Runtime) {
use std::cmp::Ordering;
let iter = Note::all_notes(rt.store());
if iter.is_err() {
trace_error(&iter.err().unwrap());
exit(1);
}
let mut iter = iter.unwrap()
.filter_map(|note| {
match note {
Err(e) => {
trace_error(&e);
None
},
Ok(e) => Some(e)
}
})
.collect::<Vec<Note>>();
iter.sort_by(|note_a, note_b| {
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
return a.cmp(&b)
} else {
return Ordering::Greater;
}
});
for note in iter {
note.get_name().map(|name| println!("{}", name));
}
}

50
imag-notes/src/ui.rs Normal file
View file

@ -0,0 +1,50 @@
use clap::{Arg, ArgGroup, App, SubCommand};
use libimagtag::ui::tag_argument;
use libimagtag::ui::tag_argument_name;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
.subcommand(SubCommand::with_name("create")
.about("Create a note")
.version("0.1")
.arg(Arg::with_name("name")
.long("name")
.short("n")
.takes_value(true)
.required(true)
.help("Create Note with this name")))
.subcommand(SubCommand::with_name("delete")
.about("Delete a Note")
.version("0.1")
.arg(Arg::with_name("name")
.long("name")
.short("n")
.takes_value(true)
.required(true)
.help("Delete Note with this name")))
.subcommand(SubCommand::with_name("edit")
.about("Edit a Note")
.version("0.1")
.arg(Arg::with_name("name")
.long("name")
.short("n")
.takes_value(true)
.required(true)
.help("Edit Note with this name"))
.arg(tag_argument())
.group(ArgGroup::with_name("editargs")
.args(&[tag_argument_name(), "name"])
.required(true))
)
.subcommand(SubCommand::with_name("list")
.about("List Notes")
.version("0.1"))
}