Skip to content

Commit

Permalink
feat(table): add support for line alignment in the table widget (#392)
Browse files Browse the repository at this point in the history
* feat(table): enforce line alignment in table render

* test(table): add table alignment render test
  • Loading branch information
jkcdarunday committed Aug 13, 2023
1 parent 4d70169 commit 7748720
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/widgets/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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!(
Expand Down

0 comments on commit 7748720

Please sign in to comment.