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

fix: Rewrite the insert_before method on Terminal to fix a bug and remove the height cap #596

Merged
merged 13 commits into from
Nov 8, 2023
Merged

fix: Rewrite the insert_before method on Terminal to fix a bug and remove the height cap #596

merged 13 commits into from
Nov 8, 2023

Conversation

danny-burrows
Copy link
Contributor

@danny-burrows danny-burrows commented Oct 25, 2023

This PR fixes some issues encountered in #584 by way of rewriting the insert_before method.

The function signature for reference:

pub fn insert_before<F>(&mut self, height: u16, draw_fn: F) -> io::Result<()>

Ultimately it does two things:

  1. It fixes a bug triggered by the height parameter of a call to insert_before being greater than self.viewport_area.height - This bug would cause an extra newline to be inserted leaving a gap in the output.
  2. Removes the height cap by rendering output in chunks. if height is too large to fit on the screen (with the viewport) then chunks are rendered as the screen is scrolled.

A Note About Test Coverage

There are currently no tests for the insert_before method; I've attempted to test this change with the TestBackend but unfortunately due to it not supporting clear ClearType::AfterCursor this would prove difficult and I think the noise of adding this functionality wouldn't be worth adding to this PR. As it's a very minimal hit I wondered if it's acceptable?

Update: I've now added some test cases for the issues encountered in the tests/ folder. I know this is now discouraged but there was already a pattern of using the TestBackend in this location, but I'd be open to moving them if requested. I've also added the clear_region and append_lines methods to the TestBackend with accompanying tests.

@danny-burrows danny-burrows changed the title Rewrite the insert_before method on Terminal to fix a bug and remove the height cap Fix: Rewrite the insert_before method on Terminal to fix a bug and remove the height cap Oct 25, 2023
@codecov
Copy link

codecov bot commented Oct 25, 2023

Codecov Report

Merging #596 (4b0bc23) into main (a2f2bd5) will increase coverage by 0.91%.
Report is 2 commits behind head on main.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #596      +/-   ##
==========================================
+ Coverage   89.56%   90.47%   +0.91%     
==========================================
  Files          41       41              
  Lines       11571    11890     +319     
==========================================
+ Hits        10363    10758     +395     
+ Misses       1208     1132      -76     
Files Coverage Δ
src/backend.rs 71.42% <ø> (-9.53%) ⬇️
src/backend/test.rs 97.79% <100.00%> (+2.94%) ⬆️
src/terminal.rs 88.60% <100.00%> (+30.80%) ⬆️

@danny-burrows danny-burrows changed the title Fix: Rewrite the insert_before method on Terminal to fix a bug and remove the height cap fix: Rewrite the insert_before method on Terminal to fix a bug and remove the height cap Oct 25, 2023
Fixes issue with inserting content with height>viewport_area.height and adds the ability to insert content of height>terminal_height
@joshka
Copy link
Member

joshka commented Oct 25, 2023

There are currently no tests for the insert_before method; I've attempted to test this change with the TestBackend but unfortunately due to it not supporting clear ClearType::AfterCursor this would prove difficult and I think the noise of adding this functionality wouldn't be worth adding to this PR. As it's a very minimal hit I wondered if it's acceptable?

I think it's probably worth adding the clear ops to the TestBackend to help with this change. There are three main reasons I like to add test coverage to existing code before changing it:

  1. When the existing code is complex or just not well understood, tests help document the existing behavior (particularly the edge cases)
  2. When the existing code is changed, the tests help document how the behavior changes from the perspective of the consuming code
  3. When the existing code is tested, any new implementation can be fearlessly refactored for simplicity and readability while maintaining correctness of implementation

These reasons each make changes easier to review as otherwise a reviewer has to determine whether the code did the right thing originally by fully understanding the code, and whether it now still does the right thing by understanding the new code. Tests like this also help capture if there are breaking changes that other users need to know about.

Here the code is not too complex, but I don't fully understand it. It currently does a particular thing and I'm not sure I know why it does that enough to understand whether the change is right.

My approach generally would be to write characterization tests that fully cover the problem code, commit them, then make the changes and see which tests break. How about adding the missing TestBackend implementations for clear that would show how this works and then adding the tests for this? Do you think this sound reasonable?

@danny-burrows
Copy link
Contributor Author

That's reasonable yes, I'll work on adding that functionality to the TestBackend and some tests to showcase the issues.

@danny-burrows
Copy link
Contributor Author

I've added the functionality we discussed above, the clear_region and append_lines methods. I've also added some tests for the insert_before method. I hope this helps! Things do look a lot cleaner now having these tests, thanks for the suggestion! :)

@kdheepak
Copy link
Collaborator

Slightly related, maybe we should set a threshold in codecov.yml?

https://docs.codecov.com/docs/commit-status#threshold

@danny-burrows
Copy link
Contributor Author

Slightly related, maybe we should set a threshold in codecov.yml?

https://docs.codecov.com/docs/commit-status#threshold

I'd be happy to add one if you think it makes sense in this PR, do you have an acceptable value in mind? Alternatively, if this requires further discussion maybe it would be better suited to its own Issue/PR?

Copy link
Member

@joshka joshka left a comment

Choose a reason for hiding this comment

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

The overarching context of my review is that I'm not as familiar with the workings of this code as you are, so I'm looking for help with the changes and tests to make it more clear that the changes are correct. They might mean that the feedback is a bit pedantic, and for that effect I apologize in advance. But I'd encourage you to think about this change with the perspective of "how would I explain these changes to someone that is reading it for the first time?" (because that's not far from the truth)

src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
tests/terminal.rs Show resolved Hide resolved
tests/terminal.rs Show resolved Hide resolved
@joshka joshka mentioned this pull request Nov 3, 2023
@joshka
Copy link
Member

joshka commented Nov 3, 2023

I'd be happy to add one if you think it makes sense in this PR, do you have an acceptable value in mind? Alternatively, if this requires further discussion maybe it would be better suited to its own Issue/PR?

Another PR would be right for this #612

Copy link
Member

@joshka joshka left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes - I understand TestBackend::append_lines() much better now. Lots of detailed feedback again that I hope will make this better, and I think there's a couple of possible bugs that I'm not sure of.

src/backend/test.rs Show resolved Hide resolved
src/backend/test.rs Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Outdated Show resolved Hide resolved
src/backend/test.rs Show resolved Hide resolved
src/backend/test.rs Show resolved Hide resolved
src/terminal.rs Outdated Show resolved Hide resolved
src/terminal.rs Show resolved Hide resolved
tests/terminal.rs Show resolved Hide resolved
src/terminal.rs Show resolved Hide resolved
@joshka joshka merged commit df0eb1f into ratatui-org:main Nov 8, 2023
33 checks passed
@joshka
Copy link
Member

joshka commented Nov 8, 2023

I've merged this - thanks for sticking with it to get it over the line. It should be available in the alpha release this Saturday.

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