2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{
|
2024-05-29 11:42:29 +00:00
|
|
|
layout::{Constraint, Layout, Rect},
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
symbols,
|
|
|
|
text::{self, Span},
|
|
|
|
widgets::{
|
|
|
|
canvas::{self, Canvas, Circle, Map, MapResolution, Rectangle},
|
|
|
|
Axis, BarChart, Block, Cell, Chart, Dataset, Gauge, LineGauge, List, ListItem, Paragraph,
|
|
|
|
Row, Sparkline, Table, Tabs, Wrap,
|
|
|
|
},
|
2024-08-02 11:18:00 +00:00
|
|
|
Frame,
|
2023-07-16 09:11:59 +00:00
|
|
|
};
|
2016-10-09 17:46:53 +00:00
|
|
|
|
2023-06-12 05:07:15 +00:00
|
|
|
use crate::app::App;
|
|
|
|
|
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
|
|
|
pub fn draw(frame: &mut Frame, app: &mut App) {
|
|
|
|
let chunks = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]).split(frame.area());
|
2024-01-11 01:16:44 +00:00
|
|
|
let tabs = app
|
2020-05-10 13:44:30 +00:00
|
|
|
.tabs
|
|
|
|
.titles
|
|
|
|
.iter()
|
2023-07-16 09:11:59 +00:00
|
|
|
.map(|t| text::Line::from(Span::styled(*t, Style::default().fg(Color::Green))))
|
2024-01-11 01:16:44 +00:00
|
|
|
.collect::<Tabs>()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title(app.title))
|
2020-07-11 17:08:28 +00:00
|
|
|
.highlight_style(Style::default().fg(Color::Yellow))
|
2019-12-15 20:38:18 +00:00
|
|
|
.select(app.tabs.index);
|
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
|
|
|
frame.render_widget(tabs, chunks[0]);
|
2019-12-15 20:38:18 +00:00
|
|
|
match app.tabs.index {
|
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
|
|
|
0 => draw_first_tab(frame, app, chunks[1]),
|
|
|
|
1 => draw_second_tab(frame, app, chunks[1]),
|
|
|
|
2 => draw_third_tab(frame, app, chunks[1]),
|
2019-12-15 20:38:18 +00:00
|
|
|
_ => {}
|
|
|
|
};
|
2016-10-09 17:46:53 +00: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 12:16:35 +00:00
|
|
|
fn draw_first_tab(frame: &mut Frame, app: &mut App, area: Rect) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::vertical([
|
|
|
|
Constraint::Length(9),
|
|
|
|
Constraint::Min(8),
|
|
|
|
Constraint::Length(7),
|
|
|
|
])
|
|
|
|
.split(area);
|
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
|
|
|
draw_gauges(frame, app, chunks[0]);
|
|
|
|
draw_charts(frame, app, chunks[1]);
|
|
|
|
draw_text(frame, chunks[2]);
|
2017-05-21 09:13:24 +00: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 12:16:35 +00:00
|
|
|
fn draw_gauges(frame: &mut Frame, app: &mut App, area: Rect) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::vertical([
|
|
|
|
Constraint::Length(2),
|
|
|
|
Constraint::Length(3),
|
2024-08-08 03:25:43 +00:00
|
|
|
Constraint::Length(2),
|
2024-01-05 15:45:14 +00:00
|
|
|
])
|
|
|
|
.margin(1)
|
|
|
|
.split(area);
|
2024-05-02 10:09:48 +00:00
|
|
|
let block = Block::bordered().title("Graphs");
|
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
|
|
|
frame.render_widget(block, area);
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2020-04-13 23:04:07 +00:00
|
|
|
let label = format!("{:.2}%", app.progress * 100.0);
|
2019-12-15 20:38:18 +00:00
|
|
|
let gauge = Gauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::new().title("Gauge:"))
|
2020-07-11 17:08:28 +00:00
|
|
|
.gauge_style(
|
2018-08-12 17:44:52 +00:00
|
|
|
Style::default()
|
|
|
|
.fg(Color::Magenta)
|
|
|
|
.bg(Color::Black)
|
2020-07-11 17:08:28 +00:00
|
|
|
.add_modifier(Modifier::ITALIC | Modifier::BOLD),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
2023-06-11 14:23:23 +00:00
|
|
|
.use_unicode(app.enhanced_graphics)
|
2020-05-10 13:44:30 +00:00
|
|
|
.label(label)
|
2020-04-13 23:04:07 +00:00
|
|
|
.ratio(app.progress);
|
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
|
|
|
frame.render_widget(gauge, chunks[0]);
|
2019-12-15 20:38:18 +00:00
|
|
|
|
|
|
|
let sparkline = Sparkline::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::new().title("Sparkline:"))
|
2018-08-12 17:44:52 +00:00
|
|
|
.style(Style::default().fg(Color::Green))
|
2020-04-13 23:04:07 +00:00
|
|
|
.data(&app.sparkline.points)
|
|
|
|
.bar_set(if app.enhanced_graphics {
|
|
|
|
symbols::bar::NINE_LEVELS
|
|
|
|
} else {
|
|
|
|
symbols::bar::THREE_LEVELS
|
|
|
|
});
|
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
|
|
|
frame.render_widget(sparkline, chunks[1]);
|
2020-09-27 22:28:26 +00:00
|
|
|
|
|
|
|
let line_gauge = LineGauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::new().title("LineGauge:"))
|
2024-05-24 18:42:52 +00:00
|
|
|
.filled_style(Style::default().fg(Color::Magenta))
|
2020-09-27 22:28:26 +00:00
|
|
|
.line_set(if app.enhanced_graphics {
|
|
|
|
symbols::line::THICK
|
|
|
|
} else {
|
|
|
|
symbols::line::NORMAL
|
|
|
|
})
|
|
|
|
.ratio(app.progress);
|
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
|
|
|
frame.render_widget(line_gauge, chunks[2]);
|
2017-05-21 09:13:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00: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 12:16:35 +00:00
|
|
|
fn draw_charts(frame: &mut Frame, app: &mut App, area: Rect) {
|
2018-08-12 17:44:52 +00:00
|
|
|
let constraints = if app.show_chart {
|
|
|
|
vec![Constraint::Percentage(50), Constraint::Percentage(50)]
|
2017-05-21 09:13:24 +00:00
|
|
|
} else {
|
2018-08-12 17:44:52 +00:00
|
|
|
vec![Constraint::Percentage(100)]
|
2017-05-21 09:13:24 +00:00
|
|
|
};
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::horizontal(constraints).split(area);
|
2018-08-12 17:44:52 +00:00
|
|
|
{
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)])
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
{
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks =
|
|
|
|
Layout::horizontal([Constraint::Percentage(50), Constraint::Percentage(50)])
|
|
|
|
.split(chunks[0]);
|
2019-12-15 20:38:18 +00:00
|
|
|
|
|
|
|
// Draw tasks
|
2020-05-10 13:44:30 +00:00
|
|
|
let tasks: Vec<ListItem> = app
|
|
|
|
.tasks
|
|
|
|
.items
|
|
|
|
.iter()
|
2023-07-16 09:11:59 +00:00
|
|
|
.map(|i| ListItem::new(vec![text::Line::from(Span::raw(*i))]))
|
2020-05-10 13:44:30 +00:00
|
|
|
.collect();
|
2019-12-15 20:38:18 +00:00
|
|
|
let tasks = List::new(tasks)
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("List"))
|
2020-07-11 17:08:28 +00:00
|
|
|
.highlight_style(Style::default().add_modifier(Modifier::BOLD))
|
2020-04-14 16:02:48 +00:00
|
|
|
.highlight_symbol("> ");
|
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
|
|
|
frame.render_stateful_widget(tasks, chunks[0], &mut app.tasks.state);
|
2019-12-15 20:38:18 +00:00
|
|
|
|
|
|
|
// Draw logs
|
2020-07-11 17:08:28 +00:00
|
|
|
let info_style = Style::default().fg(Color::Blue);
|
|
|
|
let warning_style = Style::default().fg(Color::Yellow);
|
|
|
|
let error_style = Style::default().fg(Color::Magenta);
|
|
|
|
let critical_style = Style::default().fg(Color::Red);
|
2020-05-10 13:44:30 +00:00
|
|
|
let logs: Vec<ListItem> = app
|
|
|
|
.logs
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.map(|&(evt, level)| {
|
|
|
|
let s = match level {
|
2018-09-07 20:24:52 +00:00
|
|
|
"ERROR" => error_style,
|
|
|
|
"CRITICAL" => critical_style,
|
|
|
|
"WARNING" => warning_style,
|
|
|
|
_ => info_style,
|
2020-05-10 13:44:30 +00:00
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
let content = vec![text::Line::from(vec![
|
2023-05-22 03:46:02 +00:00
|
|
|
Span::styled(format!("{level:<9}"), s),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(evt),
|
|
|
|
])];
|
|
|
|
ListItem::new(content)
|
|
|
|
})
|
|
|
|
.collect();
|
2024-05-02 10:09:48 +00:00
|
|
|
let logs = List::new(logs).block(Block::bordered().title("List"));
|
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
|
|
|
frame.render_stateful_widget(logs, chunks[1], &mut app.logs.state);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2019-12-15 20:38:18 +00:00
|
|
|
|
|
|
|
let barchart = BarChart::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Bar chart"))
|
2019-02-10 21:35:44 +00:00
|
|
|
.data(&app.barchart)
|
2018-08-12 17:44:52 +00:00
|
|
|
.bar_width(3)
|
|
|
|
.bar_gap(2)
|
2020-04-13 23:04:07 +00:00
|
|
|
.bar_set(if app.enhanced_graphics {
|
|
|
|
symbols::bar::NINE_LEVELS
|
|
|
|
} else {
|
|
|
|
symbols::bar::THREE_LEVELS
|
|
|
|
})
|
2018-08-12 17:44:52 +00:00
|
|
|
.value_style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Black)
|
|
|
|
.bg(Color::Green)
|
2020-07-11 17:08:28 +00:00
|
|
|
.add_modifier(Modifier::ITALIC),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.label_style(Style::default().fg(Color::Yellow))
|
2020-07-11 17:08:28 +00:00
|
|
|
.bar_style(Style::default().fg(Color::Green));
|
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
|
|
|
frame.render_widget(barchart, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
if app.show_chart {
|
2020-05-10 13:44:30 +00:00
|
|
|
let x_labels = vec![
|
|
|
|
Span::styled(
|
|
|
|
format!("{}", app.signals.window[0]),
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default().add_modifier(Modifier::BOLD),
|
2020-05-10 13:44:30 +00:00
|
|
|
),
|
|
|
|
Span::raw(format!(
|
|
|
|
"{}",
|
|
|
|
(app.signals.window[0] + app.signals.window[1]) / 2.0
|
|
|
|
)),
|
|
|
|
Span::styled(
|
|
|
|
format!("{}", app.signals.window[1]),
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default().add_modifier(Modifier::BOLD),
|
2020-05-10 13:44:30 +00:00
|
|
|
),
|
2019-12-15 20:38:18 +00:00
|
|
|
];
|
2020-05-10 13:44:30 +00:00
|
|
|
let datasets = vec![
|
2019-12-15 20:38:18 +00:00
|
|
|
Dataset::default()
|
|
|
|
.name("data2")
|
2020-04-13 23:04:07 +00:00
|
|
|
.marker(symbols::Marker::Dot)
|
2019-12-15 20:38:18 +00:00
|
|
|
.style(Style::default().fg(Color::Cyan))
|
|
|
|
.data(&app.signals.sin1.points),
|
|
|
|
Dataset::default()
|
|
|
|
.name("data3")
|
2020-04-13 23:04:07 +00:00
|
|
|
.marker(if app.enhanced_graphics {
|
|
|
|
symbols::Marker::Braille
|
|
|
|
} else {
|
|
|
|
symbols::Marker::Dot
|
|
|
|
})
|
2019-12-15 20:38:18 +00:00
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.data(&app.signals.sin2.points),
|
|
|
|
];
|
2020-05-10 13:44:30 +00:00
|
|
|
let chart = Chart::new(datasets)
|
2018-08-12 17:44:52 +00:00
|
|
|
.block(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::bordered().title(Span::styled(
|
|
|
|
"Chart",
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Cyan)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
)),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.x_axis(
|
2018-08-12 17:44:52 +00:00
|
|
|
Axis::default()
|
|
|
|
.title("X Axis")
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
2019-02-10 21:35:44 +00:00
|
|
|
.bounds(app.signals.window)
|
2020-05-10 13:44:30 +00:00
|
|
|
.labels(x_labels),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.y_axis(
|
2018-08-12 17:44:52 +00:00
|
|
|
Axis::default()
|
|
|
|
.title("Y Axis")
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.bounds([-20.0, 20.0])
|
2024-08-06 09:39:44 +00:00
|
|
|
.labels([
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("-20", Style::default().add_modifier(Modifier::BOLD)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw("0"),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("20", Style::default().add_modifier(Modifier::BOLD)),
|
2020-05-10 13:44:30 +00: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 12:16:35 +00:00
|
|
|
frame.render_widget(chart, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
2016-11-07 14:57:46 +00: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 12:16:35 +00:00
|
|
|
fn draw_text(frame: &mut Frame, area: Rect) {
|
2020-05-10 13:44:30 +00:00
|
|
|
let text = vec![
|
2023-07-16 09:11:59 +00:00
|
|
|
text::Line::from("This is a paragraph with several lines. You can change style your text the way you want"),
|
|
|
|
text::Line::from(""),
|
|
|
|
text::Line::from(vec![
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::from("For example: "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("under", Style::default().fg(Color::Red)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(" "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("the", Style::default().fg(Color::Green)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(" "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("rainbow", Style::default().fg(Color::Blue)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw("."),
|
|
|
|
]),
|
2023-07-16 09:11:59 +00:00
|
|
|
text::Line::from(vec![
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw("Oh and if you didn't "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("notice", Style::default().add_modifier(Modifier::ITALIC)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(" you can "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("automatically", Style::default().add_modifier(Modifier::BOLD)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(" "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("wrap", Style::default().add_modifier(Modifier::REVERSED)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(" your "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("text", Style::default().add_modifier(Modifier::UNDERLINED)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::raw(".")
|
|
|
|
]),
|
2023-07-16 09:11:59 +00:00
|
|
|
text::Line::from(
|
2020-05-10 13:44:30 +00:00
|
|
|
"One more thing is that it should display unicode characters: 10€"
|
|
|
|
),
|
2018-08-12 20:13:32 +00:00
|
|
|
];
|
2024-05-02 10:09:48 +00:00
|
|
|
let block = Block::bordered().title(Span::styled(
|
2020-05-10 13:44:30 +00:00
|
|
|
"Footer",
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default()
|
2020-05-10 13:44:30 +00:00
|
|
|
.fg(Color::Magenta)
|
2020-07-11 17:08:28 +00:00
|
|
|
.add_modifier(Modifier::BOLD),
|
2020-05-10 13:44:30 +00:00
|
|
|
));
|
|
|
|
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
|
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
|
|
|
frame.render_widget(paragraph, area);
|
2017-05-21 09:13:24 +00: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 12:16:35 +00:00
|
|
|
fn draw_second_tab(frame: &mut Frame, app: &mut App, area: Rect) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks =
|
|
|
|
Layout::horizontal([Constraint::Percentage(30), Constraint::Percentage(70)]).split(area);
|
2018-08-12 17:44:52 +00:00
|
|
|
let up_style = Style::default().fg(Color::Green);
|
2019-03-10 16:43:56 +00:00
|
|
|
let failure_style = Style::default()
|
|
|
|
.fg(Color::Red)
|
2020-07-11 17:08:28 +00:00
|
|
|
.add_modifier(Modifier::RAPID_BLINK | Modifier::CROSSED_OUT);
|
2018-09-23 18:59:51 +00:00
|
|
|
let rows = app.servers.iter().map(|s| {
|
|
|
|
let style = if s.status == "Up" {
|
|
|
|
up_style
|
|
|
|
} else {
|
|
|
|
failure_style
|
|
|
|
};
|
2020-11-15 21:47:00 +00:00
|
|
|
Row::new(vec![s.name, s.location, s.status]).style(style)
|
2018-09-23 18:59:51 +00:00
|
|
|
});
|
2023-12-04 23:22:52 +00:00
|
|
|
let table = Table::new(
|
|
|
|
rows,
|
|
|
|
[
|
2019-07-10 10:39:10 +00:00
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Length(10),
|
2023-12-04 23:22:52 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
.header(
|
|
|
|
Row::new(vec!["Server", "Location", "Status"])
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.bottom_margin(1),
|
|
|
|
)
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Servers"));
|
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
|
|
|
frame.render_widget(table, chunks[0]);
|
2016-11-07 14:57:46 +00:00
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
let map = Canvas::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("World"))
|
2018-08-12 17:44:52 +00:00
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&Map {
|
|
|
|
color: Color::White,
|
|
|
|
resolution: MapResolution::High,
|
|
|
|
});
|
|
|
|
ctx.layer();
|
2019-01-14 20:14:57 +00:00
|
|
|
ctx.draw(&Rectangle {
|
2020-03-03 08:27:28 +00:00
|
|
|
x: 0.0,
|
|
|
|
y: 30.0,
|
|
|
|
width: 10.0,
|
|
|
|
height: 10.0,
|
2019-01-14 20:14:57 +00:00
|
|
|
color: Color::Yellow,
|
|
|
|
});
|
2023-05-09 17:56:35 +00:00
|
|
|
ctx.draw(&Circle {
|
|
|
|
x: app.servers[2].coords.1,
|
|
|
|
y: app.servers[2].coords.0,
|
|
|
|
radius: 10.0,
|
|
|
|
color: Color::Green,
|
|
|
|
});
|
2018-08-12 17:44:52 +00:00
|
|
|
for (i, s1) in app.servers.iter().enumerate() {
|
|
|
|
for s2 in &app.servers[i + 1..] {
|
2023-07-16 09:11:59 +00:00
|
|
|
ctx.draw(&canvas::Line {
|
2018-08-12 17:44:52 +00:00
|
|
|
x1: s1.coords.1,
|
|
|
|
y1: s1.coords.0,
|
|
|
|
y2: s2.coords.0,
|
|
|
|
x2: s2.coords.1,
|
|
|
|
color: Color::Yellow,
|
2017-09-10 21:03:27 +00:00
|
|
|
});
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for server in &app.servers {
|
|
|
|
let color = if server.status == "Up" {
|
|
|
|
Color::Green
|
|
|
|
} else {
|
|
|
|
Color::Red
|
|
|
|
};
|
2021-10-17 16:44:00 +00:00
|
|
|
ctx.print(
|
|
|
|
server.coords.1,
|
|
|
|
server.coords.0,
|
|
|
|
Span::styled("X", Style::default().fg(color)),
|
|
|
|
);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2018-12-07 15:17:33 +00:00
|
|
|
})
|
2020-04-13 23:04:07 +00:00
|
|
|
.marker(if app.enhanced_graphics {
|
|
|
|
symbols::Marker::Braille
|
|
|
|
} else {
|
|
|
|
symbols::Marker::Dot
|
|
|
|
})
|
2018-12-07 15:17:33 +00:00
|
|
|
.x_bounds([-180.0, 180.0])
|
2019-12-15 20:38:18 +00:00
|
|
|
.y_bounds([-90.0, 90.0]);
|
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
|
|
|
frame.render_widget(map, chunks[1]);
|
2016-11-07 14:57:46 +00:00
|
|
|
}
|
2021-01-01 14:29:04 +00: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 12:16:35 +00:00
|
|
|
fn draw_third_tab(frame: &mut Frame, _app: &mut App, area: Rect) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::horizontal([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)]).split(area);
|
2021-01-01 14:29:04 +00:00
|
|
|
let colors = [
|
|
|
|
Color::Reset,
|
|
|
|
Color::Black,
|
|
|
|
Color::Red,
|
|
|
|
Color::Green,
|
|
|
|
Color::Yellow,
|
|
|
|
Color::Blue,
|
|
|
|
Color::Magenta,
|
|
|
|
Color::Cyan,
|
|
|
|
Color::Gray,
|
|
|
|
Color::DarkGray,
|
|
|
|
Color::LightRed,
|
|
|
|
Color::LightGreen,
|
|
|
|
Color::LightYellow,
|
|
|
|
Color::LightBlue,
|
|
|
|
Color::LightMagenta,
|
|
|
|
Color::LightCyan,
|
|
|
|
Color::White,
|
|
|
|
];
|
|
|
|
let items: Vec<Row> = colors
|
|
|
|
.iter()
|
|
|
|
.map(|c| {
|
|
|
|
let cells = vec![
|
2023-05-22 03:46:02 +00:00
|
|
|
Cell::from(Span::raw(format!("{c:?}: "))),
|
2021-01-01 14:29:04 +00:00
|
|
|
Cell::from(Span::styled("Foreground", Style::default().fg(*c))),
|
|
|
|
Cell::from(Span::styled("Background", Style::default().bg(*c))),
|
|
|
|
];
|
|
|
|
Row::new(cells)
|
|
|
|
})
|
|
|
|
.collect();
|
2023-12-04 23:22:52 +00:00
|
|
|
let table = Table::new(
|
|
|
|
items,
|
|
|
|
[
|
2021-01-01 14:29:04 +00:00
|
|
|
Constraint::Ratio(1, 3),
|
|
|
|
Constraint::Ratio(1, 3),
|
|
|
|
Constraint::Ratio(1, 3),
|
2023-12-04 23:22:52 +00:00
|
|
|
],
|
|
|
|
)
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Colors"));
|
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
|
|
|
frame.render_widget(table, chunks[0]);
|
2021-01-01 14:29:04 +00:00
|
|
|
}
|