add show_all

This commit is contained in:
Volodymyr Patuta 2020-11-29 18:01:25 +01:00
parent 8574cfda80
commit 69261d01cc
6 changed files with 107 additions and 3 deletions

View File

@ -25,6 +25,7 @@ fn main() -> Result<()> {
("remove", Some(m)) => process::remove(m)?, ("remove", Some(m)) => process::remove(m)?,
("edit", Some(m)) => process::edit(m)?, ("edit", Some(m)) => process::edit(m)?,
("list", Some(m)) => process::list(m)?, ("list", Some(m)) => process::list(m)?,
("show", Some(m)) => process::show(m)?,
("search", Some(m)) => process::search(m)?, ("search", Some(m)) => process::search(m)?,
_ => app.print_long_help()?, _ => app.print_long_help()?,
}; };

View File

@ -37,8 +37,7 @@ pub fn make_app() -> App<'static, 'static> {
SubCommand::with_name("list") SubCommand::with_name("list")
.alias("l") .alias("l")
.alias("ls") .alias("ls")
.alias("show") .about("List all notes or one note")
.about("Show all notes or one note")
.arg(Arg::with_name("header").help("Name of the note.")) .arg(Arg::with_name("header").help("Name of the note."))
.arg( .arg(
Arg::with_name("category") Arg::with_name("category")
@ -59,4 +58,14 @@ pub fn make_app() -> App<'static, 'static> {
) )
.arg(Arg::with_name("header").help("Name of the note.")), .arg(Arg::with_name("header").help("Name of the note.")),
) )
.subcommand(
SubCommand::with_name("show")
.arg(
Arg::with_name("all")
.help("Show all notes.")
.short("a")
.long("all"),
)
.arg(Arg::with_name("header").help("Name of the note.")),
)
} }

View File

@ -1,3 +1,4 @@
pub mod app; pub mod app;
mod notes; mod notes;
pub mod process; pub mod process;
pub mod show;

View File

@ -1,7 +1,8 @@
use crate::rnote::show;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use chrono::Utc; use chrono::Utc;
use dialoguer::{theme::ColorfulTheme, Confirm, Select}; use dialoguer::{theme::ColorfulTheme, Confirm, Select};
use std::{env, fs, process::Command}; use std::{env, fs, io::Write, process::Command};
use walkdir::WalkDir; use walkdir::WalkDir;
/// Get the path to the root directory of all notes. /// Get the path to the root directory of all notes.
@ -37,6 +38,8 @@ pub fn create(header: &str, category: &str) -> Result<()> {
let file = format!("{}{}.md", get_path(category)?, header); let file = format!("{}{}.md", get_path(category)?, header);
create_dir(category)?; create_dir(category)?;
is_duplicate(header, category)?; is_duplicate(header, category)?;
let mut f = fs::File::create(&file)?;
f.write(format!("# {}\n", header).as_bytes())?;
Command::new(editor).arg(&file).status()?; Command::new(editor).arg(&file).status()?;
Ok(()) Ok(())
} }
@ -152,3 +155,21 @@ pub fn search_by_word(word: &str) -> Result<()> {
Ok(()) Ok(())
} }
pub fn show_all() -> Result<()> {
let path = get_base_path()?;
let mut files: Vec<String> = Vec::new();
for (_, file) in WalkDir::new(path)
.into_iter()
.filter_map(|file| file.ok())
.enumerate()
{
if file.metadata()?.is_file() {
files.push(fs::read_to_string(file.path())?);
}
}
let skin = show::make_skin();
let md = &files.join("---\n");
show::run_app(skin, md)?;
Ok(())
}

View File

@ -15,6 +15,7 @@ pub fn new(matches: &ArgMatches) -> Result<()> {
notes::create(&header, category)?; notes::create(&header, category)?;
Ok(()) Ok(())
} }
pub fn remove(matches: &ArgMatches) -> Result<()> { pub fn remove(matches: &ArgMatches) -> Result<()> {
let header = match matches.value_of("header") { let header = match matches.value_of("header") {
Some(s) => s.to_owned(), Some(s) => s.to_owned(),
@ -25,6 +26,7 @@ pub fn remove(matches: &ArgMatches) -> Result<()> {
notes::remove(&header)?; notes::remove(&header)?;
Ok(()) Ok(())
} }
pub fn edit(matches: &ArgMatches) -> Result<()> { pub fn edit(matches: &ArgMatches) -> Result<()> {
let header = match matches.value_of("header") { let header = match matches.value_of("header") {
Some(s) => s.to_owned(), Some(s) => s.to_owned(),
@ -36,9 +38,11 @@ pub fn edit(matches: &ArgMatches) -> Result<()> {
notes::modify(&header)?; notes::modify(&header)?;
Ok(()) Ok(())
} }
pub fn list(matches: &ArgMatches) -> Result<()> { pub fn list(matches: &ArgMatches) -> Result<()> {
unimplemented!("list all notes, one note or category {:?}", matches); unimplemented!("list all notes, one note or category {:?}", matches);
} }
pub fn search(matches: &ArgMatches) -> Result<()> { pub fn search(matches: &ArgMatches) -> Result<()> {
match matches.value_of("header") { match matches.value_of("header") {
Some(s) => { Some(s) => {
@ -66,3 +70,14 @@ pub fn search(matches: &ArgMatches) -> Result<()> {
} }
Ok(()) Ok(())
} }
pub fn show(matches: &ArgMatches) -> Result<()> {
match matches.value_of("header") {
Some(s) => unimplemented!("{}", s),
None => match matches.is_present("all") {
true => notes::show_all()?,
false => return Err(anyhow!("No option is given. Abort.")),
},
}
Ok(())
}

57
src/rnote/show.rs Normal file
View File

@ -0,0 +1,57 @@
use crossterm::{
cursor::{Hide, Show},
event::{self, Event, KeyCode::*, KeyEvent},
queue,
style::Color::*,
terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::io::{stderr, Write};
use termimad::*;
pub fn view_area() -> Area {
let mut area = Area::full_screen();
area.pad_for_max_width(120); // we don't want a too wide text column
area
}
pub fn run_app(skin: MadSkin, md: &str) -> Result<()> {
let mut w = stderr(); // we could also have used stdout
queue!(w, EnterAlternateScreen)?;
terminal::enable_raw_mode()?;
queue!(w, Hide)?; // hiding the cursor
let mut view = MadView::from(md.to_owned(), view_area(), skin);
loop {
view.write_on(&mut w)?;
w.flush()?;
match event::read() {
Ok(Event::Key(KeyEvent { code, .. })) => match code {
Up => view.try_scroll_lines(-1),
Down => view.try_scroll_lines(1),
PageUp => view.try_scroll_pages(-1),
PageDown => view.try_scroll_pages(1),
_ => break,
},
Ok(Event::Resize(..)) => {
queue!(w, Clear(ClearType::All))?;
view.resize(&view_area());
}
_ => {}
}
}
terminal::disable_raw_mode()?;
queue!(w, Show)?; // we must restore the cursor
queue!(w, LeaveAlternateScreen)?;
w.flush()?;
Ok(())
}
pub fn make_skin() -> MadSkin {
let mut skin = MadSkin::default();
skin.table.align = Alignment::Center;
skin.set_headers_fg(AnsiValue(178));
skin.bold.set_fg(Yellow);
skin.italic.set_fg(Magenta);
skin.scrollbar.thumb.set_fg(AnsiValue(178));
skin.code_block.align = Alignment::Center;
skin
}