2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Logo 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 18:35:08 +00: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 19:50:18 +00:00
|
|
|
|
2023-12-14 02:25:21 +00:00
|
|
|
use std::{
|
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 12:16:35 +00:00
|
|
|
io::{self},
|
2023-12-14 02:25:21 +00:00
|
|
|
thread::sleep,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use indoc::indoc;
|
|
|
|
use itertools::izip;
|
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 12:16:35 +00:00
|
|
|
use ratatui::{widgets::Paragraph, TerminalOptions, Viewport};
|
2023-12-14 02:25:21 +00:00
|
|
|
|
|
|
|
/// A fun example of using half block characters to draw a logo
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::many_single_char_names)]
|
|
|
|
fn logo() -> String {
|
2023-12-14 02:25:21 +00:00
|
|
|
let r = indoc! {"
|
|
|
|
▄▄▄
|
|
|
|
█▄▄▀
|
|
|
|
█ █
|
2024-03-02 09:06:53 +00:00
|
|
|
"};
|
2023-12-14 02:25:21 +00:00
|
|
|
let a = indoc! {"
|
|
|
|
▄▄
|
|
|
|
█▄▄█
|
|
|
|
█ █
|
2024-03-02 09:06:53 +00:00
|
|
|
"};
|
2023-12-14 02:25:21 +00:00
|
|
|
let t = indoc! {"
|
|
|
|
▄▄▄
|
|
|
|
█
|
|
|
|
█
|
2024-03-02 09:06:53 +00:00
|
|
|
"};
|
2023-12-14 02:25:21 +00:00
|
|
|
let u = indoc! {"
|
|
|
|
▄ ▄
|
|
|
|
█ █
|
|
|
|
▀▄▄▀
|
2024-03-02 09:06:53 +00:00
|
|
|
"};
|
2023-12-14 02:25:21 +00:00
|
|
|
let i = indoc! {"
|
|
|
|
▄
|
|
|
|
█
|
|
|
|
█
|
2024-03-02 09:06:53 +00:00
|
|
|
"};
|
|
|
|
izip!(r.lines(), a.lines(), t.lines(), u.lines(), i.lines())
|
|
|
|
.map(|(r, a, t, u, i)| format!("{r:5}{a:5}{t:4}{a:5}{t:4}{u:5}{i:5}"))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
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 12:16:35 +00:00
|
|
|
let mut terminal = ratatui::init_with_options(TerminalOptions {
|
|
|
|
viewport: Viewport::Inline(3),
|
|
|
|
});
|
2024-08-06 03:15:14 +00:00
|
|
|
terminal.draw(|frame| frame.render_widget(Paragraph::new(logo()), frame.area()))?;
|
2023-12-14 02:25:21 +00:00
|
|
|
sleep(Duration::from_secs(5));
|
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 12:16:35 +00:00
|
|
|
ratatui::restore();
|
2023-12-14 02:25:21 +00:00
|
|
|
println!();
|
|
|
|
Ok(())
|
|
|
|
}
|