Skip to main content

popup/
popup.rs

1//! Renders a basic centered popup over wrapped background text.
2//!
3//! Run with `cargo run -p tui-popup --example popup --features crossterm`.
4//!
5//! This is the minimal popup example: `Popup::new` receives plain text, centers it in the frame,
6//! and lets the popup size itself from the content.
7//!
8//! Press any key to quit.
9
10use color_eyre::Result;
11use lipsum::lipsum;
12use ratatui::crossterm::event;
13use ratatui::prelude::{Rect, Style, Stylize};
14use ratatui::widgets::{Paragraph, Wrap};
15use ratatui::{DefaultTerminal, Frame};
16use tui_popup::Popup;
17
18fn main() -> Result<()> {
19    color_eyre::install()?;
20    ratatui::run(run)
21}
22
23fn run(terminal: &mut DefaultTerminal) -> Result<()> {
24    loop {
25        terminal.draw(|frame| {
26            render(frame);
27        })?;
28        if event::read()?.as_key_press_event().is_some() {
29            break Ok(());
30        }
31    }
32}
33
34fn render(frame: &mut Frame) {
35    let area = frame.area();
36    let background = background(area);
37    let popup = Popup::new("Press any key to exit")
38        .title("tui-popup demo")
39        .style(Style::new().white().on_blue());
40    frame.render_widget(background, area);
41    frame.render_widget(popup, area);
42}
43
44fn background(area: Rect) -> Paragraph<'static> {
45    let lorem_ipsum = lipsum(area.area() as usize / 5);
46    Paragraph::new(lorem_ipsum)
47        .wrap(Wrap { trim: false })
48        .dark_gray()
49}