From 69261d01ccb9236032cd600f3295d6f3deecebd8 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sun, 29 Nov 2020 18:01:25 +0100 Subject: [PATCH] add show_all --- src/main.rs | 1 + src/rnote/app.rs | 13 ++++++++-- src/rnote/mod.rs | 1 + src/rnote/notes.rs | 23 +++++++++++++++++- src/rnote/process.rs | 15 ++++++++++++ src/rnote/show.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/rnote/show.rs diff --git a/src/main.rs b/src/main.rs index bdfcc73..a4e4f29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ fn main() -> Result<()> { ("remove", Some(m)) => process::remove(m)?, ("edit", Some(m)) => process::edit(m)?, ("list", Some(m)) => process::list(m)?, + ("show", Some(m)) => process::show(m)?, ("search", Some(m)) => process::search(m)?, _ => app.print_long_help()?, }; diff --git a/src/rnote/app.rs b/src/rnote/app.rs index 331dfd7..ffa3b41 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -37,8 +37,7 @@ pub fn make_app() -> App<'static, 'static> { SubCommand::with_name("list") .alias("l") .alias("ls") - .alias("show") - .about("Show all notes or one note") + .about("List all notes or one note") .arg(Arg::with_name("header").help("Name of the note.")) .arg( 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.")), ) + .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.")), + ) } diff --git a/src/rnote/mod.rs b/src/rnote/mod.rs index 6f082da..806ea61 100644 --- a/src/rnote/mod.rs +++ b/src/rnote/mod.rs @@ -1,3 +1,4 @@ pub mod app; mod notes; pub mod process; +pub mod show; diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index cfa8f58..c613a26 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -1,7 +1,8 @@ +use crate::rnote::show; use anyhow::{anyhow, Result}; use chrono::Utc; use dialoguer::{theme::ColorfulTheme, Confirm, Select}; -use std::{env, fs, process::Command}; +use std::{env, fs, io::Write, process::Command}; use walkdir::WalkDir; /// 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); create_dir(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()?; Ok(()) } @@ -152,3 +155,21 @@ pub fn search_by_word(word: &str) -> Result<()> { Ok(()) } + +pub fn show_all() -> Result<()> { + let path = get_base_path()?; + let mut files: Vec = 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(()) +} diff --git a/src/rnote/process.rs b/src/rnote/process.rs index 32224e4..ffd6dd8 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -15,6 +15,7 @@ pub fn new(matches: &ArgMatches) -> Result<()> { notes::create(&header, category)?; Ok(()) } + pub fn remove(matches: &ArgMatches) -> Result<()> { let header = match matches.value_of("header") { Some(s) => s.to_owned(), @@ -25,6 +26,7 @@ pub fn remove(matches: &ArgMatches) -> Result<()> { notes::remove(&header)?; Ok(()) } + pub fn edit(matches: &ArgMatches) -> Result<()> { let header = match matches.value_of("header") { Some(s) => s.to_owned(), @@ -36,9 +38,11 @@ pub fn edit(matches: &ArgMatches) -> Result<()> { notes::modify(&header)?; Ok(()) } + pub fn list(matches: &ArgMatches) -> Result<()> { unimplemented!("list all notes, one note or category {:?}", matches); } + pub fn search(matches: &ArgMatches) -> Result<()> { match matches.value_of("header") { Some(s) => { @@ -66,3 +70,14 @@ pub fn search(matches: &ArgMatches) -> Result<()> { } 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(()) +} diff --git a/src/rnote/show.rs b/src/rnote/show.rs new file mode 100644 index 0000000..40e991c --- /dev/null +++ b/src/rnote/show.rs @@ -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 +}