Skip to content

Commit

Permalink
Merge pull request #6 from Criomby/5-flag-to-ignore-config
Browse files Browse the repository at this point in the history
5 flag to ignore config
  • Loading branch information
Criomby authored Sep 26, 2023
2 parents 9cf8ea0 + be2eef0 commit 5980a2e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "simil"
version = "0.1.0-alpha.2"
version = "0.1.0-alpha.3"
edition = "2021"
authors = ["Philippe Braum <[email protected]"]
authors = ["Philippe Braum <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
8 changes: 1 addition & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
16 changes: 13 additions & 3 deletions src/simil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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;
Expand Down
56 changes: 37 additions & 19 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<String>) -> 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");
Expand Down Expand Up @@ -136,11 +171,8 @@ pub fn check_args(args: Vec<String>) -> 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);
Expand All @@ -150,20 +182,6 @@ pub fn check_args(args: Vec<String>) -> 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);
Expand Down

0 comments on commit 5980a2e

Please sign in to comment.