2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Inline 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-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, VecDeque},
|
|
|
|
sync::mpsc,
|
|
|
|
thread,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
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 color_eyre::Result;
|
2023-04-17 12:23:50 +00:00
|
|
|
use rand::distributions::{Distribution, Uniform};
|
2024-05-29 11:42:29 +00: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 12:16:35 +00:00
|
|
|
backend::Backend,
|
|
|
|
crossterm::event,
|
2024-09-20 07:21:26 +00:00
|
|
|
layout::{Constraint, Layout, Rect},
|
2024-05-29 11:42:29 +00:00
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
symbols,
|
|
|
|
text::{Line, Span},
|
2024-09-20 07:21:26 +00:00
|
|
|
widgets::{Block, Gauge, LineGauge, List, ListItem, Paragraph, Widget},
|
2024-08-02 11:18:00 +00:00
|
|
|
Frame, Terminal, TerminalOptions, Viewport,
|
2024-05-29 11:42:29 +00:00
|
|
|
};
|
2023-04-17 12:23:50 +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 main() -> Result<()> {
|
|
|
|
color_eyre::install()?;
|
|
|
|
let mut terminal = ratatui::init_with_options(TerminalOptions {
|
|
|
|
viewport: Viewport::Inline(8),
|
|
|
|
});
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
input_handling(tx.clone());
|
|
|
|
let workers = workers(tx);
|
|
|
|
let mut downloads = downloads();
|
|
|
|
|
|
|
|
for w in &workers {
|
|
|
|
let d = downloads.next(w.id).unwrap();
|
|
|
|
w.tx.send(d).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let app_result = run(&mut terminal, workers, downloads, rx);
|
|
|
|
|
|
|
|
ratatui::restore();
|
|
|
|
|
|
|
|
app_result
|
|
|
|
}
|
|
|
|
|
2023-04-17 12:23:50 +00:00
|
|
|
const NUM_DOWNLOADS: usize = 10;
|
|
|
|
|
|
|
|
type DownloadId = usize;
|
|
|
|
type WorkerId = usize;
|
|
|
|
enum Event {
|
2024-08-11 00:43:13 +00:00
|
|
|
Input(event::KeyEvent),
|
2023-04-17 12:23:50 +00:00
|
|
|
Tick,
|
|
|
|
Resize,
|
|
|
|
DownloadUpdate(WorkerId, DownloadId, f64),
|
|
|
|
DownloadDone(WorkerId, DownloadId),
|
|
|
|
}
|
|
|
|
struct Downloads {
|
|
|
|
pending: VecDeque<Download>,
|
|
|
|
in_progress: BTreeMap<WorkerId, DownloadInProgress>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Downloads {
|
|
|
|
fn next(&mut self, worker_id: WorkerId) -> Option<Download> {
|
|
|
|
match self.pending.pop_front() {
|
|
|
|
Some(d) => {
|
|
|
|
self.in_progress.insert(
|
|
|
|
worker_id,
|
|
|
|
DownloadInProgress {
|
|
|
|
id: d.id,
|
|
|
|
started_at: Instant::now(),
|
|
|
|
progress: 0.0,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Some(d)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct DownloadInProgress {
|
|
|
|
id: DownloadId,
|
|
|
|
started_at: Instant,
|
|
|
|
progress: f64,
|
|
|
|
}
|
|
|
|
struct Download {
|
|
|
|
id: DownloadId,
|
|
|
|
size: usize,
|
|
|
|
}
|
|
|
|
struct Worker {
|
|
|
|
id: WorkerId,
|
|
|
|
tx: mpsc::Sender<Download>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn input_handling(tx: mpsc::Sender<Event>) {
|
|
|
|
let tick_rate = Duration::from_millis(200);
|
|
|
|
thread::spawn(move || {
|
|
|
|
let mut last_tick = Instant::now();
|
|
|
|
loop {
|
|
|
|
// poll for tick rate duration, if no events, sent tick event.
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
2024-08-11 00:43:13 +00:00
|
|
|
if event::poll(timeout).unwrap() {
|
|
|
|
match event::read().unwrap() {
|
|
|
|
event::Event::Key(key) => tx.send(Event::Input(key)).unwrap(),
|
|
|
|
event::Event::Resize(_, _) => tx.send(Event::Resize).unwrap(),
|
2023-04-17 12:23:50 +00:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
tx.send(Event::Tick).unwrap();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_precision_loss, clippy::needless_pass_by_value)]
|
2023-04-17 12:23:50 +00:00
|
|
|
fn workers(tx: mpsc::Sender<Event>) -> Vec<Worker> {
|
|
|
|
(0..4)
|
|
|
|
.map(|id| {
|
|
|
|
let (worker_tx, worker_rx) = mpsc::channel::<Download>();
|
|
|
|
let tx = tx.clone();
|
|
|
|
thread::spawn(move || {
|
|
|
|
while let Ok(download) = worker_rx.recv() {
|
|
|
|
let mut remaining = download.size;
|
|
|
|
while remaining > 0 {
|
|
|
|
let wait = (remaining as u64).min(10);
|
|
|
|
thread::sleep(Duration::from_millis(wait * 10));
|
|
|
|
remaining = remaining.saturating_sub(10);
|
|
|
|
let progress = (download.size - remaining) * 100 / download.size;
|
|
|
|
tx.send(Event::DownloadUpdate(id, download.id, progress as f64))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
tx.send(Event::DownloadDone(id, download.id)).unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Worker { id, tx: worker_tx }
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn downloads() -> Downloads {
|
|
|
|
let distribution = Uniform::new(0, 1000);
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let pending = (0..NUM_DOWNLOADS)
|
|
|
|
.map(|id| {
|
|
|
|
let size = distribution.sample(&mut rng);
|
|
|
|
Download { id, size }
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Downloads {
|
|
|
|
pending,
|
|
|
|
in_progress: BTreeMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
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 run(
|
|
|
|
terminal: &mut Terminal<impl Backend>,
|
2023-04-17 12:23:50 +00:00
|
|
|
workers: Vec<Worker>,
|
|
|
|
mut downloads: Downloads,
|
|
|
|
rx: mpsc::Receiver<Event>,
|
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
|
|
|
) -> Result<()> {
|
2023-04-17 12:23:50 +00:00
|
|
|
let mut redraw = true;
|
|
|
|
loop {
|
|
|
|
if redraw {
|
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
|
|
|
terminal.draw(|frame| draw(frame, &downloads))?;
|
2023-04-17 12:23:50 +00:00
|
|
|
}
|
|
|
|
redraw = true;
|
|
|
|
|
|
|
|
match rx.recv()? {
|
|
|
|
Event::Input(event) => {
|
2024-08-11 00:43:13 +00:00
|
|
|
if event.code == event::KeyCode::Char('q') {
|
2023-04-17 12:23:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Resize => {
|
|
|
|
terminal.autoresize()?;
|
|
|
|
}
|
|
|
|
Event::Tick => {}
|
|
|
|
Event::DownloadUpdate(worker_id, _download_id, progress) => {
|
|
|
|
let download = downloads.in_progress.get_mut(&worker_id).unwrap();
|
|
|
|
download.progress = progress;
|
2024-03-02 09:06:53 +00:00
|
|
|
redraw = false;
|
2023-04-17 12:23:50 +00:00
|
|
|
}
|
|
|
|
Event::DownloadDone(worker_id, download_id) => {
|
|
|
|
let download = downloads.in_progress.remove(&worker_id).unwrap();
|
|
|
|
terminal.insert_before(1, |buf| {
|
2023-05-18 18:21:43 +00:00
|
|
|
Paragraph::new(Line::from(vec![
|
2023-04-17 12:23:50 +00:00
|
|
|
Span::from("Finished "),
|
|
|
|
Span::styled(
|
2023-05-22 03:46:02 +00:00
|
|
|
format!("download {download_id}"),
|
2023-04-17 12:23:50 +00:00
|
|
|
Style::default().add_modifier(Modifier::BOLD),
|
|
|
|
),
|
|
|
|
Span::from(format!(
|
|
|
|
" in {}ms",
|
|
|
|
download.started_at.elapsed().as_millis()
|
|
|
|
)),
|
|
|
|
]))
|
|
|
|
.render(buf.area, buf);
|
|
|
|
})?;
|
|
|
|
match downloads.next(worker_id) {
|
|
|
|
Some(d) => workers[worker_id].tx.send(d).unwrap(),
|
|
|
|
None => {
|
|
|
|
if downloads.in_progress.is_empty() {
|
|
|
|
terminal.insert_before(1, |buf| {
|
|
|
|
Paragraph::new("Done !").render(buf.area, buf);
|
|
|
|
})?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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(frame: &mut Frame, downloads: &Downloads) {
|
|
|
|
let area = frame.area();
|
2023-04-17 12:23:50 +00:00
|
|
|
|
2024-09-20 07:21:26 +00:00
|
|
|
let block = Block::new().title(Line::from("Progress").centered());
|
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);
|
2023-04-17 12:23:50 +00:00
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
let vertical = Layout::vertical([Constraint::Length(2), Constraint::Length(4)]).margin(1);
|
|
|
|
let horizontal = Layout::horizontal([Constraint::Percentage(20), Constraint::Percentage(80)]);
|
2024-02-02 04:26:35 +00:00
|
|
|
let [progress_area, main] = vertical.areas(area);
|
|
|
|
let [list_area, gauge_area] = horizontal.areas(main);
|
2023-04-17 12:23:50 +00:00
|
|
|
|
|
|
|
// total progress
|
|
|
|
let done = NUM_DOWNLOADS - downloads.pending.len() - downloads.in_progress.len();
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_precision_loss)]
|
2023-04-17 12:23:50 +00:00
|
|
|
let progress = LineGauge::default()
|
2024-05-24 18:42:52 +00:00
|
|
|
.filled_style(Style::default().fg(Color::Blue))
|
2023-05-22 03:46:02 +00:00
|
|
|
.label(format!("{done}/{NUM_DOWNLOADS}"))
|
2023-04-17 12:23:50 +00:00
|
|
|
.ratio(done as f64 / NUM_DOWNLOADS as f64);
|
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(progress, progress_area);
|
2023-04-17 12:23:50 +00:00
|
|
|
|
|
|
|
// in progress downloads
|
|
|
|
let items: Vec<ListItem> = downloads
|
|
|
|
.in_progress
|
|
|
|
.values()
|
|
|
|
.map(|download| {
|
2023-05-18 18:21:43 +00:00
|
|
|
ListItem::new(Line::from(vec![
|
2023-04-17 12:23:50 +00:00
|
|
|
Span::raw(symbols::DOT),
|
|
|
|
Span::styled(
|
|
|
|
format!(" download {:>2}", download.id),
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::LightGreen)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
),
|
|
|
|
Span::raw(format!(
|
|
|
|
" ({}ms)",
|
|
|
|
download.started_at.elapsed().as_millis()
|
|
|
|
)),
|
|
|
|
]))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let list = List::new(items);
|
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(list, list_area);
|
2023-04-17 12:23:50 +00:00
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2023-04-17 12:23:50 +00:00
|
|
|
for (i, (_, download)) in downloads.in_progress.iter().enumerate() {
|
|
|
|
let gauge = Gauge::default()
|
|
|
|
.gauge_style(Style::default().fg(Color::Yellow))
|
|
|
|
.ratio(download.progress / 100.0);
|
2024-01-05 15:45:14 +00:00
|
|
|
if gauge_area.top().saturating_add(i as u16) > area.bottom() {
|
2023-04-17 12:23:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
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(
|
2023-04-17 12:23:50 +00:00
|
|
|
gauge,
|
|
|
|
Rect {
|
2024-01-05 15:45:14 +00:00
|
|
|
x: gauge_area.left(),
|
|
|
|
y: gauge_area.top().saturating_add(i as u16),
|
|
|
|
width: gauge_area.width,
|
2023-04-17 12:23:50 +00:00
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|