From b628bf558bae8a782aa540f7a6bfc5c16fc20030 Mon Sep 17 00:00:00 2001 From: Philippe Braum Date: Tue, 26 Sep 2023 21:26:45 +0200 Subject: [PATCH 1/4] closing #5, more cl options --- Cargo.toml | 4 ++-- src/main.rs | 8 +------- src/simil.rs | 12 ++++++++++-- src/utils.rs | 19 +++++++++++++++++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b0a9e81..3346254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "simil" -version = "0.1.0-alpha.2" +version = "0.1.0-alpha.3" edition = "2021" -authors = ["Philippe Braum "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index 99549fb..ea6eae6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,24 +4,18 @@ use simil::similarities; mod utils; use std::env; -// Ignore -// Ignore-starts-with - fn main() { // command line args let args = utils::check_args(env::args().collect()); //dbg!(&args); - let data = utils::parse_toml(); + let data = utils::parse_toml(&args.options); //dbg!(&data); // construct paths to files to analyze from args let (path1, path2) = utils::construct_filepaths(&args); //dbg!(&path1); //dbg!(&path2); - - //let path1 = "/Users/pbr/PycharmProjects/bids_programming/data_science_script.py"; - //let path2 = "/Users/pbr/PycharmProjects/bids_programming/exercises_script.py"; similarities(Path::new(&path1), Path::new(&path2), &data.config); } \ No newline at end of file diff --git a/src/simil.rs b/src/simil.rs index c937efc..598ecff 100644 --- a/src/simil.rs +++ b/src/simil.rs @@ -94,7 +94,11 @@ pub fn similarities( // end ignore starts with if text1 == text2 { if fl1 == fl1_last_found + 1 || fl2 == fl2_last_found + 1 { - println!("... {}", text1); + if text1.is_empty() { + println!("... empty"); + } else { + println!("... {}", text1); + } } else { println!( "\n{4}{5}{0}{2} {3}({fl1}){2}\n{4}{5}{1}{2} {3}({fl2}){2}", @@ -105,7 +109,11 @@ pub fn similarities( utils::TEXTMODE_DIM, utils::TEXTMODE_UNDERLINE, ); - println!(">>> {}", text1); + if text1.is_empty() { + println!(">>> empty"); + } else { + println!(">>> {}", text1); + } } n_found += 1; fl1_last_found = fl1; diff --git a/src/utils.rs b/src/utils.rs index 8ad218d..2c2fa16 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -37,11 +37,23 @@ pub struct Config { const VERSION: &str = env!("CARGO_PKG_VERSION"); -pub fn parse_toml() -> Data { +pub fn parse_toml(args_options: &Vec) -> Data { /* Parsing the toml config and returning the Data struct. Only finds the config if in the same dir as the executable. */ + // check for flag to ignore config + if args_options.contains(&"--ignore-config".to_string()) { + // return empty Config struct + return Data { + config: Config { + ignore: if args_options.contains(&"--ignore-empty".to_string()) {vec!["".to_string()]} else {vec![]}, + ignore_beginning: vec![], + trim_whitespace: if args_options.contains(&"--trim".to_string()) {true} else {false}, + } + } + } + // search for toml in exe dir first let mut path: PathBuf = env::current_exe().unwrap().parent().unwrap().into(); let toml_filename = Path::new("simil.toml"); @@ -137,7 +149,10 @@ pub fn check_args(args: Vec) -> Args { // check options provided let accepted_options = vec![ - "--abspath" + "--abspath", + "--ignore-config", + "--trim", + "--ignore-empty", ]; for option in &args.options { if !accepted_options.iter().any(|&i| i == option) { From c2d5b50b7109d24865ffa08d70d8976b8ab03d05 Mon Sep 17 00:00:00 2001 From: Philippe Braum Date: Tue, 26 Sep 2023 22:02:37 +0200 Subject: [PATCH 2/4] bugfix: first line match prints as subsequent, refactoring --- src/simil.rs | 4 +++- src/utils.rs | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/simil.rs b/src/simil.rs index 598ecff..764de9e 100644 --- a/src/simil.rs +++ b/src/simil.rs @@ -93,7 +93,9 @@ pub fn similarities( } // end ignore starts with if text1 == text2 { - if fl1 == fl1_last_found + 1 || fl2 == fl2_last_found + 1 { + // print subsequent finds formatted with "..." + // ignore for first line + if (fl1 == fl1_last_found + 1 || fl2 == fl2_last_found + 1) && !(fl1_last_found == 0 || fl2_last_found == 0) { if text1.is_empty() { println!("... empty"); } else { diff --git a/src/utils.rs b/src/utils.rs index 2c2fa16..f86977e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,6 +18,29 @@ pub const TEXTMODE_UNDERLINE: &'static str = "\x1B[4m"; // reset styles pub const RESET_STYLES: &'static str = "\x1B[0m"; +const ACCEPTED_OPTIONS: [&'static str; 4] = [ + "--abspath", + "--ignore-config", + "--trim", + "--ignore-empty", + ]; + +const USAGE_STR: &str = " +Usage: simil [-h] [--abspath] file1 file2 + +positional arguments: + file + +options: + -h, --help Show this help message and exit + -v, --version Show version number and exit + --abspath Using absolute filepaths (relative to cwd by default) + --ignore-config Do not use simil.toml config + --trim Trim whitespace + --ignore-empty Omit empty lines in output + +"; + // Top level struct to hold the TOML data. #[derive(Debug)] @@ -148,14 +171,8 @@ pub fn check_args(args: Vec) -> Args { }; // check options provided - let accepted_options = vec![ - "--abspath", - "--ignore-config", - "--trim", - "--ignore-empty", - ]; for option in &args.options { - if !accepted_options.iter().any(|&i| i == option) { + if !ACCEPTED_OPTIONS.iter().any(|i| i == option) { // option not recognized eprintln!("{0}error:{2} unexpected argument {1}'{3}'{2} found", COLOR_RED, COLOR_YELLOW, RESET_STYLES, option); print_usage(true); @@ -165,20 +182,6 @@ pub fn check_args(args: Vec) -> Args { return args; } - -const USAGE_STR: &str = " -Usage: simil [-h] [--abspath] file1 file2 - -positional arguments: - file - -options: - -h, --help Show this help message and exit - -v, --version Show version number and exit - --abspath Using absolute filepaths (relative to cwd by default) - -"; - pub fn print_usage(as_error: bool) { if as_error { eprint!("{}", USAGE_STR); From 5540da8b225fdc583adb7eb22b699ccb6e029b66 Mon Sep 17 00:00:00 2001 From: Philippe Braum Date: Tue, 26 Sep 2023 22:08:15 +0200 Subject: [PATCH 3/4] refactor --- src/utils.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index f86977e..5ee0bf3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,8 +21,8 @@ pub const RESET_STYLES: &'static str = "\x1B[0m"; const ACCEPTED_OPTIONS: [&'static str; 4] = [ "--abspath", "--ignore-config", - "--trim", "--ignore-empty", + "--trim", ]; const USAGE_STR: &str = " @@ -36,8 +36,8 @@ options: -v, --version Show version number and exit --abspath Using absolute filepaths (relative to cwd by default) --ignore-config Do not use simil.toml config - --trim Trim whitespace - --ignore-empty Omit empty lines in output + + --ignore-empty Omit empty lines in output + + --trim Trim whitespace "; From be2eef090e819138f281c8cfb3b03641b7dec863 Mon Sep 17 00:00:00 2001 From: Philippe Braum Date: Tue, 26 Sep 2023 22:13:03 +0200 Subject: [PATCH 4/4] usage help --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 5ee0bf3..b55888a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -26,7 +26,7 @@ const ACCEPTED_OPTIONS: [&'static str; 4] = [ ]; const USAGE_STR: &str = " -Usage: simil [-h] [--abspath] file1 file2 +Usage: simil [-h] [--abspath] [--ignore-config [[--ignore-empty] [--trim]] file1 file2 positional arguments: file