mirror of
https://github.com/ratatui-org/ratatui
synced 2025-02-16 14:08:44 +00:00
feat(table): add support for line alignment in the table widget (#392)
* feat(table): enforce line alignment in table render * test(table): add table alignment render test
This commit is contained in:
parent
4d70169bef
commit
7748720963
1 changed files with 34 additions and 3 deletions
|
@ -2,7 +2,7 @@ use unicode_width::UnicodeWidthStr;
|
|||
|
||||
use crate::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::{Style, Styled},
|
||||
text::Text,
|
||||
widgets::{Block, StatefulWidget, Widget},
|
||||
|
@ -569,7 +569,14 @@ fn render_cell(buf: &mut Buffer, cell: &Cell, area: Rect) {
|
|||
if i as u16 >= area.height {
|
||||
break;
|
||||
}
|
||||
buf.set_line(area.x, area.y + i as u16, line, area.width);
|
||||
|
||||
let x_offset = match line.alignment {
|
||||
Some(Alignment::Center) => (area.width / 2).saturating_sub(line.width() as u16 / 2),
|
||||
Some(Alignment::Right) => area.width.saturating_sub(line.width() as u16),
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
buf.set_line(area.x + x_offset, area.y + i as u16, line, area.width);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -585,13 +592,37 @@ mod tests {
|
|||
use std::vec;
|
||||
|
||||
use super::*;
|
||||
use crate::style::{Color, Modifier, Style, Stylize};
|
||||
use crate::{
|
||||
style::{Color, Modifier, Style, Stylize},
|
||||
text::Line,
|
||||
};
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn table_invalid_percentages() {
|
||||
Table::new(vec![]).widths(&[Constraint::Percentage(110)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_table_with_alignment() {
|
||||
let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));
|
||||
let table = Table::new(vec![
|
||||
Row::new(vec![Line::from("Left").alignment(Alignment::Left)]),
|
||||
Row::new(vec![Line::from("Center").alignment(Alignment::Center)]),
|
||||
Row::new(vec![Line::from("Right").alignment(Alignment::Right)]),
|
||||
])
|
||||
.widths(&[Constraint::Percentage(100)]);
|
||||
|
||||
Widget::render(table, Rect::new(0, 0, 20, 3), &mut buf);
|
||||
|
||||
let expected = Buffer::with_lines(vec![
|
||||
"Left ",
|
||||
" Center ",
|
||||
" Right",
|
||||
]);
|
||||
|
||||
assert_eq!(buf, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_can_be_stylized() {
|
||||
assert_eq!(
|
||||
|
|
Loading…
Add table
Reference in a new issue