2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
2020-05-10 13:44:30 +00:00
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
2021-05-22 14:55:24 +00:00
|
|
|
layout::{Alignment, Rect},
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Style},
|
2020-05-10 13:44:30 +00:00
|
|
|
text::Span,
|
2023-06-19 08:24:36 +00:00
|
|
|
widgets::{
|
|
|
|
block::title::{Position, Title},
|
|
|
|
Block, Borders,
|
|
|
|
},
|
2020-05-10 13:44:30 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2024-05-14 01:13:46 +00:00
|
|
|
use rstest::rstest;
|
2018-11-04 19:16:10 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_block_renders() {
|
2018-11-04 19:16:10 +00:00
|
|
|
let backend = TestBackend::new(10, 10);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
2024-05-02 10:09:48 +00:00
|
|
|
let block =
|
|
|
|
Block::bordered().title(Span::styled("Title", Style::default().fg(Color::LightBlue)));
|
2018-11-04 19:16:10 +00:00
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, Rect::new(0, 0, 8, 8)))
|
2018-12-07 15:17:33 +00:00
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2018-11-04 19:16:10 +00:00
|
|
|
"┌Title─┐ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"└──────┘ ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]);
|
|
|
|
for x in 1..=5 {
|
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, 0)].set_fg(Color::LightBlue);
|
2018-11-04 19:16:10 +00:00
|
|
|
}
|
2020-05-21 18:17:55 +00:00
|
|
|
terminal.backend().assert_buffer(&expected);
|
2018-11-04 19:16:10 +00:00
|
|
|
}
|
2020-11-14 19:26:16 +00:00
|
|
|
|
2023-06-19 08:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_titles_overlap() {
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
2024-05-14 01:13:46 +00:00
|
|
|
fn test_case<'line, Lines>(block: Block, area: Rect, expected: Lines)
|
|
|
|
where
|
|
|
|
Lines: IntoIterator,
|
|
|
|
Lines::Item: Into<ratatui::text::Line<'line>>,
|
|
|
|
{
|
2023-06-19 08:24:36 +00:00
|
|
|
let backend = TestBackend::new(area.width, area.height);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2023-06-19 08:24:36 +00:00
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer_lines(expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2023-06-19 08:24:36 +00:00
|
|
|
|
|
|
|
// Left overrides the center
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
2023-06-19 08:24:36 +00:00
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("bbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 10, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
["aaaaab ccc"],
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Left alignment overrides the center alignment which overrides the right alignment
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
2023-06-19 08:24:36 +00:00
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("bbbbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 11, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
["aaaaabbbccc"],
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Multiple left alignment overrides the center alignment and the right alignment
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
2023-06-19 08:24:36 +00:00
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("bbbbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 11, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
["aaaaabaaaaa"],
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// The right alignment doesn't override the center alignment, but pierces through it
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
2023-06-19 08:24:36 +00:00
|
|
|
.title(Title::from("bbbbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccccccccccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 11, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
["cccbbbbbccc"],
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-14 19:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_renders_on_small_areas() {
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
2024-05-14 01:13:46 +00:00
|
|
|
fn test_case(block: Block, area: Rect, expected: &Buffer) {
|
2020-11-14 19:26:16 +00:00
|
|
|
let backend = TestBackend::new(area.width, area.height);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2020-11-14 19:26:16 +00:00
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer(expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2020-11-14 19:26:16 +00:00
|
|
|
|
|
|
|
let one_cell_test_cases = [
|
|
|
|
(Borders::NONE, "T"),
|
|
|
|
(Borders::LEFT, "│"),
|
|
|
|
(Borders::TOP, "T"),
|
|
|
|
(Borders::RIGHT, "│"),
|
|
|
|
(Borders::BOTTOM, "T"),
|
|
|
|
(Borders::ALL, "┌"),
|
|
|
|
];
|
2024-03-02 09:06:53 +00:00
|
|
|
for (borders, symbol) in one_cell_test_cases {
|
2020-11-14 19:26:16 +00:00
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(borders).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 0, 0),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::empty(Rect::new(0, 0, 0, 0)),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(borders).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 1, 0),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::empty(Rect::new(0, 0, 1, 0)),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(borders).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 0, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::empty(Rect::new(0, 0, 0, 1)),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(borders).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 1, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines([symbol]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(Borders::LEFT).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["│Tes"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(Borders::RIGHT).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["Tes│"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(Borders::RIGHT).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["Tes│"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
|
|
|
.title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["│Te│"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(Borders::TOP).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["Test"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new().borders(Borders::TOP).title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 5, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["Test─"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
|
|
|
.borders(Borders::LEFT | Borders::TOP)
|
|
|
|
.title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 5, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["┌Test"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
|
|
|
.borders(Borders::LEFT | Borders::TOP)
|
|
|
|
.title("Test"),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 6, 1),
|
2024-05-14 01:13:46 +00:00
|
|
|
&Buffer::with_lines(["┌Test─"]),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-22 14:55:24 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rstest]
|
|
|
|
#[case::left_with_all_borders(Alignment::Left, Borders::ALL, [
|
|
|
|
" ┌Title──────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_top_border(Alignment::Left, Borders::LEFT | Borders::BOTTOM | Borders::RIGHT, [
|
|
|
|
" │Title │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_left_border(Alignment::Left, Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" Title───────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_right_border(Alignment::Left, Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌Title─────── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_borders(Alignment::Left, Borders::NONE, [
|
|
|
|
" Title ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
])]
|
|
|
|
#[case::center_with_all_borders(Alignment::Center, Borders::ALL, [
|
|
|
|
" ┌───Title───┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_top_border(Alignment::Center, Borders::LEFT | Borders::BOTTOM | Borders::RIGHT, [
|
|
|
|
" │ Title │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_left_border(Alignment::Center, Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ───Title────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_right_border(Alignment::Center, Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌───Title──── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_borders(Alignment::Center, Borders::NONE, [
|
|
|
|
" Title ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
])]
|
|
|
|
#[case::right_with_all_borders(Alignment::Right, Borders::ALL, [
|
|
|
|
" ┌──────Title┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_top_border(Alignment::Right, Borders::LEFT | Borders::BOTTOM | Borders::RIGHT, [
|
|
|
|
" │ Title│ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_left_border(Alignment::Right, Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ───────Title┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_right_border(Alignment::Right, Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌───────Title ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_borders(Alignment::Right, Borders::NONE, [
|
|
|
|
" Title ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
])]
|
|
|
|
fn widgets_block_title_alignment_top<'line, Lines>(
|
|
|
|
#[case] alignment: Alignment,
|
|
|
|
#[case] borders: Borders,
|
|
|
|
#[case] expected: Lines,
|
|
|
|
) where
|
|
|
|
Lines: IntoIterator,
|
|
|
|
Lines::Item: Into<ratatui::text::Line<'line>>,
|
|
|
|
{
|
|
|
|
let backend = TestBackend::new(15, 3);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
2021-05-22 14:55:24 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let block1 = Block::new()
|
|
|
|
.borders(borders)
|
|
|
|
.title(Title::from(Span::raw("Title")).alignment(alignment));
|
2021-05-22 14:55:24 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let block2 = Block::new()
|
|
|
|
.borders(borders)
|
|
|
|
.title_alignment(alignment)
|
|
|
|
.title("Title");
|
|
|
|
let area = Rect::new(1, 0, 13, 3);
|
|
|
|
let expected = Buffer::with_lines(expected);
|
2021-05-22 14:55:24 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
for block in [block1, block2] {
|
2023-04-13 20:24:31 +00:00
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2023-04-13 20:24:31 +00:00
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2023-04-13 20:24:31 +00:00
|
|
|
}
|
2023-06-19 08:24:36 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rstest]
|
|
|
|
#[case::left(Alignment::Left, Borders::ALL, [
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └Title──────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Left, Borders::LEFT | Borders::TOP | Borders::RIGHT, [
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" │Title │ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Left, Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ────────────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" Title───────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Left, Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌──────────── ",
|
|
|
|
" │ ",
|
|
|
|
" └Title─────── ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Left, Borders::NONE, [
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" Title ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Center, Borders::ALL, [
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───Title───┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Center, Borders::LEFT | Borders::TOP | Borders::RIGHT, [
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" │ Title │ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Center, Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ────────────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ───Title────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Center, Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌──────────── ",
|
|
|
|
" │ ",
|
|
|
|
" └───Title──── ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Center, Borders::NONE, [
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" Title ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Right, Borders::ALL, [
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └──────Title┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Right, Borders::LEFT | Borders::TOP | Borders::RIGHT, [
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" │ Title│ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Right, Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ────────────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ───────Title┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Right, Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌──────────── ",
|
|
|
|
" │ ",
|
|
|
|
" └───────Title ",
|
|
|
|
])]
|
|
|
|
#[case::left(Alignment::Right, Borders::NONE, [
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" Title ",
|
|
|
|
])]
|
|
|
|
fn widgets_block_title_alignment_bottom<'line, Lines>(
|
|
|
|
#[case] alignment: Alignment,
|
|
|
|
#[case] borders: Borders,
|
|
|
|
#[case] expected: Lines,
|
|
|
|
) where
|
|
|
|
Lines: IntoIterator,
|
|
|
|
Lines::Item: Into<ratatui::text::Line<'line>>,
|
|
|
|
{
|
|
|
|
let backend = TestBackend::new(15, 3);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
2023-06-19 08:24:36 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let title = Title::from(Span::styled("Title", Style::default()))
|
|
|
|
.alignment(alignment)
|
|
|
|
.position(Position::Bottom);
|
|
|
|
let block = Block::default().title(title).borders(borders);
|
|
|
|
let area = Rect::new(1, 0, 13, 3);
|
|
|
|
terminal
|
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer_lines(expected);
|
|
|
|
}
|
2023-06-19 08:24:36 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rstest]
|
|
|
|
#[case::left_with_all_borders(Title::from("foo"), Title::from("bar"), Borders::ALL, [
|
|
|
|
" ┌foo─bar────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_top_border(Title::from("foo"), Title::from("bar"), Borders::LEFT | Borders::BOTTOM | Borders::RIGHT, [
|
|
|
|
" │foo bar │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_left_border(Title::from("foo"), Title::from("bar"), Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" foo─bar─────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_right_border(Title::from("foo"), Title::from("bar"), Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌foo─bar───── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
])]
|
|
|
|
#[case::left_without_borders(Title::from("foo"), Title::from("bar"), Borders::NONE, [
|
|
|
|
" foo bar ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
])]
|
|
|
|
#[case::center_with_borders(Title::from("foo").alignment(Alignment::Center), Title::from("bar").alignment(Alignment::Center), Borders::ALL, [
|
|
|
|
" ┌──foo─bar──┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_top_border(Title::from("foo").alignment(Alignment::Center), Title::from("bar").alignment(Alignment::Center), Borders::LEFT | Borders::BOTTOM | Borders::RIGHT, [
|
|
|
|
" │ foo bar │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_left_border(Title::from("foo").alignment(Alignment::Center), Title::from("bar").alignment(Alignment::Center), Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ──foo─bar───┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_right_border(Title::from("foo").alignment(Alignment::Center), Title::from("bar").alignment(Alignment::Center), Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌──foo─bar─── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
])]
|
|
|
|
#[case::center_without_borders(Title::from("foo").alignment(Alignment::Center), Title::from("bar").alignment(Alignment::Center), Borders::NONE, [
|
|
|
|
" foo bar ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
])]
|
|
|
|
#[case::right_with_all_borders(Title::from("foo").alignment(Alignment::Right), Title::from("bar").alignment(Alignment::Right), Borders::ALL, [
|
|
|
|
" ┌────foo─bar┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_top_border(Title::from("foo").alignment(Alignment::Right), Title::from("bar").alignment(Alignment::Right), Borders::LEFT | Borders::BOTTOM | Borders::RIGHT, [
|
|
|
|
" │ foo bar│ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_left_border(Title::from("foo").alignment(Alignment::Right), Title::from("bar").alignment(Alignment::Right), Borders::TOP | Borders::RIGHT | Borders::BOTTOM, [
|
|
|
|
" ─────foo─bar┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_right_border(Title::from("foo").alignment(Alignment::Right), Title::from("bar").alignment(Alignment::Right), Borders::LEFT | Borders::TOP | Borders::BOTTOM, [
|
|
|
|
" ┌─────foo─bar ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
])]
|
|
|
|
#[case::right_without_borders(Title::from("foo").alignment(Alignment::Right), Title::from("bar").alignment(Alignment::Right), Borders::NONE, [
|
|
|
|
" foo bar ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
])]
|
|
|
|
fn widgets_block_multiple_titles<'line, Lines>(
|
|
|
|
#[case] title_a: Title,
|
|
|
|
#[case] title_b: Title,
|
|
|
|
#[case] borders: Borders,
|
|
|
|
#[case] expected: Lines,
|
|
|
|
) where
|
|
|
|
Lines: IntoIterator,
|
|
|
|
Lines::Item: Into<ratatui::text::Line<'line>>,
|
|
|
|
{
|
|
|
|
let backend = TestBackend::new(15, 3);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let block = Block::default()
|
|
|
|
.title(title_a)
|
|
|
|
.title(title_b)
|
|
|
|
.borders(borders);
|
|
|
|
let area = Rect::new(1, 0, 13, 3);
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
f.render_widget(block, area);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer_lines(expected);
|
2023-06-19 08:24:36 +00:00
|
|
|
}
|