Skip to content

Commit

Permalink
Merge pull request #19 from Criomby/revert-18-main
Browse files Browse the repository at this point in the history
Revert "Main"
  • Loading branch information
Criomby authored Oct 20, 2023
2 parents 536c311 + f64578c commit 7b04c99
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[target.x86_64-pc-windows-gnu]
# installed mingw-w64 with homebrew

[alias]
r = "run"
b = "build"
br = "build --release"
#win = "--target=x86_64-pc-windows-gnu"
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ but `simil` allows you to configure the comparison extensively for more accurate
### command line args

```
Usage: simil [-h] [--abspath] [--noconf [[--ignore-empty] [--trim]] file1 file2
Usage: simil [--abspath] [--noconf [[--ignore-empty] [--trim]] [...] file1 file2
positional arguments:
file
Expand All @@ -25,6 +25,8 @@ 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)
--global-conf Use global config (ignore local)
--local-conf Use local config (ignore global)
--noconf Do not use simil.toml config
+ --ignore-empty Omit empty lines in output
+ --trim Trim whitespace
Expand All @@ -33,8 +35,12 @@ options:
- `--abspath`<br>
This flag allows you to provide absolute paths to the files you want to analyze.<br>
By default, the filename arguments provided will be treated as relative paths (relative to the current working directory).
- `--global-conf`<br>
Use the global config (in exe dir), ignoring the local config file (cwd or parents)
- `--local-conf`<br>
Use the local config (in cwd or any parent dir), ignoring the global config (in exe dir)
- `--noconf`<br>
Ignore any simil.toml file and configure simil from the command line (or use to compare lines as-is)
Ignore any simil.toml file and configure simil from the command line (or use to compare files as-is)
- `--ignore-empty`<br>
Ignore empty lines in output
- `--trim`<br>
Expand All @@ -56,7 +62,8 @@ You have two options setting a simil.toml:
2. Place toml file in the project dir (cwd, dir from where you will invoke simil from)<br>
or any parent dir to set a config on a *per-project (per-dir & sub-dirs)* basis.

If you set both, the global config file (in the exe dir) will be used.
If you set both, the global config file (in the exe dir) will be used<br>
OR you can set which config to use if both exist with `--global-conf` or `--local-conf`.

**Structure:**<br>

Expand Down
87 changes: 63 additions & 24 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@ pub const TEXTMODE_UNDERLINE: &'static str = "\x1B[4m";
// reset styles
pub const RESET_STYLES: &'static str = "\x1B[0m";

const ACCEPTED_OPTIONS: [&'static str; 4] = [
const ACCEPTED_OPTIONS: [&'static str; 6] = [
"--abspath",
"--global-conf",
"--local-conf",
"--noconf",
"--ignore-empty",
"--trim",
];

const USAGE_STR: &str = "
Usage: simil [-h] [--abspath] [--noconf [[--ignore-empty] [--trim]] file1 file2
Usage: simil [--abspath] [--noconf [[--ignore-empty] [--trim]] [...] file1 file2
positional arguments:
file
options:
-h, --help Show this help message and exit
-v, --version Show version number and exit
-V, --version Show version number and exit
--abspath Using absolute filepaths (relative to cwd by default)
--noconf Do not use simil.toml config
--global-conf Use global config (ignore local)
--local-conf Use local config (ignore global)
--noconf Do not use any simil.toml config
+ --ignore-empty Omit empty lines in output
+ --trim Trim whitespace
Expand Down Expand Up @@ -77,34 +81,64 @@ pub fn parse_toml(args_options: &Vec<String>) -> Data {
}
}

// search for toml in exe dir first
let mut path: PathBuf = env::current_exe().unwrap().parent().unwrap().into();
let mut path: PathBuf;
let toml_filename = Path::new("simil.toml");
path.push(toml_filename);
if !path.is_file() {
// if file not in exe dir,
// search cwd and parent dirs

// check options for config
if args_options.contains(&"--global-conf".to_string()) {
// --global-conf (only search for global file)
path = env::current_exe().unwrap().parent().unwrap().into();
path.push(toml_filename);
if !path.is_file() {
eprintln!("{COLOR_RED}error:{RESET_STYLES} no file {COLOR_YELLOW}'simil.toml'{RESET_STYLES} in exe dir found
Consider removing {COLOR_YELLOW}'--global-config'{RESET_STYLES} or add a config file to the exe dir");
exit(1);
}
} else if args_options.contains(&"--local-conf".to_string()) {
// --local-conf (only search for local file)
path = env::current_dir().unwrap();
// search cwd and preant dirs
loop {
path.push(toml_filename);
if path.is_file() {
break;
}
if !(path.pop() && path.pop()) {
eprintln!("{COLOR_RED}error:{RESET_STYLES} no file {COLOR_YELLOW}'simil.toml'{RESET_STYLES} in exe dir or cwd & parent dirs found
Create a simil.toml file in the exe dir for global settings,
or the cwd or project (incl. any parent) dir for project-specific setttings.");
eprintln!("{COLOR_RED}error:{RESET_STYLES} no file {COLOR_YELLOW}'simil.toml'{RESET_STYLES} in cwd or parent dirs found
Consider removing {COLOR_YELLOW}'--local-config'{RESET_STYLES} or add a config file to the project (or any parent) dir");
exit(1);
}
}
} else {
// first, search for toml in exe dir
path = env::current_exe().unwrap().parent().unwrap().into();
path.push(toml_filename);
if !path.is_file() {
// if file not in exe dir,
// search cwd and parent dirs
path = env::current_dir().unwrap();
loop {
path.push(toml_filename);
if path.is_file() {
break;
}
if !(path.pop() && path.pop()) {
eprintln!("{COLOR_RED}error:{RESET_STYLES} no file {COLOR_YELLOW}'simil.toml'{RESET_STYLES} found
Create a simil.toml file in the exe dir for global settings,
or the cwd or project (incl. any parent) dir for project-specific setttings.
For additional options, see documentation.");
exit(1);
}
}
}
}
//dbg!(&path);

// parse toml contents
let contents = match fs::read_to_string(path) {
Ok(c) => c,
Err(e) => {
eprintln!("{COLOR_RED}error:{RESET_STYLES} could not find simil.toml in executable directory\n{e}");
eprintln!("{COLOR_RED}error:{RESET_STYLES} could not read simil.toml\n{e}");
exit(1);
}
};
Expand Down Expand Up @@ -137,20 +171,11 @@ pub fn check_args(args: Vec<String>) -> Args {
if args.contains(&"-h".to_string()) || args.contains(&"--help".to_string()) {
print_usage(false);
exit(0);
} else if args.contains(&"-v".to_string()) || args.contains(&"--version".to_string()) {
} else if args.contains(&"-V".to_string()) || args.contains(&"--version".to_string()) {
println!("simil {}", VERSION);
exit(0);
}

// check n args
// requires min 2 args (the filenames)
// (first arg is always the path with which the exe was invoked with)
if args.len() < 3 {
eprintln!("{}error:{} missing required positional argument(s)", COLOR_RED, RESET_STYLES);
print_usage(true);
exit(1);
}

// extract positional args and optional args
// positional will include the relative exe path at index 0
let mut positional: Vec<String> = vec![];
Expand All @@ -164,6 +189,20 @@ pub fn check_args(args: Vec<String>) -> Args {
positional.push(arg.to_string());
}
}

// check n args
// requires exactly 2 positional args (the filenames)
// first arg (index 0) is always the path with which the exe was invoked with
if positional.len() != 3 {
if positional.len() < 3 {
eprintln!("{}error:{} missing required positional argument(s)", COLOR_RED, RESET_STYLES);
} else {
eprintln!("{}error:{} too many arguments", COLOR_RED, RESET_STYLES);
}
print_usage(true);
exit(1);
}

let args = Args {
filepath1: positional[1].to_string(),
filepath2: positional[2].to_string(),
Expand Down

0 comments on commit 7b04c99

Please sign in to comment.