2024-01-24 11:50:18 -08:00
|
|
|
//! # [Ratatui] Layout 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.
|
|
|
|
//!
|
2024-08-21 11:35:08 -07:00
|
|
|
//! [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
|
2024-01-24 11:50:18 -08:00
|
|
|
|
2023-08-13 00:38:43 -07:00
|
|
|
use itertools::Itertools;
|
2024-03-02 10:06:53 +01:00
|
|
|
use ratatui::{
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
crossterm::event::{self, Event, KeyCode, KeyEventKind},
|
2024-05-29 04:42:29 -07:00
|
|
|
layout::{
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
Constraint::{self, Length, Max, Min, Percentage, Ratio},
|
2024-05-29 04:42:29 -07:00
|
|
|
Layout, Rect,
|
|
|
|
},
|
|
|
|
style::{Color, Style, Stylize},
|
|
|
|
text::Line,
|
2024-05-02 12:09:48 +02:00
|
|
|
widgets::{Block, Paragraph},
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
DefaultTerminal, Frame,
|
2024-03-02 10:06:53 +01:00
|
|
|
};
|
2018-09-23 20:59:51 +02:00
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
fn main() -> color_eyre::Result<()> {
|
|
|
|
color_eyre::install()?;
|
|
|
|
let terminal = ratatui::init();
|
|
|
|
let app_result = run(terminal);
|
|
|
|
ratatui::restore();
|
|
|
|
app_result
|
2021-11-01 22:27:46 +01:00
|
|
|
}
|
2018-04-01 17:57:26 +02:00
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
|
2018-04-01 17:57:26 +02:00
|
|
|
loop {
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
terminal.draw(draw)?;
|
2021-11-01 22:27:46 +01:00
|
|
|
if let Event::Key(key) = event::read()? {
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
|
|
|
break Ok(());
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-04-01 17:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 22:27:46 +01:00
|
|
|
}
|
2018-04-01 17:57:26 +02:00
|
|
|
|
2024-03-02 10:06:53 +01:00
|
|
|
#[allow(clippy::too_many_lines)]
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 05:16:35 -07:00
|
|
|
fn draw(frame: &mut Frame) {
|
2024-01-05 07:45:14 -08:00
|
|
|
let vertical = Layout::vertical([
|
|
|
|
Length(4), // text
|
|
|
|
Length(50), // examples
|
|
|
|
Min(0), // fills remaining space
|
|
|
|
]);
|
2024-08-06 05:15:14 +02:00
|
|
|
let [text_area, examples_area, _] = vertical.areas(frame.area());
|
2021-11-01 22:27:46 +01:00
|
|
|
|
2023-08-13 00:38:43 -07:00
|
|
|
// title
|
2023-07-24 12:05:37 -07:00
|
|
|
frame.render_widget(
|
2023-08-13 00:38:43 -07:00
|
|
|
Paragraph::new(vec![
|
2024-01-22 18:30:01 +01:00
|
|
|
Line::from("Horizontal Layout Example. Press q to quit".dark_gray()).centered(),
|
2023-08-13 00:38:43 -07:00
|
|
|
Line::from("Each line has 2 constraints, plus Min(0) to fill the remaining space."),
|
|
|
|
Line::from("E.g. the second line of the Len/Min box is [Length(2), Min(2), Min(0)]"),
|
|
|
|
Line::from("Note: constraint labels that don't fit are truncated"),
|
|
|
|
]),
|
2024-01-05 07:45:14 -08:00
|
|
|
text_area,
|
2023-07-24 12:05:37 -07:00
|
|
|
);
|
|
|
|
|
2024-01-05 07:45:14 -08:00
|
|
|
let example_rows = Layout::vertical([
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Min(0), // fills remaining space
|
|
|
|
])
|
|
|
|
.split(examples_area);
|
2024-08-26 22:59:30 +02:00
|
|
|
let example_areas = example_rows.iter().flat_map(|area| {
|
|
|
|
Layout::horizontal([
|
|
|
|
Length(14),
|
|
|
|
Length(14),
|
|
|
|
Length(14),
|
|
|
|
Length(14),
|
|
|
|
Length(14),
|
|
|
|
Min(0), // fills remaining space
|
|
|
|
])
|
|
|
|
.split(*area)
|
2023-08-13 00:38:43 -07:00
|
|
|
.iter()
|
2024-08-26 22:59:30 +02:00
|
|
|
.copied()
|
|
|
|
.take(5) // ignore Min(0)
|
|
|
|
.collect_vec()
|
|
|
|
});
|
2023-08-13 00:38:43 -07:00
|
|
|
|
|
|
|
// the examples are a cartesian product of the following constraints
|
|
|
|
// e.g. Len/Len, Len/Min, Len/Max, Len/Perc, Len/Ratio, Min/Len, Min/Min, ...
|
|
|
|
let examples = [
|
|
|
|
(
|
|
|
|
"Len",
|
2024-08-26 22:59:30 +02:00
|
|
|
[
|
2023-08-13 00:38:43 -07:00
|
|
|
Length(0),
|
|
|
|
Length(2),
|
|
|
|
Length(3),
|
|
|
|
Length(6),
|
|
|
|
Length(10),
|
|
|
|
Length(15),
|
|
|
|
],
|
|
|
|
),
|
2024-08-26 22:59:30 +02:00
|
|
|
("Min", [Min(0), Min(2), Min(3), Min(6), Min(10), Min(15)]),
|
|
|
|
("Max", [Max(0), Max(2), Max(3), Max(6), Max(10), Max(15)]),
|
2023-08-13 00:38:43 -07:00
|
|
|
(
|
|
|
|
"Perc",
|
2024-08-26 22:59:30 +02:00
|
|
|
[
|
2023-08-13 00:38:43 -07:00
|
|
|
Percentage(0),
|
|
|
|
Percentage(25),
|
|
|
|
Percentage(50),
|
|
|
|
Percentage(75),
|
|
|
|
Percentage(100),
|
|
|
|
Percentage(150),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Ratio",
|
2024-08-26 22:59:30 +02:00
|
|
|
[
|
2023-08-13 00:38:43 -07:00
|
|
|
Ratio(0, 4),
|
|
|
|
Ratio(1, 4),
|
|
|
|
Ratio(2, 4),
|
|
|
|
Ratio(3, 4),
|
|
|
|
Ratio(4, 4),
|
|
|
|
Ratio(6, 4),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2024-08-26 22:59:30 +02:00
|
|
|
for ((a, b), area) in examples
|
2023-08-13 00:38:43 -07:00
|
|
|
.iter()
|
|
|
|
.cartesian_product(examples.iter())
|
2024-08-26 22:59:30 +02:00
|
|
|
.zip(example_areas)
|
2023-08-13 00:38:43 -07:00
|
|
|
{
|
|
|
|
let (name_a, examples_a) = a;
|
|
|
|
let (name_b, examples_b) = b;
|
2024-08-26 22:59:30 +02:00
|
|
|
let constraints = examples_a.iter().copied().zip(examples_b.iter().copied());
|
|
|
|
render_example_combination(frame, area, &format!("{name_a}/{name_b}"), constraints);
|
2023-08-13 00:38:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders a single example box
|
2023-09-25 22:30:36 -07:00
|
|
|
fn render_example_combination(
|
|
|
|
frame: &mut Frame,
|
2023-08-13 00:38:43 -07:00
|
|
|
area: Rect,
|
|
|
|
title: &str,
|
2024-08-26 22:59:30 +02:00
|
|
|
constraints: impl ExactSizeIterator<Item = (Constraint, Constraint)>,
|
2023-08-13 00:38:43 -07:00
|
|
|
) {
|
2024-05-02 12:09:48 +02:00
|
|
|
let block = Block::bordered()
|
2023-08-13 00:38:43 -07:00
|
|
|
.title(title.gray())
|
|
|
|
.style(Style::reset())
|
|
|
|
.border_style(Style::default().fg(Color::DarkGray));
|
|
|
|
let inner = block.inner(area);
|
|
|
|
frame.render_widget(block, area);
|
2024-01-05 07:45:14 -08:00
|
|
|
let layout = Layout::vertical(vec![Length(1); constraints.len() + 1]).split(inner);
|
2024-08-26 22:59:30 +02:00
|
|
|
for ((a, b), &area) in constraints.into_iter().zip(layout.iter()) {
|
|
|
|
render_single_example(frame, area, vec![a, b, Min(0)]);
|
2023-08-13 00:38:43 -07:00
|
|
|
}
|
|
|
|
// This is to make it easy to visually see the alignment of the examples
|
|
|
|
// with the constraints.
|
|
|
|
frame.render_widget(Paragraph::new("123456789012"), layout[6]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders a single example line
|
2023-09-25 22:30:36 -07:00
|
|
|
fn render_single_example(frame: &mut Frame, area: Rect, constraints: Vec<Constraint>) {
|
2023-08-13 00:38:43 -07:00
|
|
|
let red = Paragraph::new(constraint_label(constraints[0])).on_red();
|
|
|
|
let blue = Paragraph::new(constraint_label(constraints[1])).on_blue();
|
|
|
|
let green = Paragraph::new("·".repeat(12)).on_green();
|
2024-01-05 07:45:14 -08:00
|
|
|
let horizontal = Layout::horizontal(constraints);
|
2024-02-01 20:26:35 -08:00
|
|
|
let [r, b, g] = horizontal.areas(area);
|
2024-01-05 07:45:14 -08:00
|
|
|
frame.render_widget(red, r);
|
|
|
|
frame.render_widget(blue, b);
|
|
|
|
frame.render_widget(green, g);
|
2023-08-13 00:38:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn constraint_label(constraint: Constraint) -> String {
|
|
|
|
match constraint {
|
2024-03-02 10:06:53 +01:00
|
|
|
Constraint::Ratio(a, b) => format!("{a}:{b}"),
|
|
|
|
Constraint::Length(n)
|
|
|
|
| Constraint::Min(n)
|
|
|
|
| Constraint::Max(n)
|
|
|
|
| Constraint::Percentage(n)
|
|
|
|
| Constraint::Fill(n) => format!("{n}"),
|
2023-08-13 00:38:43 -07:00
|
|
|
}
|
2018-04-01 17:57:26 +02:00
|
|
|
}
|