replace "header" by "name"
This commit is contained in:
parent
b717d6a832
commit
438835862f
@ -12,7 +12,7 @@ pub fn make_app() -> App<'static, 'static> {
|
|||||||
.alias("n")
|
.alias("n")
|
||||||
.about("Create new note")
|
.about("Create new note")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("header")
|
Arg::with_name("name")
|
||||||
.index(1)
|
.index(1)
|
||||||
.help("Give name to the note."),
|
.help("Give name to the note."),
|
||||||
)
|
)
|
||||||
@ -27,12 +27,12 @@ pub fn make_app() -> App<'static, 'static> {
|
|||||||
.alias("r")
|
.alias("r")
|
||||||
.alias("rm")
|
.alias("rm")
|
||||||
.about("Remove a note.")
|
.about("Remove a note.")
|
||||||
.arg(Arg::with_name("header").help("Name of the note."))
|
.arg(Arg::with_name("name").help("Name of the note."))
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("date")
|
Arg::with_name("date")
|
||||||
.help("Delete all notes created at given date.")
|
.help("Delete all notes created at given date.")
|
||||||
.short("d")
|
.short("d")
|
||||||
.conflicts_with("header")
|
.conflicts_with("name")
|
||||||
.long("date"),
|
.long("date"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@ -40,7 +40,7 @@ pub fn make_app() -> App<'static, 'static> {
|
|||||||
SubCommand::with_name("edit")
|
SubCommand::with_name("edit")
|
||||||
.alias("e")
|
.alias("e")
|
||||||
.about("Edit a note.")
|
.about("Edit a note.")
|
||||||
.arg(Arg::with_name("header").help("Name of the note.")),
|
.arg(Arg::with_name("name").help("Name of the note.")),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("list")
|
SubCommand::with_name("list")
|
||||||
@ -62,10 +62,10 @@ pub fn make_app() -> App<'static, 'static> {
|
|||||||
Arg::with_name("word")
|
Arg::with_name("word")
|
||||||
.help("Search by word.")
|
.help("Search by word.")
|
||||||
.short("w")
|
.short("w")
|
||||||
.conflicts_with("header")
|
.conflicts_with("name")
|
||||||
.long("word"),
|
.long("word"),
|
||||||
)
|
)
|
||||||
.arg(Arg::with_name("header").help("Name of the note.")),
|
.arg(Arg::with_name("name").help("Name of the note.")),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("show")
|
SubCommand::with_name("show")
|
||||||
@ -74,7 +74,7 @@ pub fn make_app() -> App<'static, 'static> {
|
|||||||
Arg::with_name("all")
|
Arg::with_name("all")
|
||||||
.help("Show all notes.")
|
.help("Show all notes.")
|
||||||
.short("a")
|
.short("a")
|
||||||
.conflicts_with("header")
|
.conflicts_with("name")
|
||||||
.long("all"),
|
.long("all"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@ -82,9 +82,9 @@ pub fn make_app() -> App<'static, 'static> {
|
|||||||
.help("Show all notes from a category/date.")
|
.help("Show all notes from a category/date.")
|
||||||
.short("c")
|
.short("c")
|
||||||
.long("category")
|
.long("category")
|
||||||
.conflicts_with("header"),
|
.conflicts_with("name"),
|
||||||
)
|
)
|
||||||
.arg(Arg::with_name("header").help("Name of the note.")),
|
.arg(Arg::with_name("name").help("Name of the note.")),
|
||||||
)
|
)
|
||||||
.subcommand(SubCommand::with_name("panic").about("Delete all notes."))
|
.subcommand(SubCommand::with_name("panic").about("Delete all notes."))
|
||||||
}
|
}
|
||||||
|
@ -97,21 +97,21 @@ pub fn create_dir(category: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Find a path to desired note.
|
/// Find a path to desired note.
|
||||||
pub fn get_note_path(header: &str) -> Result<Vec<String>> {
|
pub fn get_note_path(name: &str) -> Result<Vec<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);
|
let name = format!("{}.md", name);
|
||||||
for entry in WalkDir::new(base) {
|
for entry in WalkDir::new(base) {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
let p: String = match entry.path().to_str() {
|
let p: String = match entry.path().to_str() {
|
||||||
Some(s) => s.to_owned(),
|
Some(s) => s.to_owned(),
|
||||||
None => "".to_owned(),
|
None => "".to_owned(),
|
||||||
};
|
};
|
||||||
let name: &str = match entry.file_name().to_str() {
|
let file_name: &str = match entry.file_name().to_str() {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => "",
|
None => "",
|
||||||
};
|
};
|
||||||
if name == header {
|
if file_name == name {
|
||||||
paths.push(p);
|
paths.push(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,31 +149,31 @@ pub fn get_files_by_word(word: &str) -> Result<Vec<String>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new note.
|
/// Create a new note.
|
||||||
pub fn create(header: &str, category: &str) -> Result<()> {
|
pub fn create(name: &str, category: &str) -> Result<()> {
|
||||||
let editor = env::var("EDITOR")?;
|
let editor = env::var("EDITOR")?;
|
||||||
let file = format!("{}{}.md", get_category_path(category)?, header);
|
let file = format!("{}{}.md", get_category_path(category)?, name);
|
||||||
create_dir(category)?;
|
create_dir(category)?;
|
||||||
is_duplicate(header, category)?;
|
is_duplicate(name, category)?;
|
||||||
let mut f = fs::File::create(&file)?;
|
let mut f = fs::File::create(&file)?;
|
||||||
let username = env::var("USER").unwrap_or("".to_owned());
|
let username = env::var("USER").unwrap_or("".to_owned());
|
||||||
let date = Utc::now().format("%d-%m-%Y");
|
let date = Utc::now().format("%d-%m-%Y");
|
||||||
let note_header = format!(
|
let note_name = format!(
|
||||||
r#"---
|
r#"---
|
||||||
title: {}
|
title: {}
|
||||||
author: {}
|
author: {}
|
||||||
date: {}
|
date: {}
|
||||||
---"#,
|
---"#,
|
||||||
header, username, date
|
name, username, date
|
||||||
);
|
);
|
||||||
f.set_permissions(fs::Permissions::from_mode(0o600))?;
|
f.set_permissions(fs::Permissions::from_mode(0o600))?;
|
||||||
f.write(format!("{}\n", note_header).as_bytes())?;
|
f.write(format!("{}\n", note_name).as_bytes())?;
|
||||||
Command::new(editor).arg(&file).status()?;
|
Command::new(editor).arg(&file).status()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if potentially new note name already exists.
|
/// Check if potentially new note name already exists.
|
||||||
fn is_duplicate(header: &str, category: &str) -> Result<()> {
|
fn is_duplicate(name: &str, category: &str) -> Result<()> {
|
||||||
let file = format!("{}{}.md", get_category_path(category)?, header);
|
let file = format!("{}{}.md", get_category_path(category)?, name);
|
||||||
let path = format!("{}", get_category_path(category)?);
|
let path = format!("{}", get_category_path(category)?);
|
||||||
for entry in WalkDir::new(path) {
|
for entry in WalkDir::new(path) {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
@ -191,8 +191,8 @@ 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(name: &str) -> Result<Option<String>> {
|
||||||
let mut paths: Vec<String> = get_note_path(header)?;
|
let mut paths: Vec<String> = get_note_path(name)?;
|
||||||
let mut p: Vec<String> = paths.clone();
|
let mut p: Vec<String> = paths.clone();
|
||||||
let r = p[0].find("rnote").unwrap_or(0);
|
let r = p[0].find("rnote").unwrap_or(0);
|
||||||
p = p.into_iter().map(|mut s| s.drain(r..).collect()).collect();
|
p = p.into_iter().map(|mut s| s.drain(r..).collect()).collect();
|
||||||
@ -221,13 +221,13 @@ 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(name: &str) -> Result<()> {
|
||||||
let path = get_note_path_interractive(header)?;
|
let path = get_note_path_interractive(name)?;
|
||||||
if path.is_none() {
|
if path.is_none() {
|
||||||
return Err(anyhow!("Abort."));
|
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 {}?", name))
|
||||||
.interact()?
|
.interact()?
|
||||||
{
|
{
|
||||||
remove_note(&path.unwrap())?;
|
remove_note(&path.unwrap())?;
|
||||||
@ -238,9 +238,9 @@ pub fn remove_interractive(header: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Modify a note.
|
/// Modify a note.
|
||||||
pub fn modify(header: &str) -> Result<()> {
|
pub fn modify(name: &str) -> Result<()> {
|
||||||
let editor = env::var("EDITOR")?;
|
let editor = env::var("EDITOR")?;
|
||||||
let file = get_note_path_interractive(header)?;
|
let file = get_note_path_interractive(name)?;
|
||||||
match file {
|
match file {
|
||||||
Some(f) => {
|
Some(f) => {
|
||||||
Command::new(editor).arg(f).status()?;
|
Command::new(editor).arg(f).status()?;
|
||||||
@ -298,8 +298,8 @@ pub fn show_all() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Show one note.
|
/// Show one note.
|
||||||
pub fn show(header: &str) -> Result<()> {
|
pub fn show(name: &str) -> Result<()> {
|
||||||
let path = get_note_path_interractive(header)?;
|
let path = get_note_path_interractive(name)?;
|
||||||
match path {
|
match path {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
let skin = show::make_skin();
|
let skin = show::make_skin();
|
||||||
|
@ -5,7 +5,7 @@ use dialoguer::{theme::ColorfulTheme, Input};
|
|||||||
|
|
||||||
/// Process argument `new`.
|
/// Process argument `new`.
|
||||||
pub fn new(matches: &ArgMatches) -> Result<()> {
|
pub fn new(matches: &ArgMatches) -> Result<()> {
|
||||||
let header = match matches.value_of("header") {
|
let name = match matches.value_of("name") {
|
||||||
Some(s) => s.to_owned(),
|
Some(s) => s.to_owned(),
|
||||||
None => Input::with_theme(&ColorfulTheme::default())
|
None => Input::with_theme(&ColorfulTheme::default())
|
||||||
.with_prompt("Name of your note")
|
.with_prompt("Name of your note")
|
||||||
@ -19,13 +19,13 @@ pub fn new(matches: &ArgMatches) -> Result<()> {
|
|||||||
.interact_text()?,
|
.interact_text()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
notes::create(&header, &category)?;
|
notes::create(&name, &category)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process argument `remove`.
|
/// Process argument `remove`.
|
||||||
pub fn remove(matches: &ArgMatches) -> Result<()> {
|
pub fn remove(matches: &ArgMatches) -> Result<()> {
|
||||||
match matches.value_of("header") {
|
match matches.value_of("name") {
|
||||||
Some(s) => notes::remove_interractive(s)?,
|
Some(s) => notes::remove_interractive(s)?,
|
||||||
None => match matches.is_present("date") {
|
None => match matches.is_present("date") {
|
||||||
true => {
|
true => {
|
||||||
@ -36,10 +36,10 @@ pub fn remove(matches: &ArgMatches) -> Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
let header: String = Input::with_theme(&ColorfulTheme::default())
|
let name: String = Input::with_theme(&ColorfulTheme::default())
|
||||||
.with_prompt("Name of your note")
|
.with_prompt("Name of your note")
|
||||||
.interact_text()?;
|
.interact_text()?;
|
||||||
notes::remove_interractive(&header)?;
|
notes::remove_interractive(&name)?;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -48,14 +48,14 @@ pub fn remove(matches: &ArgMatches) -> Result<()> {
|
|||||||
|
|
||||||
/// Process argument `remove`.
|
/// Process argument `remove`.
|
||||||
pub fn edit(matches: &ArgMatches) -> Result<()> {
|
pub fn edit(matches: &ArgMatches) -> Result<()> {
|
||||||
let header = match matches.value_of("header") {
|
let name = match matches.value_of("name") {
|
||||||
Some(s) => s.to_owned(),
|
Some(s) => s.to_owned(),
|
||||||
None => Input::with_theme(&ColorfulTheme::default())
|
None => Input::with_theme(&ColorfulTheme::default())
|
||||||
.with_prompt("Name of your note")
|
.with_prompt("Name of your note")
|
||||||
.interact_text()?,
|
.interact_text()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
notes::modify(&header)?;
|
notes::modify(&name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ pub fn list(matches: &ArgMatches) -> Result<()> {
|
|||||||
|
|
||||||
/// Process argument `search`.
|
/// Process argument `search`.
|
||||||
pub fn search(matches: &ArgMatches) -> Result<()> {
|
pub fn search(matches: &ArgMatches) -> Result<()> {
|
||||||
match matches.value_of("header") {
|
match matches.value_of("name") {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
let p = notes::get_note_path_interractive(s)?;
|
let p = notes::get_note_path_interractive(s)?;
|
||||||
match p {
|
match p {
|
||||||
@ -101,7 +101,7 @@ pub fn search(matches: &ArgMatches) -> Result<()> {
|
|||||||
|
|
||||||
/// Process argument `show`.
|
/// Process argument `show`.
|
||||||
pub fn show(matches: &ArgMatches) -> Result<()> {
|
pub fn show(matches: &ArgMatches) -> Result<()> {
|
||||||
match matches.value_of("header") {
|
match matches.value_of("name") {
|
||||||
Some(s) => notes::show(s)?,
|
Some(s) => notes::show(s)?,
|
||||||
None => match matches.is_present("all") {
|
None => match matches.is_present("all") {
|
||||||
true => notes::show_all()?,
|
true => notes::show_all()?,
|
||||||
|
Loading…
Reference in New Issue
Block a user