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(termion): Add fallback for failing cursor detection #981

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

erak
Copy link

@erak erak commented Mar 4, 2024

With TermionBackend, when the cursor position on stdout is being read from another thread then the one
reading events from stdin, cursor detection will fail. See: https://gitlab.redox-os.org/redox-os/termion/-/issues/173

The workaround is to always store the last known cursor position and use it as fallback.

Copy link

codecov bot commented Mar 4, 2024

Codecov Report

Attention: Patch coverage is 33.33333% with 8 lines in your changes are missing coverage. Please review.

Project coverage is 91.9%. Comparing base (c4ce7e8) to head (e6b00d2).

❗ Current head e6b00d2 differs from pull request most recent head 3953430. Consider uploading reports for the commit 3953430 to get more accurate results

Files Patch % Lines
src/backend/termion.rs 33.3% 8 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main    #981     +/-   ##
=======================================
- Coverage   92.0%   91.9%   -0.1%     
=======================================
  Files         61      61             
  Lines      15933   15943     +10     
=======================================
+ Hits       14660   14663      +3     
- Misses      1273    1280      +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@erak erak force-pushed the termion-cursor branch 2 times, most recently from e6b00d2 to f033297 Compare March 5, 2024 09:32
Cargo.toml Outdated Show resolved Hide resolved
Comment on lines 144 to 153
match termion::cursor::DetectCursorPos::cursor_pos(&mut self.writer) {
Ok((x, y)) => {
self.cursor = Some((x - 1, y - 1));
Ok((x - 1, y - 1))
}
Err(_) => self
.cursor
.ok_or_else(|| io::Error::other("No last known cursor position")),
}
Copy link
Member

Choose a reason for hiding this comment

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

Personally I would keep the map logic and do the match after that. Then there is no duplicate x-1,y-1 logic needed and its only in one place.

Copy link
Author

Choose a reason for hiding this comment

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

Kept the match structure as it was, but introduced a local variable that holds the mapped cursor.

}
Err(_) => self
.cursor
.ok_or_else(|| io::Error::other("No last known cursor position")),
Copy link
Member

Choose a reason for hiding this comment

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

Can a cursor position of 0,0 just be assumed as a fallback to the fallback? Or could that have issues?

Copy link
Author

Choose a reason for hiding this comment

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

I think that could work. I discovered this issue with auto-resizing an inline viewport. Can't tell if that would have other side effects then the frame being redrawn at 0,0.

@erak erak force-pushed the termion-cursor branch 3 times, most recently from c9c857e to 0e2bdbb Compare March 5, 2024 10:56
@erak
Copy link
Author

erak commented Mar 5, 2024

I'd need some advice on the unit testing. So currently, both tests that we're added to this PR do fail on terminal
construction with Inappropriate ioctl for device:

let backend = TermionBackend::new(&mut stdout);
let mut terminal = Terminal::new(backend)?;

I could change the construction to use terminal options, which would work. But then, retrieving the cursor position seems to block and the test never finishes.

let backend = TermionBackend::new(&mut stdout);
let area = Rect::new(0, 0, 3, 1);
let mut terminal = Terminal::with_options(
    backend,
    TerminalOptions {
        viewport: Viewport::Fixed(area),
     },
)?;

@kdheepak
Copy link
Collaborator

kdheepak commented Mar 5, 2024

Are the tests failing locally for you as well, or is it just failing on GitHub Actions?

@erak
Copy link
Author

erak commented Mar 5, 2024

Are the tests failing locally for you as well, or is it just failing on GitHub Actions?

They fail with cargo nextest run <args>, but they pass with cargo test <args> 🤔

@orhun
Copy link
Sponsor Member

orhun commented Mar 5, 2024

Reported to nextest: nextest-rs/nextest#1357

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