-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
430eda3
to
5e2312a
Compare
CHANGELOG.md
Outdated
@@ -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:+x` syntax in line ranges (e.g. `20:+10`). See #1810 (@bojan88) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe n:+delta
syntax?
src/line_range.rs
Outdated
new_range.upper = if line_numbers[1].bytes().next().unwrap() == b'+' { | ||
let more_lines = &line_numbers[1][1..] | ||
.parse() | ||
.map_err(|_| "Invalid line number after +")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "invalid number after"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a bit more thinking I changed it to "Invalid character after +".
Please let me know if you still think we should change it, I don't mind.
I kept capital I
because other messages start with capital letter.
And using character
instead of number
in the message because there might be something like +-10
(an assumption by a user) or +z1
(maybe a typo)
src/line_range.rs
Outdated
@@ -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().unwrap() == b'+' { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the .unwrap()
call here is safe (but only due to the checks in lines 33:39), but still: it might be better to avoid it in case the surrounding logic here changes in the future. I think you could simply go with:
if line_numbers[1].bytes().next() == Some(b'+') {
what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like using .unwrap()
too, but it looked impossible to fail.
Your suggestion is making it even safer, and much better. Thanks.
Thank you very much for your contribution! I like this new feature. |
Working with long files and using line range can be hard sometimes. For example if we want to see just a few lines after line
55478
, we would need to write both numbers which are large, and that can lead to mistakes.It's much easier to write
55478:+5
or55478:+30
.This is influenced by similar syntax in
git
forlog
sub-command with-L
argument.