Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for line range plus syntax #1810

Merged
merged 2 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Features

- `$BAT_CONFIG_DIR` is now a recognized environment variable. It has precedence over `$XDG_CONFIG_HOME`, see #1727 (@billrisher)
- Support for `x:+delta` syntax in line ranges (e.g. `20:+10`). See #1810 (@bojan88)

## Bugfixes

Expand Down
6 changes: 5 additions & 1 deletion assets/manual/bat.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ highlights lines 30 to 40
highlights lines 1 to 40
.IP "\-\-highlight\-line 40:"
highlights lines 40 to the end of the file
.IP "\-\-highlight\-line 30:+10"
highlights lines 30 to 40
.RE
.HP
\fB\-\-file\-name\fR <name>...
Expand Down Expand Up @@ -154,6 +156,8 @@ prints lines 30 to 40
prints lines 1 to 40
.IP "\-\-line\-range 40:"
prints lines 40 to the end of the file
.IP "\-\-line\-range 30:+10"
prints lines 30 to 40
.RE
.HP
\fB\-L\fR, \fB\-\-list\-languages\fR
Expand Down Expand Up @@ -218,7 +222,7 @@ git clone https://github.com/tellnobody1/sublime-purescript-syntax

Once the cache is built, the new language will be visible in `\fB{{PROJECT_EXECUTABLE}} --list-languages\fR`.
.br
If you ever want to remove the custom languages, you can clear the cache with `\fB{{PROJECT_EXECUTABLE}} cache --clear\fR`.
sharkdp marked this conversation as resolved.
Show resolved Hide resolved
If you ever want to remove the custom languages, you can clear the cache with `\fB{{PROJECT_EXECUTABLE}} cache --clear\fR`.

.SH "ADDING CUSTOM THEMES"
Similarly to custom languages, {{PROJECT_EXECUTABLE}} supports Sublime Text \fB.tmTheme\fR themes.
Expand Down
6 changes: 4 additions & 2 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
'--highlight-line 40' highlights line 40\n \
'--highlight-line 30:40' highlights lines 30 to 40\n \
'--highlight-line :40' highlights lines 1 to 40\n \
'--highlight-line 40:' highlights lines 40 to the end of the file",
'--highlight-line 40:' highlights lines 40 to the end of the file\n \
'--highlight-line 30:+10' highlights lines 30 to 40",
),
)
.arg(
Expand Down Expand Up @@ -423,7 +424,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
'--line-range 30:40' prints lines 30 to 40\n \
'--line-range :40' prints lines 1 to 40\n \
'--line-range 40:' prints lines 40 to the end of the file\n \
'--line-range 40' only prints line 40",
'--line-range 40' only prints line 40\n \
'--line-range 30:+10' prints lines 30 to 40",
),
)
.arg(
Expand Down
28 changes: 27 additions & 1 deletion src/line_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ impl LineRange {
}
2 => {
new_range.lower = line_numbers[0].parse()?;
new_range.upper = line_numbers[1].parse()?;

new_range.upper = if line_numbers[1].bytes().next() == Some(b'+') {
let more_lines = &line_numbers[1][1..]
.parse()
.map_err(|_| "Invalid character after +")?;
new_range.lower + more_lines
} else {
line_numbers[1].parse()?
};

Ok(new_range)
}
_ => Err(
Expand Down Expand Up @@ -100,6 +109,23 @@ fn test_parse_fail() {
assert!(range.is_err());
}

#[test]
fn test_parse_plus() {
let range = LineRange::from("40:+10").expect("Shouldn't fail on test!");
assert_eq!(40, range.lower);
assert_eq!(50, range.upper);
}

#[test]
fn test_parse_plus_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
Expand Down