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..764de9e 100644 --- a/src/simil.rs +++ b/src/simil.rs @@ -93,8 +93,14 @@ pub fn similarities( } // end ignore starts with if text1 == text2 { - if fl1 == fl1_last_found + 1 || fl2 == fl2_last_found + 1 { - println!("... {}", text1); + // 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 { + println!("... {}", text1); + } } else { println!( "\n{4}{5}{0}{2} {3}({fl1}){2}\n{4}{5}{1}{2} {3}({fl2}){2}", @@ -105,7 +111,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..b55888a 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", + "--ignore-empty", + "--trim", + ]; + +const USAGE_STR: &str = " +Usage: simil [-h] [--abspath] [--ignore-config [[--ignore-empty] [--trim]] 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 + + --ignore-empty Omit empty lines in output + + --trim Trim whitespace + +"; + // Top level struct to hold the TOML data. #[derive(Debug)] @@ -37,11 +60,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"); @@ -136,11 +171,8 @@ pub fn check_args(args: Vec) -> Args { }; // check options provided - let accepted_options = vec![ - "--abspath" - ]; 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); @@ -150,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);