summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Geary <rtgnj42@gmail.com>2019-10-13 22:00:15 -0400
committerRyan Geary <rtgnj42@gmail.com>2019-10-13 22:00:15 -0400
commitf662219407e21abfb658b73a1e97ea0dc3908fe3 (patch)
treeb3ede16c1334d0a2ce1ee90f5a7bd8f236860eeb
parent0be16bf1d99af669497f7125d9e3eb6b8fb09fad (diff)
[BREAKING] Default to inclusive ranges, -x for exclusive
-rw-r--r--src/choice.rs42
-rw-r--r--src/config.rs6
-rw-r--r--test/choose_0:1.txt (renamed from test/choose_0:2.txt)0
-rw-r--r--test/choose_4:2.txt6
-rw-r--r--test/choose_:1.txt (renamed from test/choose_:2.txt)0
-rwxr-xr-xtest/e2e_test.sh6
6 files changed, 33 insertions, 27 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)]
diff --git a/test/choose_0:2.txt b/test/choose_0:1.txt
index 38b492a..38b492a 100644
--- a/test/choose_0:2.txt
+++ b/test/choose_0:1.txt
diff --git a/test/choose_4:2.txt b/test/choose_4:2.txt
new file mode 100644
index 0000000..e441054
--- /dev/null
+++ b/test/choose_4:2.txt
@@ -0,0 +1,6 @@
+amet, sit dolor
+dolore et labore
+nisi laboris ullamco
+in dolor irure
+sint Excepteur pariatur.
+mollit deserunt officia
diff --git a/test/choose_:2.txt b/test/choose_:1.txt
index 38b492a..38b492a 100644
--- a/test/choose_:2.txt
+++ b/test/choose_:1.txt
diff --git a/test/e2e_test.sh b/test/e2e_test.sh
index a76b8a2..95207f8 100755
--- a/test/e2e_test.sh
+++ b/test/e2e_test.sh
@@ -7,13 +7,13 @@ cd "$(git rev-parse --show-toplevel)"
cargo build
# basic functionality
-diff -w <(cargo run -- 0:2 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_0:2.txt")
+diff -w <(cargo run -- 0:1 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_0:1.txt")
diff -w <(cargo run -- 0 3 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_0_3.txt")
-diff -w <(cargo run -- :2 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_:2.txt")
+diff -w <(cargo run -- :1 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_:1.txt")
diff -w <(cargo run -- 9 3 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_9_3.txt")
diff -w <(cargo run -- 9 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_9.txt")
diff -w <(cargo run -- 12 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_12.txt")
-diff -w <(cargo run -- 4:1 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_4:1.txt")
+diff -w <(cargo run -- 4:2 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_4:2.txt")
# add tests for different delimiters
# add tests using piping