search by header optionally open
This commit is contained in:
parent
5893174d59
commit
12d9b54ea2
@ -64,7 +64,7 @@ fn is_duplicate(header: &str, category: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Finds a path to desired note.
|
/// Finds a path to desired note.
|
||||||
pub fn find_path(header: &str) -> Result<String> {
|
pub fn find_path(header: &str) -> Result<Option<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 header = format!("{}.md", header);
|
||||||
@ -86,14 +86,17 @@ pub 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(Some(paths.remove(0)))
|
||||||
} else {
|
} else {
|
||||||
let selection = Select::with_theme(&ColorfulTheme::default())
|
let selection = Select::with_theme(&ColorfulTheme::default())
|
||||||
.with_prompt("Choose a note")
|
.with_prompt("Optionally choose a note")
|
||||||
.default(0)
|
.default(0)
|
||||||
.items(&paths)
|
.items(&paths)
|
||||||
.interact()?;
|
.interact_opt()?;
|
||||||
Ok(paths.remove(selection))
|
match selection {
|
||||||
|
Some(s) => Ok(Some(paths.remove(s))),
|
||||||
|
None => Ok(None),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,9 +107,10 @@ pub fn remove(header: &str) -> Result<()> {
|
|||||||
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()
|
||||||
{
|
{
|
||||||
println!("Deleting...");
|
println!("Deleting...");
|
||||||
fs::remove_file(path)?;
|
fs::remove_file(path.unwrap())?;
|
||||||
println!("Successfully deleted.");
|
println!("Successfully deleted.");
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@ -118,21 +122,23 @@ pub fn remove(header: &str) -> Result<()> {
|
|||||||
pub fn modify(header: &str) -> Result<()> {
|
pub fn modify(header: &str) -> Result<()> {
|
||||||
let editor = env::var("EDITOR")?;
|
let editor = env::var("EDITOR")?;
|
||||||
let file = find_path(header)?;
|
let file = find_path(header)?;
|
||||||
Command::new(editor).arg(&file).status()?;
|
match file {
|
||||||
|
Some(f) => {
|
||||||
|
Command::new(editor).arg(f).status()?;
|
||||||
println!("Edited successfully!");
|
println!("Edited successfully!");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
fn print_path(path: String) {
|
println!("Abort.");
|
||||||
let off = path.find(".rnote/").unwrap_or(path.len()) + 7;
|
Ok(())
|
||||||
let mut path = path;
|
}
|
||||||
path.drain(..off);
|
}
|
||||||
println!("{}", path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_by_word(word: &str) -> Result<()> {
|
pub fn search_by_word(word: &str) -> Result<()> {
|
||||||
extern crate fstream;
|
extern crate fstream;
|
||||||
let path = get_base_path()?;
|
let path = get_base_path()?;
|
||||||
|
let mut paths: Vec<String> = Vec::new();
|
||||||
for (_, file) in WalkDir::new(path)
|
for (_, file) in WalkDir::new(path)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|file| file.ok())
|
.filter_map(|file| file.ok())
|
||||||
@ -144,7 +150,7 @@ pub fn search_by_word(word: &str) -> Result<()> {
|
|||||||
if b {
|
if b {
|
||||||
let path = file.path().to_str().unwrap_or("");
|
let path = file.path().to_str().unwrap_or("");
|
||||||
if !path.is_empty() {
|
if !path.is_empty() {
|
||||||
print_path(path.to_owned());
|
paths.push(path.to_owned());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,6 +158,17 @@ pub fn search_by_word(word: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let selection = Select::with_theme(&ColorfulTheme::default())
|
||||||
|
.with_prompt("Optionally choose a note")
|
||||||
|
.default(0)
|
||||||
|
.items(&paths)
|
||||||
|
.interact_opt()?;
|
||||||
|
if let Some(selection) = selection {
|
||||||
|
let editor = std::env::var("EDITOR")?;
|
||||||
|
std::process::Command::new(editor)
|
||||||
|
.arg(paths.remove(selection))
|
||||||
|
.status()?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::rnote::notes;
|
use crate::rnote::notes;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use dialoguer::{theme::ColorfulTheme, Confirm, Input};
|
use dialoguer::{theme::ColorfulTheme, Input};
|
||||||
|
|
||||||
pub fn new(matches: &ArgMatches) -> Result<()> {
|
pub fn new(matches: &ArgMatches) -> Result<()> {
|
||||||
let header = match matches.value_of("header") {
|
let header = match matches.value_of("header") {
|
||||||
@ -47,15 +47,12 @@ pub fn search(matches: &ArgMatches) -> Result<()> {
|
|||||||
match matches.value_of("header") {
|
match matches.value_of("header") {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
let p = notes::find_path(s)?;
|
let p = notes::find_path(s)?;
|
||||||
if Confirm::with_theme(&ColorfulTheme::default())
|
match p {
|
||||||
.with_prompt("Do you want to open it?")
|
Some(s) => {
|
||||||
.default(true)
|
|
||||||
.interact()?
|
|
||||||
{
|
|
||||||
let editor = std::env::var("EDITOR")?;
|
let editor = std::env::var("EDITOR")?;
|
||||||
std::process::Command::new(editor).arg(&p).status()?;
|
std::process::Command::new(editor).arg(s).status()?;
|
||||||
} else {
|
}
|
||||||
println!("{}", p);
|
None => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => match matches.is_present("word") {
|
None => match matches.is_present("word") {
|
||||||
|
Loading…
Reference in New Issue
Block a user