2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Paragraph example
|
|
|
|
//!
|
|
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
|
|
//!
|
|
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
|
|
//! crates.io, or the one that you have installed locally.
|
|
|
|
//!
|
|
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
|
|
//! library you are using.
|
|
|
|
//!
|
|
|
|
//! [Ratatui]: https://github.com/ratatui-org/ratatui
|
|
|
|
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
|
|
|
|
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
|
|
|
|
|
2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2024-03-02 09:06:53 +00:00
|
|
|
use ratatui::{
|
|
|
|
prelude::*,
|
|
|
|
widgets::{Block, Borders, Paragraph, Wrap},
|
|
|
|
};
|
2017-05-08 19:34:41 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
struct App {
|
|
|
|
scroll: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
2024-03-02 09:06:53 +00:00
|
|
|
const fn new() -> Self {
|
|
|
|
Self { scroll: 0 }
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_tick(&mut self) {
|
|
|
|
self.scroll += 1;
|
|
|
|
self.scroll %= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let tick_rate = Duration::from_millis(250);
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
2021-11-01 21:27:46 +00:00
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2024-03-02 09:06:53 +00:00
|
|
|
if key.code == KeyCode::Char('q') {
|
2021-12-23 17:46:25 +00:00
|
|
|
return Ok(());
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
2021-11-01 21:27:46 +00:00
|
|
|
let size = f.size();
|
|
|
|
|
|
|
|
// Words made "loooong" to demonstrate line breaking.
|
|
|
|
let s = "Veeeeeeeeeeeeeeeery loooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiing. ";
|
|
|
|
let mut long_line = s.repeat(usize::from(size.width) / s.len() + 4);
|
|
|
|
long_line.push('\n');
|
|
|
|
|
2023-07-01 09:14:16 +00:00
|
|
|
let block = Block::default().black();
|
2021-11-01 21:27:46 +00:00
|
|
|
f.render_widget(block, size);
|
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
let layout = Layout::vertical([Constraint::Ratio(1, 4); 4]).split(size);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
let text = vec![
|
2023-05-18 18:21:43 +00:00
|
|
|
Line::from("This is a line "),
|
2023-07-01 09:14:16 +00:00
|
|
|
Line::from("This is a line ".red()),
|
|
|
|
Line::from("This is a line".on_blue()),
|
|
|
|
Line::from("This is a longer line".crossed_out()),
|
|
|
|
Line::from(long_line.on_green()),
|
|
|
|
Line::from("This is a line".green().italic()),
|
2023-05-18 18:21:43 +00:00
|
|
|
Line::from(vec![
|
2023-07-01 09:14:16 +00:00
|
|
|
"Masked text: ".into(),
|
2023-05-09 17:59:24 +00:00
|
|
|
Span::styled(
|
|
|
|
Masked::new("password", '*'),
|
|
|
|
Style::default().fg(Color::Red),
|
|
|
|
),
|
|
|
|
]),
|
2021-11-01 21:27:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let create_block = |title| {
|
|
|
|
Block::default()
|
|
|
|
.borders(Borders::ALL)
|
2023-04-27 10:06:45 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
2021-11-01 21:27:46 +00:00
|
|
|
.title(Span::styled(
|
|
|
|
title,
|
|
|
|
Style::default().add_modifier(Modifier::BOLD),
|
|
|
|
))
|
|
|
|
};
|
2023-04-27 10:06:45 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-04-27 10:06:45 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.block(create_block("Default alignment (Left), no wrap"));
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(paragraph, layout[0]);
|
2023-04-27 10:06:45 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-04-27 10:06:45 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.block(create_block("Default alignment (Left), with wrap"))
|
2021-11-01 21:27:46 +00:00
|
|
|
.wrap(Wrap { trim: true });
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(paragraph, layout[1]);
|
2023-04-27 10:06:45 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2023-04-27 10:06:45 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.block(create_block("Right alignment, with wrap"))
|
2024-01-24 11:31:52 +00:00
|
|
|
.right_aligned()
|
2023-04-27 10:06:45 +00:00
|
|
|
.wrap(Wrap { trim: true });
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(paragraph, layout[2]);
|
2023-04-27 10:06:45 +00:00
|
|
|
|
|
|
|
let paragraph = Paragraph::new(text)
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.block(create_block("Center alignment, with wrap, with scroll"))
|
2024-01-24 11:31:52 +00:00
|
|
|
.centered()
|
2021-11-01 21:27:46 +00:00
|
|
|
.wrap(Wrap { trim: true })
|
|
|
|
.scroll((app.scroll, 0));
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(paragraph, layout[3]);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|