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 minus syntax #1901

Merged
merged 6 commits into from
Nov 24, 2021

Conversation

bojan88
Copy link
Contributor

@bojan88 bojan88 commented Oct 11, 2021

Continuation from #1810

In case we are working with a long file and we are interested in line 55478, currently we could use 55478:+5 to quickly see 5 lines after 55478.

We might be interested to see a few lines preceding line 55478, and with this change we could do it with 55478:-5 or 55478:-30

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

src/line_range.rs Outdated Show resolved Hide resolved
src/line_range.rs Outdated Show resolved Hide resolved
src/line_range.rs Outdated Show resolved Hide resolved
@sharkdp
Copy link
Owner

sharkdp commented Oct 24, 2021

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

Could you please explain to us if the syntax is only similar or exactly the same? Might make sense to adopt what git log implemented.

@bojan88
Copy link
Contributor Author

bojan88 commented Oct 26, 2021

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

Could you please explain to us if the syntax is only similar or exactly the same? Might make sense to adopt what git log implemented.

It's similar - git log -L has more options. Here's part of the description from man page:

-L<start>,<end>:<file>, -L:<funcname>:<file>

<start> and <end> can take one of these forms:

           •   number

               If <start> or <end> is a number, it specifies an
               absolute line number (lines count from 1).

           •   /regex/

               This form will use the first line matching the given
               POSIX regex. If <start> is a regex, it will search
               from the end of the previous -L range, if any,
               otherwise from the start of file. If <start> is
               ^/regex/, it will search from the start of file. If
               <end> is a regex, it will search starting at the line
               given by <start>.

           •   +offset or -offset

               This is only valid for <end> and will specify a
               number of lines before or after the line given by
               <start>.

assert_eq!(1, range.lower);
assert_eq!(5, range.upper);
let range = LineRange::from("5:-5").expect("Shouldn't fail on test!");
assert_eq!(0, range.lower);
Copy link
Owner

Choose a reason for hiding this comment

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

Wait. Shouldn't that be 1? Is 0 a valid line number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out it is.
Try running bat -r 0:5 [some_file] and bat -r 1:5 [some_file] - both commands produce the same output

@sharkdp
Copy link
Owner

sharkdp commented Nov 22, 2021

Thank you for the updates!

Could you please add an entry to the "unreleased" section in CHANGELOG.md? The format for entries is:

- Description what has been changed, see #123 (@user)

where #123 links to the bug ticket and/or the PR and user is your username.

@bojan88
Copy link
Contributor Author

bojan88 commented Nov 23, 2021

@sharkdp updated. I also merged latest master.

@sharkdp sharkdp merged commit 9a2979d into sharkdp:master Nov 24, 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

4 participants