php-tui / term

low-level terminal manipulation library for PHP
MIT License
30 stars 4 forks source link

Disabling the raw mode with try/finally #7

Closed stof closed 12 months ago

stof commented 12 months ago

Currently, examples will leave the terminal in a broken state by leaving them in raw mode in case a PHP exception happens.

I think it would be great that all examples that enable raw mode wrap the following code in a try/finally block to disable it again in case of exception.

Btw, if exit is not used inside that code block, it might be the only place that needs to disable it back. And maybe a Terminal::withRawMode(callable(): T): T API would make sense to run that callable with raw mode enabled (the T is might proposal is about making that method generic and returning the return value of the callback) in a finally block (this would not be a good idea if the most common case is to use exit as that would not reach the end of the callback to re-disable it)

dantleech commented 12 months ago

i think it's specific to the events.php example? but yes, adding error handling would be good - these examples are honestly quite quick-and-dirty.

r.e. withRawMode "raw mode" might not be the only thing that should be reset, e.g. enabling/disabling "mouse capture", showing/hiding the cursor etc also mutates the terminal.

some setup/teardown should be expected in any app, I wonder if trying to anticipate that in the API would overcomplicate things?

dantleech commented 12 months ago

created a PR to handle errors: https://github.com/php-tui/term/pull/8

dantleech commented 12 months ago

merged that, closing this issue but feel free to continue the discussion :)