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

Conversation

bojan88
Copy link
Contributor

@bojan88 bojan88 commented Aug 21, 2021

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 or 55478:+30.

This is influenced by similar syntax in git for log sub-command with -L argument.

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe n:+delta syntax?

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 +")?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "invalid number after"?

Copy link
Contributor Author

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)

@@ -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'+' {
Copy link
Owner

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?

Copy link
Contributor Author

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.

@sharkdp
Copy link
Owner

sharkdp commented Aug 22, 2021

Thank you very much for your contribution! I like this new feature.

@sharkdp sharkdp merged commit c86a179 into sharkdp:master Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants