mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 04:33:13 +00:00
ed51c4b342
These are simple opinionated methods for creating a terminal that is useful to use in most apps. The new init method creates a crossterm backend writing to stdout, enables raw mode, enters the alternate screen, and sets a panic handler that restores the terminal on panic. A minimal hello world now looks a bit like: ```rust use ratatui::{ crossterm::event::{self, Event}, text::Text, Frame, }; fn main() { let mut terminal = ratatui::init(); loop { terminal .draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area())) .expect("Failed to draw"); if matches!(event::read().expect("failed to read event"), Event::Key(_)) { break; } } ratatui::restore(); } ``` A type alias `DefaultTerminal` is added to represent this terminal type and to simplify any cases where applications need to pass this terminal around. It is equivalent to: `Terminal<CrosstermBackend<Stdout>>` We also added `ratatui::try_init()` and `try_restore()`, for situations where you might want to handle initialization errors yourself instead of letting the panic handler fire and cleanup. Simple Apps should prefer the `init` and `restore` functions over these functions. Corresponding functions to allow passing a `TerminalOptions` with a `Viewport` (e.g. inline, fixed) are also available (`init_with_options`, and `try_init_with_options`). The existing code to create a backend and terminal will remain and is not deprecated by this approach. This just provides a simple one line initialization using the common options. --------- Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
100 lines
3.5 KiB
Rust
100 lines
3.5 KiB
Rust
//! # [Ratatui] Modifiers example
|
|
//!
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
//!
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
//! crates.io, or the one that you have installed locally.
|
|
//!
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
//! library you are using.
|
|
//!
|
|
//! [Ratatui]: https://github.com/ratatui/ratatui
|
|
//! [examples]: https://github.com/ratatui/ratatui/blob/main/examples
|
|
//! [examples readme]: https://github.com/ratatui/ratatui/blob/main/examples/README.md
|
|
|
|
// This example is useful for testing how your terminal emulator handles different modifiers.
|
|
// It will render a grid of combinations of foreground and background colors with all
|
|
// modifiers applied to them.
|
|
|
|
use std::{error::Error, iter::once, result};
|
|
|
|
use itertools::Itertools;
|
|
use ratatui::{
|
|
crossterm::event::{self, Event, KeyCode, KeyEventKind},
|
|
layout::{Constraint, Layout},
|
|
style::{Color, Modifier, Style, Stylize},
|
|
text::Line,
|
|
widgets::Paragraph,
|
|
DefaultTerminal, Frame,
|
|
};
|
|
|
|
type Result<T> = result::Result<T, Box<dyn Error>>;
|
|
|
|
fn main() -> Result<()> {
|
|
color_eyre::install()?;
|
|
let terminal = ratatui::init();
|
|
let app_result = run(terminal);
|
|
ratatui::restore();
|
|
app_result
|
|
}
|
|
|
|
fn run(mut terminal: DefaultTerminal) -> Result<()> {
|
|
loop {
|
|
terminal.draw(draw)?;
|
|
if let Event::Key(key) = event::read()? {
|
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn draw(frame: &mut Frame) {
|
|
let vertical = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]);
|
|
let [text_area, main_area] = vertical.areas(frame.area());
|
|
frame.render_widget(
|
|
Paragraph::new("Note: not all terminals support all modifiers")
|
|
.style(Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
|
|
text_area,
|
|
);
|
|
let layout = Layout::vertical([Constraint::Length(1); 50])
|
|
.split(main_area)
|
|
.iter()
|
|
.flat_map(|area| {
|
|
Layout::horizontal([Constraint::Percentage(20); 5])
|
|
.split(*area)
|
|
.to_vec()
|
|
})
|
|
.collect_vec();
|
|
|
|
let colors = [
|
|
Color::Black,
|
|
Color::DarkGray,
|
|
Color::Gray,
|
|
Color::White,
|
|
Color::Red,
|
|
];
|
|
let all_modifiers = once(Modifier::empty())
|
|
.chain(Modifier::all().iter())
|
|
.collect_vec();
|
|
let mut index = 0;
|
|
for bg in &colors {
|
|
for fg in &colors {
|
|
for modifier in &all_modifiers {
|
|
let modifier_name = format!("{modifier:11?}");
|
|
let padding = (" ").repeat(12 - modifier_name.len());
|
|
let paragraph = Paragraph::new(Line::from(vec![
|
|
modifier_name.fg(*fg).bg(*bg).add_modifier(*modifier),
|
|
padding.fg(*fg).bg(*bg).add_modifier(*modifier),
|
|
// This is a hack to work around a bug in VHS which is used for rendering the
|
|
// examples to gifs. The bug is that the background color of a paragraph seems
|
|
// to bleed into the next character.
|
|
".".black().on_black(),
|
|
]));
|
|
frame.render_widget(paragraph, layout[index]);
|
|
index += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|