some patches + doc

This commit is contained in:
Volodymyr Patuta 2020-11-27 16:02:08 +01:00
parent 39aebd3066
commit 9350f94611
1 changed files with 24 additions and 7 deletions

View File

@ -4,11 +4,13 @@ use std::{env, fs, process::Command};
use text_io::read;
use walkdir::WalkDir;
/// Get the path to the root directory of all notes.
fn get_base_path() -> Result<String> {
let home = env::var("HOME")?;
Ok(format!("{}/.rnote/", home))
}
/// Get path to a category/date directory.
fn get_path(category: &str) -> Result<String> {
let base = get_base_path()?;
let date = Utc::now().format("%Y-%m-%d");
@ -18,6 +20,7 @@ fn get_path(category: &str) -> Result<String> {
}
}
/// Create directory for a note.
pub fn create_dir(category: &str) -> Result<()> {
let path = get_base_path()?;
let date = Utc::now().format("%Y-%m-%d");
@ -28,22 +31,25 @@ pub fn create_dir(category: &str) -> Result<()> {
Ok(())
}
pub fn create_note(header: &str, category: &str) -> Result<()> {
/// Create a new note.
pub fn create(header: &str, category: &str) -> Result<()> {
let editor = env::var("EDITOR").unwrap_or("/bin/vi".to_owned());
let file = format!("{}{}.md", get_path(category)?, header);
is_duplicate(header, category)?;
create_dir(category)?;
Command::new(editor).arg(&file).status()?;
Ok(())
}
/// Checks if potentially new note name already exists.
fn is_duplicate(header: &str, category: &str) -> Result<()> {
let file = format!("{}{}", get_path(category)?, header);
let path = format!("{}", get_path(category)?);
for entry in WalkDir::new(path) {
let entry = entry?;
let p: &str = match entry.path().to_str() {
Some(s) => s,
None => "",
let p: String = match entry.path().to_str() {
Some(s) => format!("{}.md", s),
None => "".to_owned(),
};
if p == file {
return Err(anyhow!(
@ -54,9 +60,11 @@ fn is_duplicate(header: &str, category: &str) -> Result<()> {
Ok(())
}
/// Finds a path to desired note.
fn find_path(header: &str) -> Result<String> {
let mut paths: Vec<String> = Vec::new();
let base = get_base_path()?;
let header = format!("{}.md", header);
for entry in WalkDir::new(base) {
let entry = entry?;
let p: &str = match entry.path().to_str() {
@ -75,7 +83,7 @@ fn find_path(header: &str) -> Result<String> {
Err(anyhow!("Note not found."))
} else {
if paths.len() == 1 {
Ok(paths.remove(0))
Ok(format!("{}.md", paths[0]))
} else {
let mut n: usize;
loop {
@ -90,12 +98,13 @@ fn find_path(header: &str) -> Result<String> {
break;
}
}
Ok(paths.remove(n))
Ok(format!("{}.md", paths[n]))
}
}
}
pub fn delete_note(header: &str) -> Result<()> {
/// Deletes a note.
pub fn delete(header: &str) -> Result<()> {
let path = find_path(header)?;
println!("Are you sure you want to delete {} [Y/n]", header);
let response: String = read!();
@ -108,3 +117,11 @@ pub fn delete_note(header: &str) -> Result<()> {
Err(anyhow!("Abort."))
}
}
/// Modify a note.
pub fn modify(header: &str) -> Result<()> {
let editor = env::var("EDITOR").unwrap_or("/bin/vi".to_owned());
let file = find_path(header)?;
Command::new(editor).arg(&file).status()?;
Ok(())
}