mirror of
https://github.com/sharkdp/bat
synced 2024-11-27 14:20:45 +00:00
Support for line range minus syntax
This commit is contained in:
parent
d2175d6382
commit
bc86c29185
1 changed files with 30 additions and 1 deletions
|
@ -47,12 +47,24 @@ impl LineRange {
|
|||
}
|
||||
2 => {
|
||||
new_range.lower = line_numbers[0].parse()?;
|
||||
let first_byte = line_numbers[1].bytes().next();
|
||||
|
||||
new_range.upper = if line_numbers[1].bytes().next() == Some(b'+') {
|
||||
new_range.upper = if first_byte == Some(b'+') {
|
||||
let more_lines = &line_numbers[1][1..]
|
||||
.parse()
|
||||
.map_err(|_| "Invalid character after +")?;
|
||||
new_range.lower + more_lines
|
||||
} else if first_byte == Some(b'-') {
|
||||
// this will prevent values like "-+5" even though "+5" is valid integer
|
||||
if &line_numbers[1][1..].bytes().next() == &Some(b'+') {
|
||||
return Err("Invalid character after -".into());
|
||||
}
|
||||
let less_lines = &line_numbers[1][1..]
|
||||
.parse()
|
||||
.map_err(|_| "Invalid character after -")?;
|
||||
let prev_lower = new_range.lower;
|
||||
new_range.lower = new_range.lower - less_lines;
|
||||
prev_lower
|
||||
} else {
|
||||
line_numbers[1].parse()?
|
||||
};
|
||||
|
@ -126,6 +138,23 @@ fn test_parse_plus_fail() {
|
|||
assert!(range.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_minus_success() {
|
||||
let range = LineRange::from("40:-10").expect("Shouldn't fail on test!");
|
||||
assert_eq!(30, range.lower);
|
||||
assert_eq!(40, range.upper);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_minus_fail() {
|
||||
let range = LineRange::from("40:-z");
|
||||
assert!(range.is_err());
|
||||
let range = LineRange::from("40:-+10");
|
||||
assert!(range.is_err());
|
||||
let range = LineRange::from("40:-");
|
||||
assert!(range.is_err());
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub enum RangeCheckResult {
|
||||
// Within one of the given ranges
|
||||
|
|
Loading…
Reference in a new issue