on master help sign in
Support for line range plus syntax
commit07487834044a1e24247d4c1631447419fdd463d2 authorBojan Durdevic <bojan88@gmail.com> dateSat 21 Aug 2021, 13:55 (5 years ago) parentb3247d9
4 files changed+36−3
CHANGELOG.md+1 assets/manual/bat.1.in+4 src/bin/bat/clap_app.rs+4−2 src/line_range.rs+27−1
CHANGELOG.md+1
@@ -3,6 +3,7 @@
33## Features
44
55- `$BAT_CONFIG_DIR` is now a recognized environment variable. It has precedence over `$XDG_CONFIG_HOME`, see #1727 (@billrisher)
6+- Support for `x:+x` syntax in line ranges (e.g. `20:+10`). See #1810 (@bojan88)
67
78## Bugfixes
89
assets/manual/bat.1.in+4
@@ -50,6 +50,8 @@ highlights lines 30 to 40
5050highlights lines 1 to 40
5151.IP "\-\-highlight\-line 40:"
5252highlights lines 40 to the end of the file
53+.IP "\-\-highlight\-line 30:+10"
54+highlights lines 30 to 40
5355.RE
5456.HP
5557\fB\-\-file\-name\fR <name>...
@@ -154,6 +156,8 @@ prints lines 30 to 40
154156prints lines 1 to 40
155157.IP "\-\-line\-range 40:"
156158prints lines 40 to the end of the file
159+.IP "\-\-line\-range 30:+10"
160+prints lines 30 to 40
157161.RE
158162.HP
159163\fB\-L\fR, \fB\-\-list\-languages\fR
src/bin/bat/clap_app.rs+4−2
@@ -95,7 +95,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
9595 '--highlight-line 40' highlights line 40\n \
9696 '--highlight-line 30:40' highlights lines 30 to 40\n \
9797 '--highlight-line :40' highlights lines 1 to 40\n \
98− '--highlight-line 40:' highlights lines 40 to the end of the file",
98+ '--highlight-line 40:' highlights lines 40 to the end of the file\n \
99+ '--highlight-line 30:+10' highlights lines 30 to 40",
99100 ),
100101 )
101102 .arg(
@@ -423,7 +424,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
423424 '--line-range 30:40' prints lines 30 to 40\n \
424425 '--line-range :40' prints lines 1 to 40\n \
425426 '--line-range 40:' prints lines 40 to the end of the file\n \
426− '--line-range 40' only prints line 40",
427+ '--line-range 40' only prints line 40\n \
428+ '--line-range 30:+10' prints lines 30 to 40",
427429 ),
428430 )
429431 .arg(
src/line_range.rs+27−1
@@ -47,7 +47,16 @@ impl LineRange {
4747 }
4848 2 => {
4949 new_range.lower = line_numbers[0].parse()?;
50− new_range.upper = line_numbers[1].parse()?;
50+
51+ new_range.upper = if line_numbers[1].bytes().next().unwrap() == b'+' {
52+ let more_lines = &line_numbers[1][1..]
53+ .parse()
54+ .map_err(|_| "Invalid line number after +")?;
55+ new_range.lower + more_lines
56+ } else {
57+ line_numbers[1].parse()?
58+ };
59+
5160 Ok(new_range)
5261 }
5362 _ => Err(
@@ -100,6 +109,23 @@ fn test_parse_fail() {
100109 assert!(range.is_err());
101110}
102111
112+#[test]
113+fn test_parse_plus() {
114+ let range = LineRange::from("40:+10").expect("Shouldn't fail on test!");
115+ assert_eq!(40, range.lower);
116+ assert_eq!(50, range.upper);
117+}
118+
119+#[test]
120+fn test_parse_plus_fail() {
121+ let range = LineRange::from("40:+z");
122+ assert!(range.is_err());
123+ let range = LineRange::from("40:+-10");
124+ assert!(range.is_err());
125+ let range = LineRange::from("40:+");
126+ assert!(range.is_err());
127+}
128+
103129#[derive(Copy, Clone, Debug, PartialEq)]
104130pub enum RangeCheckResult {
105131 // Within one of the given ranges