ratatui icon indicating copy to clipboard operation
ratatui copied to clipboard

Paragraph panics when drawn outside of frame

Open jwodder opened this issue 1 year ago • 2 comments

The following code:

use ratatui::{
    crossterm::event::{self, Event, KeyEvent},
    layout::Rect,
    widgets::Paragraph,
    DefaultTerminal,
};

fn main() -> std::io::Result<()> {
    let terminal = ratatui::init();
    let r = run(terminal);
    ratatui::restore();
    r
}

fn run(mut terminal: DefaultTerminal) -> std::io::Result<()> {
    loop {
        terminal.draw(|frame| {
            frame.render_widget(
                Paragraph::new("Beyond the pale"),
                Rect {
                    x: 200,
                    y: 200,
                    width: 10,
                    height: 10,
                },
            );
        })?;
        if let Event::Key(KeyEvent { .. }) = event::read()? {
            break;
        }
    }
    Ok(())
}

when run on an insufficiently large terminal panics with:

thread 'main' panicked at /Users/jwodder/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ratatui-core-0.1.0-alpha.2/src/buffer/buffer.rs:253:13:
index outside of buffer: the area is Rect { x: 0, y: 0, width: 203, height: 62 } but index is (200, 200)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

If Text or Line is used instead of Paragraph, then the program draws nothing instead of panicking, which is what I believe should happen. I have not checked any other widgets.

Environment

  • OS: macOS 14.7.2
  • Terminal Emulator: wezterm 20240203-110809-5046fc22
  • Font: —
  • Crate version: both 0.29.0 and 0.30.0-alpha.1
  • Backend: crossterm 0.28.1

jwodder avatar Feb 10 '25 18:02 jwodder

Thanks for the bug report, would you consider submitting a PR to fix this?

joshka avatar Feb 11 '25 06:02 joshka

@joshka PR to fix this: https://github.com/ratatui/ratatui/pull/1670

jwodder avatar Feb 11 '25 14:02 jwodder

This one should probably be finished up for 0.30. I need to take another look at the PR and get back up to speed on it.

joshka avatar May 29 '25 19:05 joshka