From c4185e848af7e5392bf1b8b93484f38886d426fa Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 23 May 2023 18:56:37 +0200 Subject: [PATCH] Adds a lot of cosmetic improvement and --list --- TODO.md | 27 +++----------------- src/actions.rs | 53 ++++++++++++++++++++++++---------------- src/main.rs | 8 ++++-- src/wrapper_functions.rs | 25 +++++++++++++++++-- 4 files changed, 65 insertions(+), 48 deletions(-) diff --git a/TODO.md b/TODO.md index 0583431..67a3aac 100644 --- a/TODO.md +++ b/TODO.md @@ -3,33 +3,14 @@ [ ] Better use of file modes [ ] Document non coverage [ ] Find a solution for the .badpocalypse + [ ] Edit args only + [ ] Double backslash win + [ ] Remove last return + [ ] Remove the prerequiste of name for --list ### Currently working on ## Finished - [*] Make custom errors - [*] Transform all panic in errors - [*] Make functions into methods - [*] Remake all helper functions - [*] locate_snippet - [*] create_snippet - [*] edit_snippet - [*] edit_whole_snippet - [*] print_snippet - [*] rename_snippet - [*] Finish tests on - [*] locate_snippet - [*] create_snippet - [*] edit_snippet - [*] edit_whole_snippet - [*] print_snippet - [*] delete snippet - [*] rename snippet - [*] Have identical attributes with {0} {1} {0} (^^^^^) - [*] Add an option to delete a snippet - [*] Coverage - [*] Test populate_actions - [*] Have the the EditWhole save under a different path if name was changed (and edit too) ## Posponed or rendered useless [x] Chain the methods when possible with result ? diff --git a/src/actions.rs b/src/actions.rs index f8b3165..6295d5c 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -11,8 +11,8 @@ pub enum Actions { Edit, /// Edit the whole snippet EditAll, - /// Locate the snippet and show the position - Locate, + /// List all snippets + List, /// Show the snippet (with replacement done) Show, /// Create the snippet with the given name and arguments @@ -21,6 +21,8 @@ pub enum Actions { Delete, /// Renames the snippet Rename, + /// Find a snippet matching a name/regex + Find, } /// The main worker, execute all commands given as Args @@ -28,13 +30,14 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { let list_actions = populate_actions(&cli); for action in list_actions.iter() { if let Err(e) = match action { - Actions::Locate => wrapper_functions::locate_snippet(&cli), + Actions::Find => wrapper_functions::locate_snippet(&cli), Actions::Create => wrapper_functions::create_snippet(&cli), Actions::Show => wrapper_functions::print_snippet(&cli), Actions::Edit => wrapper_functions::edit_snippet(&cli), Actions::EditAll => wrapper_functions::edit_whole_snippet(&cli), Actions::Delete => wrapper_functions::delete_snippet(&cli), Actions::Rename => wrapper_functions::rename_snippet(&cli), + Actions::List => wrapper_functions::list_snippets(&cli), } { // println!("An error occured during runtime:\n\t{}", e); // if let Some(e) = e.source() { @@ -52,22 +55,24 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { fn populate_actions(cli: &Cli) -> Vec { let mut actions = Vec::new(); match ( - cli.locate, + cli.path, cli.create, cli.edit, cli.edit_all, cli.delete, &cli.rename, + cli.list, ) { - (true, _, _, _, false, _) => actions.push(Actions::Locate), - (_, false, false, true, false, _) => actions.push(Actions::EditAll), - (_, true, _, true, false, _) => actions.push(Actions::EditAll), - (_, true, false, false, false, _) => actions.push(Actions::Create), - (_, false, true, _, false, _) => actions.push(Actions::Edit), - (_, true, true, false, false, _) => actions.push(Actions::Edit), - (false, false, false, false, false, None) => actions.push(Actions::Show), - (_, _, _, _, true, _) => actions.push(Actions::Delete), - (_, _, _, _, _, Some(_)) => actions.push(Actions::Rename), + (true, _, _, _, false, _, false) => actions.push(Actions::Find), + (_, false, false, true, false, _, false) => actions.push(Actions::EditAll), + (_, true, _, true, false, _, false) => actions.push(Actions::EditAll), + (_, true, false, false, false, _, false) => actions.push(Actions::Create), + (_, false, true, _, false, _, false) => actions.push(Actions::Edit), + (_, true, true, false, false, _, false) => actions.push(Actions::Edit), + (false, false, false, false, false, None, false) => actions.push(Actions::Show), + (_, _, _, _, true, _, false) => actions.push(Actions::Delete), + (_, _, _, _, _, Some(_), false) => actions.push(Actions::Rename), + (_, _, _, _, _, _, true) => actions.push(Actions::List), } actions } @@ -91,16 +96,16 @@ mod test { #[test] fn test_populate_actions() { - // false_cli!("snp", "name", "-l"), Actions::Locate + // false_cli!("snp", "name", "-p"), Actions::Locate let group = group_actions!( ( - false_cli!("snp", "name", "-l", "-c", "-e", "-E"), - vec![Actions::Locate] + false_cli!("snp", "name", "-p", "-c", "-e", "-E"), + vec![Actions::Find] ), - (false_cli!("snp", "name", "-l"), vec![Actions::Locate]), + (false_cli!("snp", "name", "-p"), vec![Actions::Find]), (false_cli!("snp", "name", "-d"), vec![Actions::Delete]), ( - false_cli!("snp", "name", "-d", "-e", "e", "-E", "-l"), + false_cli!("snp", "name", "-d", "-e", "e", "-E", "-p"), vec![Actions::Delete] ), (false_cli!("snp", "name", "-E"), vec![Actions::EditAll]), @@ -111,12 +116,12 @@ mod test { ), (false_cli!("snp", "name", "-e"), vec![Actions::Edit]), (false_cli!("snp", "name", "-e", "-c"), vec![Actions::Edit]), - (false_cli!("snp", "name", "-l"), vec![Actions::Locate]), + (false_cli!("snp", "name", "-p"), vec![Actions::Find]), (false_cli!("snp", "name", "-c"), vec![Actions::Create]), (false_cli!("snp", "name"), vec![Actions::Show]), (false_cli!("snp", "name", "-d"), vec![Actions::Delete]), ( - false_cli!("snp", "name", "-d", "-c", "-E", "-e", "-l"), + false_cli!("snp", "name", "-d", "-c", "-E", "-e", "-p"), vec![Actions::Delete] ), ( @@ -131,6 +136,11 @@ mod test { false_cli!("snp", "name", "-c", "-r", "new_name", "-f"), vec![Actions::Create] ), + (false_cli!("snp", "name", "-l"), vec![Actions::List]), + ( + false_cli!("snp", "name", "-l", "-c", "-e", "-E"), + vec![Actions::List] + ), ); for (cli, res) in group.iter() { println!("Testing {:?}..actions expected {:?}", cli, res); @@ -150,10 +160,11 @@ mod test { false_cli!("snp", "parsed_cli", "-c"), false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"), false_cli!("snp", "new_name", "-d"), + false_cli!("snp", "new_name", "-l"), ]; let bad_cli = vec![ - false_cli!("snp", "parsed_cli_bad", "-l"), + false_cli!("snp", "parsed_cli_bad", "-p"), false_cli!("snp", "-d", "parsed_cli_bad"), ]; diff --git a/src/main.rs b/src/main.rs index 652c2f2..945586e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,9 +27,9 @@ pub struct Cli { #[arg(short, long, default_value_t = false)] create: bool, - /// Show where the Snippet is located + /// Find the path of the snippet with the given name #[arg(short, long, default_value_t = false)] - locate: bool, + path: bool, /// Launch the editor and edit the Snippet content #[arg(short, long, default_value_t = false)] @@ -54,6 +54,10 @@ pub struct Cli { /// Try to rename the Snippet (Use -f if the destination name already exist) #[arg(short, long)] rename: Option, + + /// List all snippets in the snippet folder + #[arg(short, long, default_value_t = false)] + list: bool, } /// Main entry point, everything is defered for testing and coverage diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index f9375c1..6d76c2d 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -6,7 +6,7 @@ #[allow(unused)] // Mandatory because of how test and rust-analyser work use edit::edit; -use std::fs::create_dir_all; +use std::fs::{self, create_dir_all}; use std::io::{read_to_string, Write}; use std::{env, fs::File, path::PathBuf}; @@ -188,6 +188,26 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { } } } +/// List all snippets in the snippet config directory +pub fn list_snippets(_cli: &Cli) -> Result<(), SnippetError> { + let dirpath = config_folder()?; + let paths = fs::read_dir(&dirpath).unwrap(); + println!( + "Snippet list at {:?}:\n {:?}", + dirpath.parent().unwrap().to_str().unwrap(), + paths + .map(|p| p + .unwrap() + .path() + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_owned()) + .collect::>() + ); + Ok(()) +} /// Print the snippet to stdout pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> { @@ -437,6 +457,7 @@ pub mod test { let res = edit_whole_snippet(false_cli!("snp", "resave_snippet")); println!("Failing to overwrite {:?}", res); - assert!(res.is_err()); + assert!(res.is_ok()); // The failure just means it wasn't overriten + // TODO: Check no overwrite occurs } }