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

feat(list): support line alignment #599

Merged
merged 5 commits into from
Nov 23, 2023
Merged
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
168 changes: 166 additions & 2 deletions src/widgets/list.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::{Corner, Rect},
layout::{Alignment, Corner, Rect},
style::{Style, Styled},
text::Text,
widgets::{Block, HighlightSpacing, StatefulWidget, Widget},
Expand Down Expand Up @@ -286,7 +286,14 @@ impl<'a> StatefulWidget for List<'a> {
} else {
(x, list_area.width)
};
buf.set_line(elem_x, y + j as u16, line, max_element_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(elem_x + x_offset, y + j as u16, line, max_element_width);
joshka marked this conversation as resolved.
Show resolved Hide resolved
joshka marked this conversation as resolved.
Show resolved Hide resolved
}
if is_selected {
buf.set_style(area, self.highlight_style);
Expand Down Expand Up @@ -333,6 +340,7 @@ mod tests {
use super::*;
use crate::{
assert_buffer_eq,
prelude::Alignment,
style::{Color, Modifier, Stylize},
text::{Line, Span},
widgets::{Borders, StatefulWidget, Widget},
Expand Down Expand Up @@ -1065,4 +1073,160 @@ mod tests {
.remove_modifier(Modifier::DIM)
)
}

#[test]
fn test_render_list_with_alignment() {
let items = [
Line::from("Left").alignment(Alignment::Left),
Line::from("Center").alignment(Alignment::Center),
Line::from("Right").alignment(Alignment::Right),
]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 10, 5);
let expected = Buffer::with_lines(vec![
"Left ",
" Center ",
" Right",
" ",
" ",
]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_odd_line_odd_area() {
let items = [
Line::from("Odd").alignment(Alignment::Left),
Line::from("Even").alignment(Alignment::Center),
Line::from("Width").alignment(Alignment::Right),
]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 7, 5);
let expected =
Buffer::with_lines(vec!["Odd ", " Even ", " Width", " ", " "]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_even_line_even_area() {
let items = [
Line::from("Odd").alignment(Alignment::Left),
Line::from("Even").alignment(Alignment::Center),
Line::from("Width").alignment(Alignment::Right),
]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 6, 4);
let expected = Buffer::with_lines(vec!["Odd ", " Even ", " Width", " "]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_odd_line_even_area() {
let items = [
Line::from("Odd").alignment(Alignment::Left),
Line::from("Even").alignment(Alignment::Center),
Line::from("Width").alignment(Alignment::Right),
]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 8, 4);
let expected = Buffer::with_lines(vec!["Odd ", " Even ", " Width", " "]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_even_line_odd_area() {
let items = [
Line::from("Odd").alignment(Alignment::Left),
Line::from("Even").alignment(Alignment::Center),
Line::from("Width").alignment(Alignment::Right),
]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 6, 5);
let expected = Buffer::with_lines(vec!["Odd ", " Even ", " Width", " ", " "]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_zero_line_width() {
let items = [Line::from("This line has zero width").alignment(Alignment::Center)]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 0, 5);
let expected = Buffer::with_lines(vec!["", "", "", "", ""]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_zero_area_width() {
let items = [Line::from("Text").alignment(Alignment::Left)]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
// assert_buffer_eq! doesn't handle zero height buffers so we call this test manually
// rather than using render_widget
let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1));
Widget::render(list, Rect::new(0, 0, 4, 0), &mut buffer);
let expected = Buffer::with_lines(vec![" "]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_line_less_than_width() {
let items = [Line::from("Small").alignment(Alignment::Center)]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 10, 5);
let expected = Buffer::with_lines(vec![
" Small ",
" ",
" ",
" ",
" ",
]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_line_equal_to_width() {
let items = [Line::from("Exact").alignment(Alignment::Left)]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 5, 3);
let expected = Buffer::with_lines(vec!["Exact", " ", " "]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn test_render_list_alignment_line_greater_than_width() {
let items = [Line::from("Large line").alignment(Alignment::Left)]
.into_iter()
.map(ListItem::new)
.collect::<Vec<ListItem>>();
let list = List::new(items);
let buffer = render_widget(list, 5, 3);
let expected = Buffer::with_lines(vec!["Large", " ", " "]);
assert_buffer_eq!(buffer, expected);
}
}