1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Container renderables — collections of lines and renderables.
//!
//! Provides [`Lines`] for rendering a collection of lines with optional
//! highlight support, and [`Renderables`] for rendering a sequence of
//! independent renderables one after another.
use crate::console::{ConsoleOptions, DynRenderable, Renderable, RenderResult};
use crate::segment::Segment;
use crate::style::Style;
// ---------------------------------------------------------------------------
// Lines
// ---------------------------------------------------------------------------
/// A collection of lines (each line is a [`Renderable`]).
///
/// Each item in [`Lines`] is rendered as a single line of output. The
/// `highlight` option applies a style to a specific 0-indexed line.
///
/// # Example
///
/// ```rust
/// use rusty_rich::Lines;
///
/// let mut lines = Lines::new();
/// lines.add("First line");
/// lines.add("Second line");
/// ```
#[derive(Debug, Clone)]
pub struct Lines {
lines: Vec<DynRenderable>,
highlight: Option<usize>,
style: Style,
}
impl Default for Lines {
fn default() -> Self {
Self::new()
}
}
impl Lines {
/// Create a new empty [`Lines`] container.
pub fn new() -> Self {
Self {
lines: Vec::new(),
highlight: None,
style: Style::new(),
}
}
/// Add a renderable line to the container.
pub fn add(&mut self, renderable: impl Renderable + Send + Sync + 'static) -> &mut Self {
self.lines.push(DynRenderable::new(renderable));
self
}
/// Builder: highlight the line at the given 0-based index.
pub fn highlight(mut self, index: usize) -> Self {
self.highlight = Some(index);
self
}
/// Builder: set the default style for all lines.
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
}
impl Renderable for Lines {
fn render(&self, options: &ConsoleOptions) -> RenderResult {
let mut all_lines: Vec<Vec<Segment>> = Vec::new();
for (i, item) in self.lines.iter().enumerate() {
let mut result = item.render(options);
// Apply highlight style to the highlighted line
if Some(i) == self.highlight {
for line in &mut result.lines {
for seg in line.iter_mut() {
if let Some(ref existing) = seg.style {
seg.style = Some(existing.clone().bold(true));
} else {
seg.style = Some(self.style.clone().bold(true));
}
}
}
} else if !self.style.is_plain() {
for line in &mut result.lines {
for seg in line.iter_mut() {
if seg.style.is_none() {
seg.style = Some(self.style.clone());
}
}
}
}
all_lines.extend(result.lines);
}
RenderResult {
lines: all_lines,
items: Vec::new(),
}
}
}
// ---------------------------------------------------------------------------
// Renderables
// ---------------------------------------------------------------------------
/// A flexible container for multiple renderables.
///
/// Renders each contained renderable in sequence, concatenating their
/// output lines.
///
/// # Example
///
/// ```rust
/// use rusty_rich::Renderables;
///
/// let mut items = Renderables::new();
/// items.add("First item");
/// items.add("Second item");
/// ```
#[derive(Debug, Clone)]
pub struct Renderables {
items: Vec<DynRenderable>,
}
impl Default for Renderables {
fn default() -> Self {
Self::new()
}
}
impl Renderables {
/// Create a new empty [`Renderables`] container.
pub fn new() -> Self {
Self {
items: Vec::new(),
}
}
/// Add a renderable to the container.
pub fn add(&mut self, renderable: impl Renderable + Send + Sync + 'static) -> &mut Self {
self.items.push(DynRenderable::new(renderable));
self
}
}
impl Renderable for Renderables {
fn render(&self, options: &ConsoleOptions) -> RenderResult {
let mut all_lines: Vec<Vec<Segment>> = Vec::new();
for item in &self.items {
let result = item.render(options);
all_lines.extend(result.lines);
}
RenderResult {
lines: all_lines,
items: Vec::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::console::ConsoleOptions;
#[test]
fn test_lines_empty() {
let lines = Lines::new();
let opts = ConsoleOptions::default();
let result = lines.render(&opts);
assert!(result.lines.is_empty());
}
#[test]
fn test_lines_with_content() {
let mut lines = Lines::new();
lines.add("Hello");
lines.add("World");
let opts = ConsoleOptions::default();
let result = lines.render(&opts);
assert_eq!(result.lines.len(), 2);
}
#[test]
fn test_lines_highlight() {
let mut lines = Lines::new().highlight(1);
lines.add("First");
lines.add("Highlighted");
lines.add("Third");
let opts = ConsoleOptions::default();
let result = lines.render(&opts);
assert_eq!(result.lines.len(), 3);
}
#[test]
fn test_renderables_empty() {
let items = Renderables::new();
let opts = ConsoleOptions::default();
let result = items.render(&opts);
assert!(result.lines.is_empty());
}
#[test]
fn test_renderables_with_content() {
let mut items = Renderables::new();
items.add("A");
items.add("B");
items.add("C");
let opts = ConsoleOptions::default();
let result = items.render(&opts);
assert_eq!(result.lines.len(), 3);
}
}