mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-28 05:21:23 +00:00

* refactor: add Line type to replace Spans `Line` is a significantly better name over `Spans` as the plural causes confusion and the type really is a representation of a line of text made up of spans. This is a backwards compatible version of the approach from https://github.com/tui-rs-revival/ratatui/pull/175 There is a significant amount of code that uses the Spans type and methods, so instead of just renaming it, we add a new type and replace parameters that accepts a `Spans` with a parameter that accepts `Into<Line>`. Note that the examples have been intentionally left using `Spans` in this commit to demonstrate the compiler warnings that will be emitted in existing code. Implementation notes: - moves the Spans code to text::spans and publicly reexports on the text module. This makes the test in that module only relevant to the Spans type. - adds a line module with a copy of the code and tests from Spans with a single addition: `impl<'a> From<Spans<'a>> for Line<'a>` - adds tests for `Spans` (created and checked before refactoring) - adds the same tests for `Line` - updates all widget methods that accept and store Spans to instead store `Line` and accept `Into<Line>` * refactor: move text::Masked to text::masked::Masked Re-exports the Masked type at text::Masked * refactor: replace Spans with Line in tests/examples/docs
234 lines
8.2 KiB
Rust
234 lines
8.2 KiB
Rust
#![allow(deprecated)]
|
|
|
|
use ratatui::{
|
|
backend::TestBackend,
|
|
buffer::Buffer,
|
|
layout::Alignment,
|
|
text::{Line, Span, Text},
|
|
widgets::{Block, Borders, Padding, Paragraph, Wrap},
|
|
Terminal,
|
|
};
|
|
|
|
const SAMPLE_STRING: &str = "The library is based on the principle of immediate rendering with \
|
|
intermediate buffers. This means that at each new frame you should build all widgets that are \
|
|
supposed to be part of the UI. While providing a great flexibility for rich and \
|
|
interactive UI, this may introduce overhead for highly dynamic content.";
|
|
|
|
#[test]
|
|
fn widgets_paragraph_can_wrap_its_content() {
|
|
let test_case = |alignment, expected| {
|
|
let backend = TestBackend::new(22, 12);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
.draw(|f| {
|
|
let size = f.size();
|
|
let text = vec![Line::from(SAMPLE_STRING)];
|
|
let paragraph = Paragraph::new(text)
|
|
.block(Block::default().borders(Borders::ALL).padding(Padding {
|
|
left: 2,
|
|
right: 2,
|
|
top: 1,
|
|
bottom: 1,
|
|
}))
|
|
.alignment(alignment)
|
|
.wrap(Wrap { trim: true });
|
|
f.render_widget(paragraph, size);
|
|
})
|
|
.unwrap();
|
|
terminal.backend().assert_buffer(&expected);
|
|
};
|
|
|
|
test_case(
|
|
Alignment::Left,
|
|
Buffer::with_lines(vec![
|
|
"┌────────────────────┐",
|
|
"│ │",
|
|
"│ The library is │",
|
|
"│ based on the │",
|
|
"│ principle of │",
|
|
"│ immediate │",
|
|
"│ rendering with │",
|
|
"│ intermediate │",
|
|
"│ buffers. This │",
|
|
"│ means that at │",
|
|
"│ │",
|
|
"└────────────────────┘",
|
|
]),
|
|
);
|
|
test_case(
|
|
Alignment::Right,
|
|
Buffer::with_lines(vec![
|
|
"┌────────────────────┐",
|
|
"│ │",
|
|
"│ The library is │",
|
|
"│ based on the │",
|
|
"│ principle of │",
|
|
"│ immediate │",
|
|
"│ rendering with │",
|
|
"│ intermediate │",
|
|
"│ buffers. This │",
|
|
"│ means that at │",
|
|
"│ │",
|
|
"└────────────────────┘",
|
|
]),
|
|
);
|
|
test_case(
|
|
Alignment::Center,
|
|
Buffer::with_lines(vec![
|
|
"┌────────────────────┐",
|
|
"│ │",
|
|
"│ The library is │",
|
|
"│ based on the │",
|
|
"│ principle of │",
|
|
"│ immediate │",
|
|
"│ rendering with │",
|
|
"│ intermediate │",
|
|
"│ buffers. This │",
|
|
"│ means that at │",
|
|
"│ │",
|
|
"└────────────────────┘",
|
|
]),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn widgets_paragraph_renders_double_width_graphemes() {
|
|
let backend = TestBackend::new(10, 10);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let s = "コンピュータ上で文字を扱う場合、典型的には文字による通信を行う場合にその両端点では、";
|
|
terminal
|
|
.draw(|f| {
|
|
let size = f.size();
|
|
let text = vec![Line::from(s)];
|
|
let paragraph = Paragraph::new(text)
|
|
.block(Block::default().borders(Borders::ALL))
|
|
.wrap(Wrap { trim: true });
|
|
f.render_widget(paragraph, size);
|
|
})
|
|
.unwrap();
|
|
|
|
let expected = Buffer::with_lines(vec![
|
|
"┌────────┐",
|
|
"│コンピュ│",
|
|
"│ータ上で│",
|
|
"│文字を扱│",
|
|
"│う場合、│",
|
|
"│典型的に│",
|
|
"│は文字に│",
|
|
"│よる通信│",
|
|
"│を行う場│",
|
|
"└────────┘",
|
|
]);
|
|
terminal.backend().assert_buffer(&expected);
|
|
}
|
|
|
|
#[test]
|
|
fn widgets_paragraph_renders_mixed_width_graphemes() {
|
|
let backend = TestBackend::new(10, 7);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let s = "aコンピュータ上で文字を扱う場合、";
|
|
terminal
|
|
.draw(|f| {
|
|
let size = f.size();
|
|
let text = vec![Line::from(s)];
|
|
let paragraph = Paragraph::new(text)
|
|
.block(Block::default().borders(Borders::ALL))
|
|
.wrap(Wrap { trim: true });
|
|
f.render_widget(paragraph, size);
|
|
})
|
|
.unwrap();
|
|
|
|
let expected = Buffer::with_lines(vec![
|
|
// The internal width is 8 so only 4 slots for double-width characters.
|
|
"┌────────┐",
|
|
"│aコンピ │", // Here we have 1 latin character so only 3 double-width ones can fit.
|
|
"│ュータ上│",
|
|
"│で文字を│",
|
|
"│扱う場合│",
|
|
"│、 │",
|
|
"└────────┘",
|
|
]);
|
|
terminal.backend().assert_buffer(&expected);
|
|
}
|
|
|
|
#[test]
|
|
fn widgets_paragraph_can_wrap_with_a_trailing_nbsp() {
|
|
let nbsp: &str = "\u{00a0}";
|
|
let line = Line::from(vec![Span::raw("NBSP"), Span::raw(nbsp)]);
|
|
let backend = TestBackend::new(20, 3);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
let expected = Buffer::with_lines(vec![
|
|
"┌──────────────────┐",
|
|
"│NBSP\u{00a0} │",
|
|
"└──────────────────┘",
|
|
]);
|
|
terminal
|
|
.draw(|f| {
|
|
let size = f.size();
|
|
|
|
let paragraph = Paragraph::new(line).block(Block::default().borders(Borders::ALL));
|
|
f.render_widget(paragraph, size);
|
|
})
|
|
.unwrap();
|
|
terminal.backend().assert_buffer(&expected);
|
|
}
|
|
#[test]
|
|
fn widgets_paragraph_can_scroll_horizontally() {
|
|
let test_case = |alignment, scroll, expected| {
|
|
let backend = TestBackend::new(20, 10);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
.draw(|f| {
|
|
let size = f.size();
|
|
let text = Text::from(
|
|
"段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line",
|
|
);
|
|
let paragraph = Paragraph::new(text)
|
|
.block(Block::default().borders(Borders::ALL))
|
|
.alignment(alignment)
|
|
.scroll(scroll);
|
|
f.render_widget(paragraph, size);
|
|
})
|
|
.unwrap();
|
|
terminal.backend().assert_buffer(&expected);
|
|
};
|
|
|
|
test_case(
|
|
Alignment::Left,
|
|
(0, 7),
|
|
Buffer::with_lines(vec![
|
|
"┌──────────────────┐",
|
|
"│在可以水平滚动了!│",
|
|
"│ph can scroll hori│",
|
|
"│ine │",
|
|
"│ │",
|
|
"│ │",
|
|
"│ │",
|
|
"│ │",
|
|
"│ │",
|
|
"└──────────────────┘",
|
|
]),
|
|
);
|
|
// only support Alignment::Left
|
|
test_case(
|
|
Alignment::Right,
|
|
(0, 7),
|
|
Buffer::with_lines(vec![
|
|
"┌──────────────────┐",
|
|
"│段落现在可以水平滚│",
|
|
"│Paragraph can scro│",
|
|
"│ Short line│",
|
|
"│ │",
|
|
"│ │",
|
|
"│ │",
|
|
"│ │",
|
|
"│ │",
|
|
"└──────────────────┘",
|
|
]),
|
|
);
|
|
}
|