show only rnote root path

This commit is contained in:
Volodymyr Patuta 2020-12-12 15:39:26 +01:00
parent 045e65c170
commit 70502677b0
2 changed files with 26 additions and 12 deletions

View File

@ -11,15 +11,15 @@ pub fn make_app() -> App<'static, 'static> {
SubCommand::with_name("new") SubCommand::with_name("new")
.alias("n") .alias("n")
.about("Create new note") .about("Create new note")
.arg(
Arg::with_name("header")
.index(1)
.help("Give name to the note."),
)
.arg( .arg(
Arg::with_name("category") Arg::with_name("category")
.help("Create note in category.") .help("Create note in category.")
.index(2), .index(2),
)
.arg(
Arg::with_name("header")
.index(1)
.help("Give name to the file."),
), ),
) )
.subcommand( .subcommand(

View File

@ -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. /// Find a path to desired note and prompt to choose one to open.
pub fn get_note_path_interractive(header: &str) -> Result<Option<String>> { pub fn get_note_path_interractive(header: &str) -> Result<Option<String>> {
let mut paths: Vec<String> = get_note_path(header)?; let mut paths: Vec<String> = get_note_path(header)?;
let mut p: Vec<String> = 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 { if paths.len() == 1 {
Ok(Some(paths.remove(0))) Ok(Some(paths.remove(0)))
} else { } else {
let selection = Select::with_theme(&ColorfulTheme::default()) let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Optionally choose a note") .with_prompt("Optionally choose a note")
.default(0) .default(0)
.items(&paths) .items(&p)
.interact_opt()?; .interact_opt()?;
match selection { match selection {
Some(s) => Ok(Some(paths.remove(s))), Some(s) => Ok(Some(paths.remove(s))),
@ -210,10 +213,12 @@ pub fn remove_note(path: &str) -> Result<()> {
/// Prompt user to delete a note. /// Prompt user to delete a note.
pub fn remove_interractive(header: &str) -> Result<()> { pub fn remove_interractive(header: &str) -> Result<()> {
let path = get_note_path_interractive(header)?; let path = get_note_path_interractive(header)?;
if path.is_none() {
return Err(anyhow!("Abort."));
}
if Confirm::with_theme(&ColorfulTheme::default()) if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!("Do you want to delete {}?", header)) .with_prompt(format!("Do you want to delete {}?", header))
.interact()? .interact()?
&& path.is_some()
{ {
remove_note(&path.unwrap())?; remove_note(&path.unwrap())?;
Ok(()) Ok(())
@ -242,10 +247,13 @@ pub fn modify(header: &str) -> Result<()> {
/// Prompt user to open one of found notes by word. /// Prompt user to open one of found notes by word.
pub fn search_by_word(word: &str) -> Result<()> { pub fn search_by_word(word: &str) -> Result<()> {
let mut paths: Vec<String> = get_files_by_word(word)?; let mut paths: Vec<String> = get_files_by_word(word)?;
let mut p: Vec<String> = 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()) let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Optionally choose a note") .with_prompt("Optionally choose a note")
.default(0) .default(0)
.items(&paths) .items(&p)
.interact_opt()?; .interact_opt()?;
if let Some(selection) = selection { if let Some(selection) = selection {
let editor = std::env::var("EDITOR")?; let editor = std::env::var("EDITOR")?;
@ -259,7 +267,7 @@ pub fn search_by_word(word: &str) -> Result<()> {
/// Show all notes. /// Show all notes.
pub fn show_all() -> Result<()> { pub fn show_all() -> Result<()> {
let mut files: Vec<String> = get_all_notes()?; let files: Vec<String> = get_all_notes()?;
let skin = show::make_skin(); let skin = show::make_skin();
let md = &files.join("---\n"); let md = &files.join("---\n");
show::run_app(skin, md)?; show::run_app(skin, md)?;
@ -282,7 +290,7 @@ pub fn show(header: &str) -> Result<()> {
/// Show all notes in the given category. /// Show all notes in the given category.
pub fn show_category(category: &str) -> Result<()> { pub fn show_category(category: &str) -> Result<()> {
let mut files: Vec<String> = get_notes_in_category(category)?; let files: Vec<String> = get_notes_in_category(category)?;
let skin = show::make_skin(); let skin = show::make_skin();
let md = &files.join("---\n"); let md = &files.join("---\n");
show::run_app(skin, md)?; show::run_app(skin, md)?;
@ -292,10 +300,13 @@ pub fn show_category(category: &str) -> Result<()> {
/// List all notes and prompt to open one. /// List all notes and prompt to open one.
pub fn list_all_notes() -> Result<()> { pub fn list_all_notes() -> Result<()> {
let mut files: Vec<String> = get_all_notes()?; let mut files: Vec<String> = get_all_notes()?;
let mut p: Vec<String> = 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()) let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Optionally choose a note") .with_prompt("Optionally choose a note")
.default(0) .default(0)
.items(&files) .items(&p)
.interact_opt()?; .interact_opt()?;
if let Some(selection) = selection { if let Some(selection) = selection {
let editor = std::env::var("EDITOR")?; 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. /// List all notes in the given category and optionally open one.
pub fn list_category(category: &str) -> Result<()> { pub fn list_category(category: &str) -> Result<()> {
let mut files: Vec<String> = get_notes_in_category(category)?; let mut files: Vec<String> = get_notes_in_category(category)?;
let mut p: Vec<String> = 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()) let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Optionally choose a note") .with_prompt("Optionally choose a note")
.default(0) .default(0)
.items(&files) .items(&p)
.interact_opt()?; .interact_opt()?;
if let Some(selection) = selection { if let Some(selection) = selection {
let editor = std::env::var("EDITOR")?; let editor = std::env::var("EDITOR")?;