2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
2020-04-14 16:02:48 +00:00
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
2020-04-16 14:06:50 +00:00
|
|
|
layout::Rect,
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Style},
|
2020-04-16 14:06:50 +00:00
|
|
|
symbols,
|
2023-05-18 18:21:43 +00:00
|
|
|
text::Line,
|
2023-08-13 08:24:51 +00:00
|
|
|
widgets::{Block, Borders, HighlightSpacing, List, ListItem, ListState},
|
2020-04-14 16:02:48 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2024-05-14 01:13:46 +00:00
|
|
|
use rstest::rstest;
|
2020-04-14 16:02:48 +00:00
|
|
|
|
2022-04-15 01:46:45 +00:00
|
|
|
#[test]
|
|
|
|
fn list_should_shows_the_length() {
|
|
|
|
let items = vec![
|
|
|
|
ListItem::new("Item 1"),
|
|
|
|
ListItem::new("Item 2"),
|
|
|
|
ListItem::new("Item 3"),
|
|
|
|
];
|
|
|
|
let list = List::new(items);
|
|
|
|
assert_eq!(list.len(), 3);
|
|
|
|
assert!(!list.is_empty());
|
|
|
|
|
2023-12-08 22:20:49 +00:00
|
|
|
let empty_list = List::default();
|
2022-04-15 01:46:45 +00:00
|
|
|
assert_eq!(empty_list.len(), 0);
|
|
|
|
assert!(empty_list.is_empty());
|
|
|
|
}
|
|
|
|
|
2020-04-14 16:02:48 +00:00
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_list_should_highlight_the_selected_item() {
|
2020-04-14 16:02:48 +00:00
|
|
|
let backend = TestBackend::new(10, 3);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let mut state = ListState::default();
|
|
|
|
state.select(Some(1));
|
|
|
|
terminal
|
2020-06-15 20:57:23 +00:00
|
|
|
.draw(|f| {
|
2020-04-14 16:02:48 +00:00
|
|
|
let items = vec![
|
2020-05-10 13:44:30 +00:00
|
|
|
ListItem::new("Item 1"),
|
|
|
|
ListItem::new("Item 2"),
|
|
|
|
ListItem::new("Item 3"),
|
2020-04-14 16:02:48 +00:00
|
|
|
];
|
2020-05-10 13:44:30 +00:00
|
|
|
let list = List::new(items)
|
2020-07-11 17:08:28 +00:00
|
|
|
.highlight_style(Style::default().bg(Color::Yellow))
|
2020-04-14 16:02:48 +00:00
|
|
|
.highlight_symbol(">> ");
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(list, f.area(), &mut state);
|
2020-04-14 16:02:48 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut expected = Buffer::with_lines([
|
|
|
|
" Item 1 ",
|
|
|
|
">> Item 2 ",
|
|
|
|
" Item 3 ",
|
|
|
|
]);
|
2020-07-11 17:08:28 +00:00
|
|
|
for x in 0..10 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(x, 1)].set_bg(Color::Yellow);
|
2020-04-14 16:02:48 +00:00
|
|
|
}
|
2020-05-21 18:42:34 +00:00
|
|
|
terminal.backend().assert_buffer(&expected);
|
2020-04-14 16:02:48 +00:00
|
|
|
}
|
2020-04-16 14:06:50 +00:00
|
|
|
|
2024-02-05 19:59:19 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_list_should_highlight_the_selected_item_wide_symbol() {
|
|
|
|
let backend = TestBackend::new(10, 3);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let mut state = ListState::default();
|
|
|
|
|
|
|
|
let wide_symbol = "▶ ";
|
|
|
|
|
|
|
|
state.select(Some(1));
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let items = vec![
|
|
|
|
ListItem::new("Item 1"),
|
|
|
|
ListItem::new("Item 2"),
|
|
|
|
ListItem::new("Item 3"),
|
|
|
|
];
|
|
|
|
let list = List::new(items)
|
|
|
|
.highlight_style(Style::default().bg(Color::Yellow))
|
|
|
|
.highlight_symbol(wide_symbol);
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(list, f.area(), &mut state);
|
2024-02-05 19:59:19 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut expected = Buffer::with_lines([
|
|
|
|
" Item 1 ",
|
|
|
|
"▶ Item 2 ",
|
|
|
|
" Item 3 ",
|
|
|
|
]);
|
2024-02-05 19:59:19 +00:00
|
|
|
for x in 0..10 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(x, 1)].set_bg(Color::Yellow);
|
2024-02-05 19:59:19 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:06:50 +00:00
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_list_should_truncate_items() {
|
2020-04-16 14:06:50 +00:00
|
|
|
struct TruncateTestCase<'a> {
|
|
|
|
selected: Option<usize>,
|
2020-05-10 13:44:30 +00:00
|
|
|
items: Vec<ListItem<'a>>,
|
2020-04-16 14:06:50 +00:00
|
|
|
expected: Buffer,
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
let backend = TestBackend::new(10, 2);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let cases = [
|
2020-05-21 18:42:34 +00:00
|
|
|
// An item is selected
|
2020-04-16 14:06:50 +00:00
|
|
|
TruncateTestCase {
|
|
|
|
selected: Some(0),
|
2020-05-10 13:44:30 +00:00
|
|
|
items: vec![
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
],
|
2024-05-14 01:13:46 +00:00
|
|
|
expected: Buffer::with_lines([
|
2020-04-16 14:06:50 +00:00
|
|
|
format!(">> A ve{} ", symbols::line::VERTICAL),
|
|
|
|
format!(" A ve{} ", symbols::line::VERTICAL),
|
|
|
|
]),
|
|
|
|
},
|
2020-05-21 18:42:34 +00:00
|
|
|
// No item is selected
|
2020-04-16 14:06:50 +00:00
|
|
|
TruncateTestCase {
|
|
|
|
selected: None,
|
2020-05-10 13:44:30 +00:00
|
|
|
items: vec![
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
],
|
2024-05-14 01:13:46 +00:00
|
|
|
expected: Buffer::with_lines([
|
2020-04-16 14:06:50 +00:00
|
|
|
format!("A very {} ", symbols::line::VERTICAL),
|
|
|
|
format!("A very {} ", symbols::line::VERTICAL),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
];
|
2020-05-10 13:44:30 +00:00
|
|
|
for case in cases {
|
2020-04-16 14:06:50 +00:00
|
|
|
let mut state = ListState::default();
|
|
|
|
state.select(case.selected);
|
|
|
|
terminal
|
2020-06-15 20:57:23 +00:00
|
|
|
.draw(|f| {
|
2020-05-10 13:44:30 +00:00
|
|
|
let list = List::new(case.items.clone())
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::new().borders(Borders::RIGHT))
|
2020-04-16 14:06:50 +00:00
|
|
|
.highlight_symbol(">> ");
|
|
|
|
f.render_stateful_widget(list, Rect::new(0, 0, 8, 2), &mut state);
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-05-21 18:42:34 +00:00
|
|
|
terminal.backend().assert_buffer(&case.expected);
|
2020-04-16 14:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 15:51:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_list_should_clamp_offset_if_items_are_removed() {
|
|
|
|
let backend = TestBackend::new(10, 4);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let mut state = ListState::default();
|
|
|
|
|
|
|
|
// render with 6 items => offset will be at 2
|
|
|
|
state.select(Some(5));
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let items = vec![
|
|
|
|
ListItem::new("Item 0"),
|
|
|
|
ListItem::new("Item 1"),
|
|
|
|
ListItem::new("Item 2"),
|
|
|
|
ListItem::new("Item 3"),
|
|
|
|
ListItem::new("Item 4"),
|
|
|
|
ListItem::new("Item 5"),
|
|
|
|
];
|
|
|
|
let list = List::new(items).highlight_symbol(">> ");
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(list, f.area(), &mut state);
|
2021-08-01 15:51:44 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer_lines([
|
|
|
|
" Item 2 ",
|
|
|
|
" Item 3 ",
|
|
|
|
" Item 4 ",
|
|
|
|
">> Item 5 ",
|
|
|
|
]);
|
2021-08-01 15:51:44 +00:00
|
|
|
|
|
|
|
// render again with 1 items => check offset is clamped to 1
|
|
|
|
state.select(Some(1));
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let items = vec![ListItem::new("Item 3")];
|
|
|
|
let list = List::new(items).highlight_symbol(">> ");
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(list, f.area(), &mut state);
|
2021-08-01 15:51:44 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer_lines([
|
2024-06-24 08:37:22 +00:00
|
|
|
">> Item 3 ",
|
2024-05-14 01:13:46 +00:00
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]);
|
2021-08-01 15:51:44 +00:00
|
|
|
}
|
2021-10-17 15:05:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_list_should_display_multiline_items() {
|
|
|
|
let backend = TestBackend::new(10, 6);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let mut state = ListState::default();
|
|
|
|
state.select(Some(1));
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let items = vec![
|
2023-05-18 18:21:43 +00:00
|
|
|
ListItem::new(vec![Line::from("Item 1"), Line::from("Item 1a")]),
|
|
|
|
ListItem::new(vec![Line::from("Item 2"), Line::from("Item 2b")]),
|
|
|
|
ListItem::new(vec![Line::from("Item 3"), Line::from("Item 3c")]),
|
2021-10-17 15:05:51 +00:00
|
|
|
];
|
|
|
|
let list = List::new(items)
|
|
|
|
.highlight_style(Style::default().bg(Color::Yellow))
|
|
|
|
.highlight_symbol(">> ");
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(list, f.area(), &mut state);
|
2021-10-17 15:05:51 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2021-10-17 15:05:51 +00:00
|
|
|
" Item 1 ",
|
|
|
|
" Item 1a",
|
|
|
|
">> Item 2 ",
|
|
|
|
" Item 2b",
|
|
|
|
" Item 3 ",
|
2021-10-17 15:12:50 +00:00
|
|
|
" Item 3c",
|
|
|
|
]);
|
2021-10-17 15:05:51 +00:00
|
|
|
for x in 0..10 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(x, 2)].set_bg(Color::Yellow);
|
|
|
|
expected[(x, 3)].set_bg(Color::Yellow);
|
2021-10-17 15:05:51 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_list_should_repeat_highlight_symbol() {
|
|
|
|
let backend = TestBackend::new(10, 6);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let mut state = ListState::default();
|
|
|
|
state.select(Some(1));
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let items = vec![
|
2023-05-18 18:21:43 +00:00
|
|
|
ListItem::new(vec![Line::from("Item 1"), Line::from("Item 1a")]),
|
|
|
|
ListItem::new(vec![Line::from("Item 2"), Line::from("Item 2b")]),
|
|
|
|
ListItem::new(vec![Line::from("Item 3"), Line::from("Item 3c")]),
|
2021-10-17 15:05:51 +00:00
|
|
|
];
|
|
|
|
let list = List::new(items)
|
|
|
|
.highlight_style(Style::default().bg(Color::Yellow))
|
|
|
|
.highlight_symbol(">> ")
|
|
|
|
.repeat_highlight_symbol(true);
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(list, f.area(), &mut state);
|
2021-10-17 15:05:51 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2021-10-17 15:05:51 +00:00
|
|
|
" Item 1 ",
|
|
|
|
" Item 1a",
|
|
|
|
">> Item 2 ",
|
|
|
|
">> Item 2b",
|
|
|
|
" Item 3 ",
|
2021-10-17 15:12:50 +00:00
|
|
|
" Item 3c",
|
|
|
|
]);
|
2021-10-17 15:05:51 +00:00
|
|
|
for x in 0..10 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(x, 2)].set_bg(Color::Yellow);
|
|
|
|
expected[(x, 3)].set_bg(Color::Yellow);
|
2021-10-17 15:05:51 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
2023-02-21 12:22:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widget_list_should_not_ignore_empty_string_items() {
|
|
|
|
let backend = TestBackend::new(6, 4);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let items = vec![
|
|
|
|
ListItem::new("Item 1"),
|
|
|
|
ListItem::new(""),
|
|
|
|
ListItem::new(""),
|
|
|
|
ListItem::new("Item 4"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let list = List::new(items)
|
|
|
|
.style(Style::default())
|
|
|
|
.highlight_style(Style::default());
|
|
|
|
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_widget(list, f.area());
|
2023-02-21 12:22:37 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal
|
|
|
|
.backend()
|
|
|
|
.assert_buffer_lines(["Item 1", "", "", "Item 4"]);
|
2023-02-21 12:22:37 +00:00
|
|
|
}
|
2023-08-13 08:24:51 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rstest]
|
|
|
|
#[case::none_when_selected(None, HighlightSpacing::WhenSelected, [
|
|
|
|
"┌─────────────┐",
|
|
|
|
"│Item 1 │",
|
|
|
|
"│Item 1a │",
|
|
|
|
"│Item 2 │",
|
|
|
|
"│Item 2b │",
|
|
|
|
"│Item 3 │",
|
|
|
|
"│Item 3c │",
|
|
|
|
"└─────────────┘",
|
|
|
|
])]
|
|
|
|
#[case::none_always(None, HighlightSpacing::Always, [
|
|
|
|
"┌─────────────┐",
|
|
|
|
"│ Item 1 │",
|
|
|
|
"│ Item 1a │",
|
|
|
|
"│ Item 2 │",
|
|
|
|
"│ Item 2b │",
|
|
|
|
"│ Item 3 │",
|
|
|
|
"│ Item 3c │",
|
|
|
|
"└─────────────┘",
|
|
|
|
])]
|
|
|
|
#[case::none_never(None, HighlightSpacing::Never, [
|
|
|
|
"┌─────────────┐",
|
|
|
|
"│Item 1 │",
|
|
|
|
"│Item 1a │",
|
|
|
|
"│Item 2 │",
|
|
|
|
"│Item 2b │",
|
|
|
|
"│Item 3 │",
|
|
|
|
"│Item 3c │",
|
|
|
|
"└─────────────┘",
|
|
|
|
])]
|
|
|
|
#[case::first_when_selected(Some(0), HighlightSpacing::WhenSelected, [
|
|
|
|
"┌─────────────┐",
|
|
|
|
"│>> Item 1 │",
|
|
|
|
"│ Item 1a │",
|
|
|
|
"│ Item 2 │",
|
|
|
|
"│ Item 2b │",
|
|
|
|
"│ Item 3 │",
|
|
|
|
"│ Item 3c │",
|
|
|
|
"└─────────────┘",
|
|
|
|
])]
|
|
|
|
#[case::first_always(Some(0), HighlightSpacing::Always, [
|
|
|
|
"┌─────────────┐",
|
|
|
|
"│>> Item 1 │",
|
|
|
|
"│ Item 1a │",
|
|
|
|
"│ Item 2 │",
|
|
|
|
"│ Item 2b │",
|
|
|
|
"│ Item 3 │",
|
|
|
|
"│ Item 3c │",
|
|
|
|
"└─────────────┘",
|
|
|
|
])]
|
|
|
|
#[case::first_never(Some(0), HighlightSpacing::Never, [
|
|
|
|
"┌─────────────┐",
|
|
|
|
"│Item 1 │",
|
|
|
|
"│Item 1a │",
|
|
|
|
"│Item 2 │",
|
|
|
|
"│Item 2b │",
|
|
|
|
"│Item 3 │",
|
|
|
|
"│Item 3c │",
|
|
|
|
"└─────────────┘",
|
|
|
|
])]
|
|
|
|
fn widgets_list_enable_always_highlight_spacing<'line, Lines>(
|
|
|
|
#[case] selected: Option<usize>,
|
|
|
|
#[case] space: HighlightSpacing,
|
|
|
|
#[case] expected: Lines,
|
|
|
|
) where
|
|
|
|
Lines: IntoIterator,
|
|
|
|
Lines::Item: Into<Line<'line>>,
|
|
|
|
{
|
|
|
|
let mut state = ListState::default().with_selected(selected);
|
|
|
|
let backend = TestBackend::new(15, 8);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let table = List::new(vec![
|
|
|
|
ListItem::new(vec![Line::from("Item 1"), Line::from("Item 1a")]),
|
|
|
|
ListItem::new(vec![Line::from("Item 2"), Line::from("Item 2b")]),
|
|
|
|
ListItem::new(vec![Line::from("Item 3"), Line::from("Item 3c")]),
|
|
|
|
])
|
|
|
|
.block(Block::bordered())
|
|
|
|
.highlight_symbol(">> ")
|
|
|
|
.highlight_spacing(space);
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_stateful_widget(table, f.area(), &mut state);
|
2024-05-14 01:13:46 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
terminal
|
|
|
|
.backend()
|
|
|
|
.assert_buffer(&Buffer::with_lines(expected));
|
2023-08-13 08:24:51 +00:00
|
|
|
}
|