Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic in insert_before if viewport fills entire screen #999

Open
chris-olszewski opened this issue Mar 25, 2024 · 2 comments
Open

Panic in insert_before if viewport fills entire screen #999

chris-olszewski opened this issue Mar 25, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@chris-olszewski
Copy link

chris-olszewski commented Mar 25, 2024

Description

It's possible to cause a panic with an Inline viewport if it is the size of the screen. If this happens, then max_chunk_size will end up being 0 causing chunks to panic.

To Reproduce

use ratatui::{
    backend::{Backend, CrosstermBackend},
    text::Text,
    widgets::Widget,
    Terminal, TerminalOptions,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    crossterm::terminal::enable_raw_mode()?;

    let backend = CrosstermBackend::new(std::io::stdout());
    let size = backend.size()?;
    let mut terminal = Terminal::with_options(
        backend,
        TerminalOptions {
            viewport: ratatui::Viewport::Inline(size.height),
        },
    )?;
    terminal.insert_before(1, |buf| Text::raw("Hello World!").render(buf.area, buf))?;

    crossterm::terminal::disable_raw_mode()?;
    Ok(())
}

Expected behavior

Either:

  • Write to a line in the scrollback
  • Error instead of panic

Environment

  • OS: macOS
  • Terminal Emulator: iTerm2
  • Font: N/A
  • Crate version: 0.26.1
  • Backend: Crossterm
@chris-olszewski chris-olszewski added the bug Something isn't working label Mar 25, 2024
chris-olszewski added a commit to vercel/turbo that referenced this issue Mar 25, 2024
### Description

With #7822 (and #7805 ) we're using `insert_before` to persist logs.
This can panic (ratatui-org/ratatui#999) if
the viewport fills the entire terminal.

This PR changes our viewport construction so it now takes terminal size
into account. (Previously we always would use 60, even if the terminal
didn't have 60 rows)

### Testing Instructions

Use UI with a terminal that has a height of 60 or less.


Closes TURBO-2695
@joshka
Copy link
Member

joshka commented Mar 25, 2024

Unfortunately we cant' change things easily to return errors as there are lots of apps that this would break, but we can add new methods that do this (and it's one of those things we've spoken about a few times), so the first option is really orthogonal to this problem. Ideally we should never panic for things that are expected (and sometimes even for things which can be handled by ignoring the problem).

Here, I think this probably should succeed and write the line above the viewport.

Would you consider submitting a PR to fix this?

I'd suggest adding a unit / integration test if possible that shows this panicking and then make that unit test succeed.

@chris-olszewski
Copy link
Author

Would you consider submitting a PR to fix this?

Yes! Thanks for the guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants