Skip to content

Commit fd88898

Browse files
committed
feat(tui-big-text): add alignment helper methods
Adds helper methods to the `BigTextBuilder` struct to set the alignment of the text. This makes it simpler to set the alignment of the text. ```rust let left = BigText::builder() .left_aligned() .lines(vec!["Left".white().into()]) .build()?; let right = BigText::builder() .right_aligned() .lines(vec!["Right".green().into()]) .build()?; let centered = BigText::builder() .centered() .lines(vec!["Centered".red().into()]) .build()?; ```
1 parent dcb65cb commit fd88898

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

tui-big-text/examples/alignment.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use color_eyre::Result;
22
use ratatui::{
33
layout::Offset,
4-
prelude::{Alignment, Frame, Stylize},
4+
prelude::{Frame, Stylize},
55
text::Line,
66
};
77
use tui_big_text::{BigText, PixelSize};
@@ -21,19 +21,19 @@ fn render(frame: &mut Frame) -> Result<()> {
2121

2222
let left = BigText::builder()
2323
.pixel_size(PixelSize::Quadrant)
24-
.alignment(Alignment::Left)
24+
.left_aligned()
2525
.lines(vec!["Left".white().into()])
2626
.build()?;
2727

2828
let right = BigText::builder()
2929
.pixel_size(PixelSize::Quadrant)
30-
.alignment(Alignment::Right)
30+
.right_aligned()
3131
.lines(vec!["Right".green().into()])
3232
.build()?;
3333

3434
let centered = BigText::builder()
3535
.pixel_size(PixelSize::Quadrant)
36-
.alignment(Alignment::Center)
36+
.centered()
3737
.lines(vec!["Centered".red().into()])
3838
.build()?;
3939

tui-big-text/src/big_text.rs

+17
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ impl BigText<'static> {
8585
}
8686
}
8787

88+
impl BigTextBuilder<'_> {
89+
/// Set the alignment of the text.
90+
pub fn left_aligned(&mut self) -> &mut Self {
91+
self.alignment(Alignment::Left)
92+
}
93+
94+
/// Set the alignment of the text.
95+
pub fn right_aligned(&mut self) -> &mut Self {
96+
self.alignment(Alignment::Right)
97+
}
98+
99+
/// Set the alignment of the text.
100+
pub fn centered(&mut self) -> &mut Self {
101+
self.alignment(Alignment::Center)
102+
}
103+
}
104+
88105
impl Widget for BigText<'_> {
89106
fn render(self, area: Rect, buf: &mut Buffer) {
90107
let layout = layout(area, &self.pixel_size, self.alignment, &self.lines);

0 commit comments

Comments
 (0)