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

Add VisualLines newtype wrapper #616

Merged
merged 3 commits into from Dec 30, 2023
Merged

Conversation

smoelius
Copy link
Contributor

An attempt to add a newtype wrapper, as discussed in #612 (comment) and #612 (comment).

Notes

  1. The wrapper is called VisualLines because that was the term introduced in fix and rename real_len #608, but I am not tied to that name.
  2. This PR adds a dependency: derive_more.
  3. I did not modify the TermLike trait. It has both "height" and "width" characteristics, and it seemed weird to modify just one.

@chris-laplante
Copy link
Collaborator

Thanks! First impressions are that I actually really like this. There are a lot fewer casts than I expected. (I also didn't know about SliceIndex - that's pretty cool.)

The real question will be whether or not it makes the code clearer and easier to read though. I'm going to take some time to digest it over the next few days.

Thanks again for the work!

Copy link
Collaborator

@djc djc left a comment

Choose a reason for hiding this comment

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

LGTM modulo nits, thanks for working on this!

use std::sync::{Arc, RwLock, RwLockWriteGuard};
use std::thread::panicking;
use std::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

use console::Term;
use derive_more::{Add, AddAssign, Sub};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to avoid this extra dependency in favor of just writing out the impls manually.

@@ -72,7 +74,7 @@ impl ProgressDrawTarget {
Self {
kind: TargetKind::Term {
term,
last_line_count: 0,
last_line_count: VisualLines::zero(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I'd prefer relying on VisualLines::default() instead of zero() especially for cases like these where we're initializing. Is there any case where you think that's unreasonable and zero() makes for a better alternative?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any case where you think that's unreasonable and zero() makes for a better alternative?

Not really. And I don't have a strong preference.

@@ -248,7 +250,7 @@ enum TargetKind {
impl TargetKind {
/// Adjust `last_line_count` such that the next draw operation keeps/clears additional lines
fn adjust_last_line_count(&mut self, adjust: LineAdjust) {
let last_line_count: &mut usize = match self {
let last_line_count: &mut VisualLines = match self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: let's just get rid of the type annotation here?


pub(crate) fn visual_line_count<I: SliceIndex<[String], Output = [String]>>(
&self,
range: I,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: prefer writing this as impl SliceIndex<[String], Output = [String]>, avoiding the need for an intermediate I variable.

Comment on lines 572 to 576
impl<T: Into<usize>> From<T> for VisualLines {
fn from(value: T) -> Self {
Self(value.into())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: move this below the inherent impl, please.

Comment on lines 579 to 590
pub(crate) fn as_usize(&self) -> usize {
self.0
}
pub(crate) fn saturating_add(&self, other: Self) -> Self {
Self(self.0.saturating_add(other.0))
}
pub(crate) fn saturating_sub(&self, other: Self) -> Self {
Self(self.0.saturating_sub(other.0))
}
pub(crate) fn zero() -> Self {
Self(0)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nits: let's order these zero() (if we're keeping it), the saturating methods, then as_usize(), each separated by an empty line.

}

#[test]
fn visual_lines_default_is_zero() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move this into the mod tests below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed zero per the above request.

@smoelius
Copy link
Contributor Author

@djc I think I addressed all of your comments. Please let me know if there are any I missed.

@djc djc merged commit 9f99ad5 into console-rs:main Dec 30, 2023
10 checks passed
@smoelius smoelius deleted the newtype-wrapper branch December 30, 2023 11:10
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