diff options
author | Ryan Geary <rtgnj42@gmail.com> | 2019-10-13 22:00:15 -0400 |
---|---|---|
committer | Ryan Geary <rtgnj42@gmail.com> | 2019-10-13 22:00:15 -0400 |
commit | f662219407e21abfb658b73a1e97ea0dc3908fe3 (patch) | |
tree | b3ede16c1334d0a2ce1ee90f5a7bd8f236860eeb /src | |
parent | 0be16bf1d99af669497f7125d9e3eb6b8fb09fad (diff) |
[BREAKING] Default to inclusive ranges, -x for exclusive
Diffstat (limited to 'src')
-rw-r--r-- | src/choice.rs | 42 | ||||
-rw-r--r-- | src/config.rs | 6 |
2 files changed, 24 insertions, 24 deletions
diff --git a/src/choice.rs b/src/choice.rs index 5ee25d1..2fb0974 100644 --- a/src/choice.rs +++ b/src/choice.rs @@ -51,27 +51,27 @@ impl Choice { .map(|x| x.1) .collect::<Vec<&str>>(), (None, Some(end)) => { - let e: usize = if config.opt.inclusive { - (end + 1).try_into().unwrap() + let e: usize = if config.opt.exclusive { + (end - 1).try_into().unwrap() } else { (*end).try_into().unwrap() }; words - .filter(|x| x.0 < e) + .filter(|x| x.0 <= e) .map(|x| x.1) .collect::<Vec<&str>>() } (Some(start), Some(end)) => { - let e: usize = if config.opt.inclusive { - (end + 1).try_into().unwrap() + let e: usize = if config.opt.exclusive { + (end - 1).try_into().unwrap() } else { (*end).try_into().unwrap() }; words .filter(|x| { - (x.0 < e && x.0 >= (*start).try_into().unwrap()) + (x.0 <= e && x.0 >= (*start).try_into().unwrap()) || self.is_reverse_range() - && (x.0 > e && x.0 <= (*start).try_into().unwrap()) + && (x.0 >= e && x.0 <= (*start).try_into().unwrap()) }) .map(|x| x.1) .collect::<Vec<&str>>() @@ -144,8 +144,8 @@ mod tests { } #[test] - fn print_1_to_3() { - let config = Config::from_iter(vec!["choose", "1:3"]); + fn print_1_to_3_exclusive() { + let config = Config::from_iter(vec!["choose", "1:3", "-x"]); assert_eq!( vec!["is", "pretty"], config.opt.choice[0] @@ -154,8 +154,8 @@ mod tests { } #[test] - fn print_1_to_3_inclusive() { - let config = Config::from_iter(vec!["choose", "1:3", "-n"]); + fn print_1_to_3() { + let config = Config::from_iter(vec!["choose", "1:3" ]); assert_eq!( vec!["is", "pretty", "cool"], config.opt.choice[0] @@ -167,15 +167,15 @@ mod tests { fn print_1_to_3_separated_by_hashtag() { let config = Config::from_iter(vec!["choose", "1:3", "-f", "#"]); assert_eq!( - vec!["is", "pretty"], + vec!["is", "pretty", "cool"], config.opt.choice[0] .get_choice_slice(&String::from("rust#is#pretty#cool"), &config) ); } #[test] - fn print_1_to_3_separated_by_varying_multiple_hashtag() { - let config = Config::from_iter(vec!["choose", "1:3", "-f", "#"]); + fn print_1_to_3_separated_by_varying_multiple_hashtag_exclusive() { + let config = Config::from_iter(vec!["choose", "1:3", "-f", "#", "-x"]); assert_eq!( vec!["is", "pretty"], config.opt.choice[0] @@ -184,8 +184,8 @@ mod tests { } #[test] - fn print_1_to_3_separated_by_varying_multiple_hashtag_inclusive() { - let config = Config::from_iter(vec!["choose", "1:3", "-f", "#", "-n"]); + fn print_1_to_3_separated_by_varying_multiple_hashtag() { + let config = Config::from_iter(vec!["choose", "1:3", "-f", "#"]); assert_eq!( vec!["is", "pretty", "cool"], config.opt.choice[0] @@ -194,8 +194,8 @@ mod tests { } #[test] - fn print_1_to_3_separated_by_regex_group_vowels() { - let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]"]); + fn print_1_to_3_separated_by_regex_group_vowels_exclusive() { + let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]", "-x"]); assert_eq!( vec![" q", "ck br"], config.opt.choice[0].get_choice_slice( @@ -206,8 +206,8 @@ mod tests { } #[test] - fn print_1_to_3_separated_by_regex_group_vowels_inclusive() { - let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]", "-n"]); + fn print_1_to_3_separated_by_regex_group_vowels() { + let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]"]); assert_eq!( vec![" q", "ck br", "wn f"], config.opt.choice[0].get_choice_slice( @@ -221,7 +221,7 @@ mod tests { fn print_3_to_1() { let config = Config::from_iter(vec!["choose", "3:1"]); assert_eq!( - vec!["pretty", "is"], + vec!["pretty", "is", "lang"], config.opt.choice[0].get_choice_slice( &String::from("rust lang is pretty darn cool"), &config diff --git a/src/config.rs b/src/config.rs index 5ee68b6..8dfba5e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,9 +13,9 @@ pub struct Opt { #[structopt(short, long)] pub field_separator: Option<String>, - /// Use inclusive ranges - #[structopt(short = "n", long)] - pub inclusive: bool, + /// Use exclusive ranges, similar to array slicing in many programming languages + #[structopt(short = "x", long)] + pub exclusive: bool, /// Activate debug mode #[structopt(short, long)] |