-
Notifications
You must be signed in to change notification settings - Fork 260
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
chore(examples): simplify all examples #1189
base: main
Are you sure you want to change the base?
Conversation
Adds: - `CrosstermBackend::stdout` and `CrosstermBackend::stderr` for creating backends with `std::io::stdout` and `std::io::stderr` respectively. - `CrosstermBackend::into_terminal` for converting a backend into a terminal. - `CrosstermBackend::into_terminal_with_options` for converting a backend into a terminal with options. - `CrosstermBackend::into_terminal_with_defaults` for converting a backend into a terminal with default settings (raw mode and alternate screen). - `CrosstermBackend::with_raw_mode` for enabling raw mode. - `CrosstermBackend::with_alternate_screen` for switching to the alternate screen. - `CrosstermBackend::with_mouse_support` for enabling mouse support. - `CrosstermBackend::reset` for resetting the terminal to its default state. - `Drop` implementation for restoring raw mode, mouse capture, and alternate screen on drop. Most applications can now just use the `into_terminal_with_defaults` method to set up the terminal with raw mode and the alternate screen, and now longer need to worry about restoring the terminal to its default state when the application exits. The reset method can be used to restore the terminal to its default state if needed (e.g. in a panic handler). ```rust use ratatui::backend::CrosstermBackend; let terminal = CrosstermBackend::stdout() .into_terminal_with_defaults()?; ```
Adds: - `CrosstermBackend::stdout` and `CrosstermBackend::stderr` for creating backends with `std::io::stdout` and `std::io::stderr` respectively. - `CrosstermBackend::into_terminal` for converting a backend into a terminal. - `CrosstermBackend::into_terminal_with_options` for converting a backend into a terminal with options. - `CrosstermBackend::into_terminal_with_defaults` for converting a backend into a terminal with default settings (raw mode and alternate screen). - `CrosstermBackend::with_raw_mode` for enabling raw mode. - `CrosstermBackend::with_alternate_screen` for switching to the alternate screen. - `CrosstermBackend::with_mouse_support` for enabling mouse support. - `CrosstermBackend::reset` for resetting the terminal to its default state. - `Drop` implementation for restoring raw mode, mouse capture, and alternate screen on drop. Most applications can now just use the `into_terminal_with_defaults` method to set up the terminal with raw mode and the alternate screen, and now longer need to worry about restoring the terminal to its default state when the application exits. The reset method can be used to restore the terminal to its default state if needed (e.g. in a panic handler). ```rust use ratatui::backend::CrosstermBackend; let terminal = CrosstermBackend::stdout() .into_terminal_with_defaults()?; ```
Apps can now enable color-eyre hooks using the with_color_eyre_hooks method on CrosstermBackend. This is also added to the default features in the Cargo.toml file, and the defaults that are applied to terminals created using CrosstermBackend::into_terminal_with_defaults.
Note - panic example is not yet complete, and this probably needs a once over to check that I've not accidentally introduced bugs. This is still a WIP, but the key takeaway is that this drops about 900 lines of code from 41 example files. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1189 +/- ##
=======================================
- Coverage 94.3% 93.7% -0.6%
=======================================
Files 60 62 +2
Lines 14679 15036 +357
=======================================
+ Hits 13843 14095 +252
- Misses 836 941 +105 ☔ View full report in Codecov by Sentry. |
Blocked on #1181 |
In #1180 and
#1181, we introduced some
simpler ways to create a backend and terminal, and to setup color_eyre
to handle errors. This PR updates the examples to use these new methods,
and removes a bunch of unnecessary boilerplate code.