-
Notifications
You must be signed in to change notification settings - Fork 393
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
Add option to show line numbers #190
Conversation
Thanks very much for this @clnoll! This is a substantial contribution and it's a feature that's been requested multiple times. I'll start reviewing. |
92fe139
to
aa1b35b
Compare
src/delta.rs
Outdated
let (raw_code_fragment, line_number) = parse::parse_hunk_metadata(&line); | ||
let (raw_code_fragment, line_number_in, line_number_out) = parse::parse_hunk_metadata(&line); | ||
painter.line_number_in = line_number_in.parse::<usize>().unwrap_or(0); | ||
painter.line_number_out = line_number_out.parse::<usize>().unwrap_or(0); |
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.
Do you know under what circumstances we expect this parse to fail?
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.
It was failing on new files (new line number would be 0). But at any rate as you've seen, I updated parse_hunk_metadata
to handle this case.
src/delta.rs
Outdated
None, | ||
Some(painter.line_number_out), | ||
config, | ||
)); |
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.
Are these ones (in the -
and +
branches) necessary?
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.
Nope! Removed.
src/delta.rs
Outdated
let divider = paint::paint_text_foreground("│", config.hunk_color, config.true_color); | ||
format!("{}{}", numbers, divider) | ||
} | ||
|
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.
Playing around with the vertical dividing line character. E.g. https://jrgraphix.net/r/Unicode/2500-257F https://unicode.org/charts/nameslist/n_2500.html. Any thoughts? (I haven't found one yet which yields a vertical dashed line without "breaks" between lines.)
Current:
Alternatives:
U2506
U250A
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.
Added an option to input a divider (--number-center-divider-string
).
src/delta.rs
Outdated
Some(x) => format!("{:^4}", x), | ||
None => format!(" "), |
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.
does this handle line numbers over 4 digits without breaking alignment? Maybe if it were deferred to paint_lines
as suggested below, it would be easier to ensure that longer numbers are handled since you'd have access to all of them.
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.
👍 deferred to paint_lines
. Need to take a look at how it handles >4 digits.
Thanks @dandavison & @rygwdn! Great suggestions, making these changes. |
70730d7
to
487649b
Compare
@dandavison @rygwdn I took all of the suggestions mentioned above, and added some additional command line options (colors for the line numbers and the dividers, and the ability to configure a string for the dividers). FYI I'm seeing that it's a bit slower than Master:
Feature branch, with
Feature branch, without
|
fab512b
to
a07c8f2
Compare
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.
OK this is looking fantastic. Delta is getting much more customizable! In addition to having line numbers, with the configurability you've added here it's going to be possible to use it in some interesting new ways I think. I've made a few minor comments and will now have a look at performance etc.
README.md
Outdated
@@ -219,6 +219,7 @@ FLAGS: | |||
The default behavior is to output a space character in place of these markers. | |||
--light Use default colors appropriate for a light terminal background. For more control, | |||
see the other color options. | |||
--line-numbers Show line numbers for each line of the diff hunk. |
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.
This is out of date now. (I do this step with delta --help >> README.md
and then moving the appended text into position and fiddling around a bit.)
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.
👍 done.
da2780b
to
c62d67c
Compare
let caps = re.captures(line).unwrap(); | ||
let line_numbers = _make_line_number_vec(caps.name("lns").unwrap().as_str()); | ||
let code_fragment = caps.name("cf").unwrap().as_str(); | ||
return (code_fragment, line_numbers); |
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 just learned that building regular expressions like this inside a function called many times is extremely inefficient. This commit on master speeds delta up by almost 2x 2cb3e40! I suspect if you do the same thing here it will get rid of the performance difference between this branch and master.
In addition to make benchmark
I've also added make flamegraph
, for profiling on MacOS, which is sort of fun. (The Makefile assumes that you've cloned https://github.com/brendangregg/FlameGraph and symlinked two of its perl scripts into your $PATH with the names flamegraph
and stackcollapse-sample
.)
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.
Awesome, updated to do the same.
} | ||
return numbers; | ||
} | ||
|
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.
Do you think it's possible to do it all with a regular expression, i.e. capture the numbers as well rather than using this function? While we're at it, we could also capture and expose the first set of @
markers seeing as they will be needed for determining the number of merge parents for #189 cc @rygwdn
I've been doing a lot of refactoring in parallel and so if it's OK with you You'll note that it...doesn't actually compile :) But it's fairly superficial -- we need to re-express the color options in this branch as what are referred to as "styles" in |
src/cli.rs
Outdated
/// Color for the right (plus) column of line numbers (--number), if --number is set. | ||
/// Defaults to --hunk-color. | ||
#[structopt(long = "number-plus-foreground-color")] | ||
pub number_plus_foreground_color: Option<String>, |
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.
In the new world of the style-strings
branch I think we're going to want these options to be named --number-minus-style
and --number-plus-style
. (It might help to take a look at the new style documentation in delta --help
.)
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.
Done!
src/cli.rs
Outdated
|
||
/// String to use as the right divider of the line numbers section, if --number is set. | ||
#[structopt(long = "number-right-divider-string", default_value = "│")] | ||
pub number_right_divider_string: String, |
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 I'd like to investigate offering users a format string, rather than specifying the divider characters individually. So basically something with placeholder slots for the two line numbers. It's hard to make future-proof decisions about the CLI: I wonder whether that should be called something like --hunk-line-format
in order to retain the possibility that in the future it might have other placeholder slots?
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.
👍 made this change.
The style-string refactoring is all in |
037846f
to
ba30656
Compare
ab02c14
to
030a997
Compare
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.
@dandavison I've addressed all of your comments and modified the CLI to accept a format string & the updated style strings. Let me know what you think!
19f34f2
to
142b1d0
Compare
assert_eq!(code_fragment, " dependencies =",); | ||
assert_eq!(line_numbers[0], 293,); | ||
assert_eq!(line_numbers[1], 358,); | ||
assert_eq!(line_numbers[2], 358,); |
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.
Nice! Looks like this will help with highlighting merge diffs in #189.
- format string for specifying minus number line - format string for specifying plus number line - minus number style - plus number style - minus format string style - plus format string style
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.
Thanks very much for this work @cnoll. This is a really nicely implemented and substantial feature, that I think people are going to appreciate, and with tons of customizability. Merging now!
Adds a CLI option,
--number
, that shows line numbers for the input and output diffs. The first column shows the line number for the original file, and the second column shows line number for the output file. A blank cell indicates that the line does not exist in one of the files (was removed or added, respectively).Also provides the ability to customize the appearance of line numbers. Users can pass in a format string showing the position of the line numbers within the column, and can apply style elements to the numbers as well as the format string text, using new command line options:
--number-minus-style
--number-plus-style
--number-minus-format
--number-plus-format
--number-minus-format-style
--number-plus-format-style
Addresses #130.
TODO: tests.