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

Missing reset/cleanup in provided example (README.md) leading to terminal state corruption #3

Closed
glenux opened this issue Nov 20, 2023 · 5 comments

Comments

@glenux
Copy link

glenux commented Nov 20, 2023

Context

I'm experiencing an issue with the example provided in the tput.cr library's README.md (rather than the tput.cr library itself)... and as a result, I don't know what the good practices are for using tput.cr 😓

I've searched the source code and tried various things before the exit (soft_reset, clear, etc.), but I haven't (yet) found a function that solves the problem.

Environment

  • Crystal Version: 1.10.1
  • tput.cr Version: v1.0.7
  • Operating System: Debian Bookworm (12)
  • Terminal Emulator: Konsole 23.08.1 , xterm 386-1, qterminal 1.3.0-1,

Reproduction Steps

  1. Create a project using the shards.yml below :
---
name: Test
version: 0.1.0

targets:
  test:
    main: src/test.cr

dependencies:
  tput:
    github: crystallabs/tput.cr
    version: ~> 1.0

license: MIT
  1. Copy-paste the example from tput.cr's README.md in src/test.cr.
  2. Compile with shards build and executed in Konsole.

Observed Behavior

  • Post-execution, the terminal is left in an unusable state with no echo (invisible text), and the cursor is still colored.
  • No specific error logs are generated by tput.cr or by the terminal.
  • Typing "reset" in the terminal restores its functionality, indicating a probable issue in the cleanup sequence of the example code.

Insights/Fixes

The example code in tput.cr's documentation likely lacks an adequate cleanup or reset sequence for the terminal state, leading to this issue.

@glenux
Copy link
Author

glenux commented Nov 20, 2023

I tried a variant without the listen() loop that works perfectly. Same thing with break instead of exit.

It seems that exiting from the listen loop creates the terminal state corruption (maybe because of a raw mode access to the terminal for reading events ?)

@docelic
Copy link
Collaborator

docelic commented Nov 20, 2023

Heya, let me check and I will report back shortly.

@docelic
Copy link
Collaborator

docelic commented Nov 20, 2023

Hey,

Yes, you are right, break helps a lot because it properly ends the with_raw_input block which returns the terminal mode back to what it was. So that solves the first part of the issue.

The second part is that even after mode is restored, cursor color remains red, and not even reset helps. Obviously that's because reset doesn't touch the cursor itself. Unfortunately tput.cr currently does not support any escape sequences that read data from the terminal -- including the one which reads the complete terminal state.
(I am referring to some equivalent of function _bindResponse in Blessed's file https://github.com/chjj/blessed/blob/master/lib/program.js , on line 1040).

(I would be very much interested in receiving a PR that adds this; that would be amazing!)

In any case, because of inability to read terminal state, it is not possible to return the cursor color and blink to what it was before running the program; what we can do is just assume that the color before was white, and set it to white.

I've updated the README example to show this, and to fix some other things -- for example, terminal type and features were printed at the top of example, but weren't visible on screen because after print the screen was switched to alternate buffer. And I've made a number of smaller clarifications/improvements in code comments.

Hope it helps and thanks for the report!

Feel free to continue the discussion, or close the ticket if your question was adequately covered.

@glenux
Copy link
Author

glenux commented Nov 20, 2023

  1. Thank you for your clarification and updates.

  2. Can you create a more detailed issue with your thoughts and explaining what you expect when reading the terminal state? I confess I'm still figuring out the interactions between terminfo, tput, tcgetattr, etc. but given what I understand, I'll probably come across the state reading issue soon (I'm developing a project that incorporates something similar to fzf) and I would love to contribute with a PR then :)

  3. Yes, you can close the current issue.

@docelic docelic closed this as completed Nov 20, 2023
@docelic
Copy link
Collaborator

docelic commented Nov 21, 2023

I added a longer description in #4. Feel free to comment there if you are interested.

Regarding your project -- sounds interesting and like it could benefit both you and tput.cr. Feel free to open tickets for any discussion or help you'd need from me. Thanks!

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

No branches or pull requests

2 participants