show all notes from a category

This commit is contained in:
Volodymyr Patuta 2020-11-29 23:33:14 +01:00
parent 12d9b54ea2
commit 6d3a932b54
3 changed files with 52 additions and 2 deletions

View File

@ -66,6 +66,14 @@ pub fn make_app() -> App<'static, 'static> {
.short("a")
.long("all"),
)
.arg(
Arg::with_name("category")
.help("Show all notes from a category/date")
.short("c")
.short("d")
.long("category")
.long("date"),
)
.arg(Arg::with_name("header").help("Name of the note.")),
)
}

View File

@ -190,3 +190,37 @@ pub fn show_all() -> Result<()> {
show::run_app(skin, md)?;
Ok(())
}
pub fn show(header: &str) -> Result<()> {
let path = find_path(header)?;
match path {
Some(s) => {
let skin = show::make_skin();
let content = fs::read_to_string(s)?;
show::run_app(skin, &content)?;
Ok(())
}
None => Err(anyhow!("Abort.")),
}
}
pub fn show_category(category: &str) -> Result<()> {
let base = get_base_path()?;
let path = format!("{}{}", base, category);
let mut files: Vec<String> = Vec::new();
if std::path::Path::new(&path).exists() {
for (_, file) in WalkDir::new(path)
.into_iter()
.filter_map(|file| file.ok())
.enumerate()
{
if file.metadata()?.is_file() {
files.push(fs::read_to_string(file.path())?);
}
}
let skin = show::make_skin();
let md = &files.join("---\n");
show::run_app(skin, md)?;
}
Ok(())
}

View File

@ -70,11 +70,19 @@ pub fn search(matches: &ArgMatches) -> Result<()> {
pub fn show(matches: &ArgMatches) -> Result<()> {
match matches.value_of("header") {
Some(s) => unimplemented!("{}", s),
Some(s) => notes::show(s)?,
None => match matches.is_present("all") {
true => notes::show_all()?,
false => match matches.is_present("category") {
true => {
let category: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Name of category:")
.interact_text()?;
notes::show_category(&category)?;
}
false => return Err(anyhow!("No option is given. Abort.")),
},
},
}
Ok(())
}