From 70502677b061d9355cdc29eda616baff8237aa0f Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <90917-fiplox@users.noreply.framagit.org> Date: Sat, 12 Dec 2020 15:39:26 +0100 Subject: [PATCH] show only rnote root path --- src/rnote/app.rs | 10 +++++----- src/rnote/notes.rs | 28 +++++++++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/rnote/app.rs b/src/rnote/app.rs index 183f664..9e6ba32 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -11,15 +11,15 @@ pub fn make_app() -> App<'static, 'static> { SubCommand::with_name("new") .alias("n") .about("Create new note") + .arg( + Arg::with_name("header") + .index(1) + .help("Give name to the note."), + ) .arg( Arg::with_name("category") .help("Create note in category.") .index(2), - ) - .arg( - Arg::with_name("header") - .index(1) - .help("Give name to the file."), ), ) .subcommand( diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index b10a01a..0af8a8c 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -183,13 +183,16 @@ fn is_duplicate(header: &str, category: &str) -> Result<()> { /// Find a path to desired note and prompt to choose one to open. pub fn get_note_path_interractive(header: &str) -> Result> { let mut paths: Vec = get_note_path(header)?; + let mut p: Vec = paths.clone(); + let r = p[0].find("rnote").unwrap_or(0); + p = p.into_iter().map(|mut s| s.drain(r..).collect()).collect(); if paths.len() == 1 { Ok(Some(paths.remove(0))) } else { let selection = Select::with_theme(&ColorfulTheme::default()) .with_prompt("Optionally choose a note") .default(0) - .items(&paths) + .items(&p) .interact_opt()?; match selection { Some(s) => Ok(Some(paths.remove(s))), @@ -210,10 +213,12 @@ pub fn remove_note(path: &str) -> Result<()> { /// Prompt user to delete a note. pub fn remove_interractive(header: &str) -> Result<()> { let path = get_note_path_interractive(header)?; + if path.is_none() { + return Err(anyhow!("Abort.")); + } if Confirm::with_theme(&ColorfulTheme::default()) .with_prompt(format!("Do you want to delete {}?", header)) .interact()? - && path.is_some() { remove_note(&path.unwrap())?; Ok(()) @@ -242,10 +247,13 @@ pub fn modify(header: &str) -> Result<()> { /// Prompt user to open one of found notes by word. pub fn search_by_word(word: &str) -> Result<()> { let mut paths: Vec = get_files_by_word(word)?; + let mut p: Vec = paths.clone(); + let r = p[0].find("rnote").unwrap_or(0); + p = p.into_iter().map(|mut s| s.drain(r..).collect()).collect(); let selection = Select::with_theme(&ColorfulTheme::default()) .with_prompt("Optionally choose a note") .default(0) - .items(&paths) + .items(&p) .interact_opt()?; if let Some(selection) = selection { let editor = std::env::var("EDITOR")?; @@ -259,7 +267,7 @@ pub fn search_by_word(word: &str) -> Result<()> { /// Show all notes. pub fn show_all() -> Result<()> { - let mut files: Vec = get_all_notes()?; + let files: Vec = get_all_notes()?; let skin = show::make_skin(); let md = &files.join("---\n"); show::run_app(skin, md)?; @@ -282,7 +290,7 @@ pub fn show(header: &str) -> Result<()> { /// Show all notes in the given category. pub fn show_category(category: &str) -> Result<()> { - let mut files: Vec = get_notes_in_category(category)?; + let files: Vec = get_notes_in_category(category)?; let skin = show::make_skin(); let md = &files.join("---\n"); show::run_app(skin, md)?; @@ -292,10 +300,13 @@ pub fn show_category(category: &str) -> Result<()> { /// List all notes and prompt to open one. pub fn list_all_notes() -> Result<()> { let mut files: Vec = get_all_notes()?; + let mut p: Vec = files.clone(); + let r = p[0].find("rnote").unwrap_or(0); + p = p.into_iter().map(|mut s| s.drain(r..).collect()).collect(); let selection = Select::with_theme(&ColorfulTheme::default()) .with_prompt("Optionally choose a note") .default(0) - .items(&files) + .items(&p) .interact_opt()?; if let Some(selection) = selection { let editor = std::env::var("EDITOR")?; @@ -309,10 +320,13 @@ pub fn list_all_notes() -> Result<()> { /// List all notes in the given category and optionally open one. pub fn list_category(category: &str) -> Result<()> { let mut files: Vec = get_notes_in_category(category)?; + let mut p: Vec = files.clone(); + let r = p[0].find("rnote").unwrap_or(0); + p = p.into_iter().map(|mut s| s.drain(r..).collect()).collect(); let selection = Select::with_theme(&ColorfulTheme::default()) .with_prompt("Optionally choose a note") .default(0) - .items(&files) + .items(&p) .interact_opt()?; if let Some(selection) = selection { let editor = std::env::var("EDITOR")?;