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

Linenumber refactoring #740

Merged
merged 2 commits into from Oct 18, 2021
Merged

Linenumber refactoring #740

merged 2 commits into from Oct 18, 2021

Conversation

th1000s
Copy link
Contributor

@th1000s th1000s commented Oct 17, 2021

One way to restore the ANSI background in side-by-side mode is to ensure the whole width of the terminal is used.

However if the width is odd, then one extra character needs to be inserted somewhere. A good place would be at the end of the first prefix, i.e. usually before the first line number. This lays the groundwork for that.

And according to hyperfine, using SmolStr does not cause a slowdown (here it was a tiny bit faster more often than not, but that might just be luck with the linker).

Also move number and style logic out of `format_and_paint_line_numbers()`
and into a separate `linenumbers_and_styles()` function.
Some("commit") => s.push_str(&pad(&delta::format_raw_line(blame.commit, config))),
Some(Placeholder::Str("author")) => s.push_str(&pad(blame.author)),
Some(Placeholder::Str("commit")) => {
s.push_str(&pad(&delta::format_raw_line(blame.commit, config)))
Copy link
Owner

Choose a reason for hiding this comment

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

To check my understanding: we might decide in the future to add "timestamp", "author", and "commit" to the Placeholder enum and handle them in the try_from implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, these could be turned into enum fields as well. I left it at strs to show that this is still flexible enough for generic input.

pub alignment_spec: Option<&'a str>,
pub width: Option<usize>,
pub suffix: &'a str,
pub prefix: SmolStr,
Copy link
Owner

Choose a reason for hiding this comment

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

Is the basic idea here that this will be more efficient when the prefix/suffix material is incorporated into a "painted" string (i.e. wrapped with ANSI escapes) by ansi_term::Style::paint, and that this efficiency increase is due to the O(1) clone of SmolStr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I read the ANSIGenericString<str> and style.paint() correctly then it won't copy anything but just hold a reference into the SmolStr in its Cow<'a, str>, the .into() should not copy anything, as per from: "Converts a string slice into a Borrowed variant. No heap allocation is performed, and the string is not copied."

And I actually thought of pre-calculating these, but then the placeholder would need a {prefix,suffix} x {left, right} = 4 extra fields for that. I was also tempted to sacrifice clarity for imagined performance gains and move suffix and prefix into a single field and only store offsets into it :)

Copy link
Owner

@dandavison dandavison left a comment

Choose a reason for hiding this comment

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

More very nice improvements, LGTM!

@dandavison dandavison merged commit 3a03d4b into dandavison:master Oct 18, 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

2 participants