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