Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tui-big-text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ derive_builder.workspace = true
font8x8 = { version = "0.3.1", default-features = false, features = ["unicode"] }
itertools.workspace = true
ratatui-core.workspace = true
ratatui-widgets.workspace = true

[dev-dependencies]
color-eyre.workspace = true
Expand Down
35 changes: 34 additions & 1 deletion tui-big-text/src/big_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ratatui_core::layout::{Alignment, Rect};
use ratatui_core::style::Style;
use ratatui_core::text::{Line, StyledGrapheme};
use ratatui_core::widgets::Widget;
use ratatui_widgets::block::{Block, BlockExt};

use crate::PixelSize;

Expand Down Expand Up @@ -84,6 +85,10 @@ pub struct BigText<'a> {
/// Defaults to `Alignment::default()` (=> Alignment::Left)
#[builder(default)]
pub alignment: Alignment,

/// Block to wrap the widget in
#[builder(default, setter(into, strip_option))]
pub block: Option<Block<'a>>,
}

impl BigText<'static> {
Expand Down Expand Up @@ -118,13 +123,17 @@ impl<'a> BigTextBuilder<'a> {
style: self.style.unwrap_or_default(),
pixel_size: self.pixel_size.unwrap_or_default(),
alignment: self.alignment.unwrap_or_default(),
block: self.block.as_ref().cloned().unwrap_or_default(),
}
}
}

impl Widget for BigText<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let layout = layout(area, &self.pixel_size, self.alignment, &self.lines);
self.block.as_ref().render(area, buf);
let inner = self.block.inner_if_some(area);

let layout = layout(inner, &self.pixel_size, self.alignment, &self.lines);
for (line, line_layout) in self.lines.iter().zip(layout) {
for (g, cell) in line.styled_graphemes(self.style).zip(line_layout) {
render_symbol(g, cell, buf, &self.pixel_size);
Expand Down Expand Up @@ -223,6 +232,7 @@ mod tests {
style,
pixel_size,
alignment,
block: None,
}
);
}
Expand Down Expand Up @@ -1135,4 +1145,27 @@ mod tests {
]);
assert_eq!(buf, expected);
}

#[test]
fn render_with_block() {
let big_text = BigText::builder()
.lines(vec![Line::from("In Block")])
.block(Block::bordered().title("Big Text Block"))
.build();
let mut buf = Buffer::empty(Rect::new(0, 0, 65, 10));
big_text.render(buf.area, &mut buf);
let expected = Buffer::with_lines(vec![
"┌Big Text Block─────────────────────────────────────────────────┐",
"│ ████ ██████ ███ ███ │",
"│ ██ ██ ██ ██ ██ │",
"│ ██ █████ ██ ██ ██ ████ ████ ██ ██│",
"│ ██ ██ ██ █████ ██ ██ ██ ██ ██ ██ ██ │",
"│ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ │",
"│ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ │",
"│ ████ ██ ██ ██████ ████ ████ ████ ███ ██│",
"│ │",
"└───────────────────────────────────────────────────────────────┘",
]);
assert_eq!(buf, expected);
}
}