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

Crash when rendering a block with an empty title with alignment right #929

Closed
kdheepak opened this issue Feb 6, 2024 · 1 comment · Fixed by #933
Closed

Crash when rendering a block with an empty title with alignment right #929

kdheepak opened this issue Feb 6, 2024 · 1 comment · Fixed by #933
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@kdheepak
Copy link
Collaborator

kdheepak commented Feb 6, 2024

When rendering a block with a empty title with alignment right, I get a crash. Here's a minimal test:

    #[test]
    fn render_crash() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 15, 3));
        Block::default()
            .title("")
            // .title_position(crate::widgets::block::Position::Bottom)
            .title_alignment(crate::layout::Alignment::Right)
            .render(buffer.area, &mut buffer);
        assert_buffer_eq!(
            buffer,
            Buffer::with_lines(vec![
                "               ",
                "               ",
                "               ",
            ])
        );
    }

Here's the stacktrace from the test:

running 1 test
thread 'widgets::block::tests::render_crash' panicked at src/buffer/buffer.rs:138:9:
Trying to access position outside the buffer: x=15, y=0, area=Rect { x: 0, y: 0, width: 15, height: 3 }
stack backtrace:
   0: rust_begin_unwind
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:72:14
   2: ratatui::buffer::buffer::Buffer::index_of
             at ./src/buffer/buffer.rs:138:9
   3: ratatui::buffer::buffer::Buffer::set_stringn
             at ./src/buffer/buffer.rs:216:25
   4: ratatui::buffer::buffer::Buffer::set_line
             at ./src/buffer/buffer.rs:251:23
   5: ratatui::widgets::block::Block::render_right_titles::{{closure}}
             at ./src/widgets/block.rs:697:17
   6: core::iter::traits::iterator::Iterator::for_each::call::{{closure}}
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/iter/traits/iterator.rs:855:29
   7: core::iter::adapters::filter::filter_fold::{{closure}}
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/iter/adapters/filter.rs:40:44
   8: core::iter::traits::double_ended::DoubleEndedIterator::rfold
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/iter/traits/double_ended.rs:307:21
   9: <core::iter::adapters::filter::Filter<I,P> as core::iter::traits::double_ended::DoubleEndedIterator>::rfold
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/iter/adapters/filter.rs:185:9
  10: <core::iter::adapters::rev::Rev<I> as core::iter::traits::iterator::Iterator>::fold
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/iter/adapters/rev.rs:64:9
  11: core::iter::traits::iterator::Iterator::for_each
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/iter/traits/iterator.rs:858:9
  12: ratatui::widgets::block::Block::render_right_titles
             at ./src/widgets/block.rs:683:9
  13: ratatui::widgets::block::Block::render_title_position
             at ./src/widgets/block.rs:708:9
  14: ratatui::widgets::block::Block::render_titles
             at ./src/widgets/block.rs:714:9
  15: <ratatui::widgets::block::Block as ratatui::widgets::WidgetRef>::render_ref
             at ./src/widgets/block.rs:532:9
  16: <ratatui::widgets::block::Block as ratatui::widgets::Widget>::render
             at ./src/widgets/block.rs:522:9
  17: ratatui::widgets::block::tests::render_crash
             at ./src/widgets/block.rs:1351:9
  18: ratatui::widgets::block::tests::render_crash::{{closure}}
             at ./src/widgets/block.rs:1349:22
  19: core::ops::function::FnOnce::call_once
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
  20: core::ops::function::FnOnce::call_once
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test widgets::block::tests::render_crash ... FAILED

failures:

failures:
    widgets::block::tests::render_crash

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 1332 filtered out; finished in 0.08s
@kdheepak kdheepak added bug Something isn't working good first issue Good for newcomers labels Feb 6, 2024
@joshka
Copy link
Member

joshka commented Feb 6, 2024

Rewriting the block implementation is next up on my todo list.

@Valentin271 Valentin271 linked a pull request Feb 7, 2024 that will close this issue
joshka added a commit that referenced this issue Feb 7, 2024
- Simplified implementation of the rendering for block.
- Introduces a subtle rendering change where centered titles that are
  odd in length will now be rendered one character to the left compared
  to before. This aligns with other places that we render centered text
  and is a more consistent behavior. See
  #807 (comment)
  for another example of this.

Fixes: #929
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants