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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// (C) 2025 - Enzo Lombardi
//! ANSI art background view - displays colored ASCII art from ANSI escape sequences.
//!
//! This view renders ANSI art centered on the desktop background, supporting
//! 16-color, 256-color, and true color ANSI escape sequences.
//!
//! # Example
//!
//! ```rust,no_run
//! use turbo_vision::views::ansi_background::AnsiBackground;
//! use turbo_vision::core::geometry::Rect;
//! use turbo_vision::core::palette::{Attr, TvColor};
//!
//! // Load from file
//! let bg = AnsiBackground::from_file(
//! Rect::new(0, 0, 80, 24),
//! "logo.ans",
//! Attr::new(TvColor::LightGray, TvColor::DarkGray),
//! ).expect("Failed to load ANSI file");
//!
//! // Or from string
//! let ansi_art = "\x1b[31mRed\x1b[0m Logo";
//! let bg = AnsiBackground::from_string(
//! Rect::new(0, 0, 80, 24),
//! ansi_art,
//! Attr::new(TvColor::LightGray, TvColor::DarkGray),
//! );
//! ```
use crate::core::ansi::AnsiImage;
use crate::core::draw::DrawBuffer;
use crate::core::event::Event;
use crate::core::geometry::Rect;
use crate::core::palette::Attr;
use crate::core::state::StateFlags;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
use std::io;
use std::path::Path;
/// A background view that displays ANSI art.
pub struct AnsiBackground {
bounds: Rect,
state: StateFlags,
/// The parsed ANSI image.
image: AnsiImage,
/// Default attribute for areas outside the image.
default_attr: Attr,
/// Whether to center the image horizontally.
center_x: bool,
/// Whether to center the image vertically.
center_y: bool,
owner: Option<*const dyn View>,
}
impl AnsiBackground {
/// Creates a new ANSI background from an `AnsiImage`.
///
/// # Arguments
/// * `bounds` - The bounding rectangle for the view
/// * `image` - The parsed ANSI image to display
/// * `default_attr` - Color attributes for areas outside the image
pub fn new(bounds: Rect, image: AnsiImage, default_attr: Attr) -> Self {
Self {
bounds,
state: 0,
image,
default_attr,
center_x: true,
center_y: true,
owner: None,
}
}
/// Creates a new ANSI background by loading from a file.
///
/// # Arguments
/// * `bounds` - The bounding rectangle for the view
/// * `path` - Path to the ANSI art file
/// * `default_attr` - Color attributes for areas outside the image
///
/// # Errors
/// Returns an IO error if the file cannot be read.
pub fn from_file<P: AsRef<Path>>(
bounds: Rect,
path: P,
default_attr: Attr,
) -> io::Result<Self> {
let image = AnsiImage::load(path)?;
Ok(Self::new(bounds, image, default_attr))
}
/// Creates a new ANSI background from a string.
///
/// # Arguments
/// * `bounds` - The bounding rectangle for the view
/// * `content` - String containing ANSI escape sequences
/// * `default_attr` - Color attributes for areas outside the image
pub fn from_string(bounds: Rect, content: &str, default_attr: Attr) -> Self {
let image = AnsiImage::parse(content);
Self::new(bounds, image, default_attr)
}
/// Sets whether to center the image horizontally.
#[must_use]
pub fn center_x(mut self, center: bool) -> Self {
self.center_x = center;
self
}
/// Sets whether to center the image vertically.
#[must_use]
pub fn center_y(mut self, center: bool) -> Self {
self.center_y = center;
self
}
/// Sets whether to center the image both horizontally and vertically.
#[must_use]
pub fn centered(mut self, center: bool) -> Self {
self.center_x = center;
self.center_y = center;
self
}
/// Gets the ANSI image.
pub fn image(&self) -> &AnsiImage {
&self.image
}
/// Sets a new ANSI image.
pub fn set_image(&mut self, image: AnsiImage) {
self.image = image;
}
/// Loads a new image from a file.
pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
self.image = AnsiImage::load(path)?;
Ok(())
}
/// Parses and sets content from a string.
pub fn set_content(&mut self, content: &str) {
self.image = AnsiImage::parse(content);
}
}
impl View for AnsiBackground {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn state(&self) -> StateFlags {
self.state
}
fn set_state(&mut self, state: StateFlags) {
self.state = state;
}
fn draw(&mut self, terminal: &mut Terminal) {
let width = self.bounds.width_clamped() as usize;
let height = self.bounds.height() as usize;
// Calculate offsets for centering
let x_offset = if self.center_x && self.image.width < width {
(width - self.image.width) / 2
} else {
0
};
let y_offset = if self.center_y && self.image.height < height {
(height - self.image.height) / 2
} else {
0
};
// Draw each row
for row in 0..height {
let mut buf = DrawBuffer::new(width);
// Fill entire line with default attribute
buf.move_char(0, ' ', self.default_attr, width);
// Calculate which image row to draw (if any)
if row >= y_offset && row < y_offset + self.image.height {
let image_row = row - y_offset;
if let Some(line) = self.image.lines.get(image_row) {
// Draw the image line at the offset position
for (col, cell) in line.iter().enumerate() {
let x = x_offset + col;
if x < width {
buf.put_char(x, cell.ch, cell.attr);
}
}
}
}
write_line_to_terminal(
terminal,
self.bounds.a.x,
self.bounds.a.y + row as i16,
&buf,
);
}
}
fn handle_event(&mut self, _event: &mut Event) {
// Background doesn't handle events
}
fn set_owner(&mut self, owner: *const dyn View) {
self.owner = Some(owner);
}
fn get_owner(&self) -> Option<*const dyn View> {
self.owner
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{palettes, Palette};
Some(Palette::from_slice(palettes::CP_BACKGROUND))
}
}
/// Builder for creating ANSI backgrounds with a fluent API.
pub struct AnsiBackgroundBuilder {
bounds: Option<Rect>,
image: Option<AnsiImage>,
default_attr: Option<Attr>,
center_x: bool,
center_y: bool,
}
impl AnsiBackgroundBuilder {
/// Creates a new builder.
pub fn new() -> Self {
Self {
bounds: None,
image: None,
default_attr: None,
center_x: true,
center_y: true,
}
}
/// Sets the bounds.
#[must_use]
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = Some(bounds);
self
}
/// Sets the image from a file path.
#[must_use]
pub fn file<P: AsRef<Path>>(mut self, path: P) -> Self {
if let Ok(image) = AnsiImage::load(path) {
self.image = Some(image);
}
self
}
/// Sets the image from a string.
#[must_use]
pub fn content(mut self, content: &str) -> Self {
self.image = Some(AnsiImage::parse(content));
self
}
/// Sets the image directly.
#[must_use]
pub fn image(mut self, image: AnsiImage) -> Self {
self.image = Some(image);
self
}
/// Sets the default attribute.
#[must_use]
pub fn default_attr(mut self, attr: Attr) -> Self {
self.default_attr = Some(attr);
self
}
/// Sets horizontal centering.
#[must_use]
pub fn center_x(mut self, center: bool) -> Self {
self.center_x = center;
self
}
/// Sets vertical centering.
#[must_use]
pub fn center_y(mut self, center: bool) -> Self {
self.center_y = center;
self
}
/// Sets both centering options.
#[must_use]
pub fn centered(mut self, center: bool) -> Self {
self.center_x = center;
self.center_y = center;
self
}
/// Builds the `AnsiBackground`.
///
/// # Panics
/// Panics if bounds, image, or `default_attr` are not set.
pub fn build(self) -> AnsiBackground {
let mut bg = AnsiBackground::new(
self.bounds.expect("bounds must be set"),
self.image.expect("image must be set"),
self.default_attr.expect("default_attr must be set"),
);
bg.center_x = self.center_x;
bg.center_y = self.center_y;
bg
}
/// Builds and boxes the `AnsiBackground`.
pub fn build_boxed(self) -> Box<AnsiBackground> {
Box::new(self.build())
}
}
impl Default for AnsiBackgroundBuilder {
fn default() -> Self {
Self::new()
}
}