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
use crate::charset::Charset;
use crate::theme::Theme;
use crate::video_buffer::{self, Cell, VideoBuffer};
use crossterm::style::Color;
/// A modal info window with border and title (for Help, About, etc.)
pub struct InfoWindow {
pub width: u16,
pub height: u16,
pub x: u16,
pub y: u16,
pub title: String,
pub content: Vec<String>, // Pre-formatted lines of content
}
impl InfoWindow {
/// Create a new info window (auto-sized and centered)
pub fn new(title: String, content_text: &str, buffer_width: u16, buffer_height: u16) -> Self {
// Split content into lines
let content: Vec<String> = content_text.lines().map(|s| s.to_string()).collect();
// Calculate dimensions based on content
let max_line_width = content
.iter()
.map(|line| Self::strip_color_codes(line).len())
.max()
.unwrap_or(40) as u16;
// Width: content + padding + borders
let width = (max_line_width + 6).min(buffer_width - 4); // 6 = 2 borders + 4 padding
// Height: title + content + padding + borders + instruction
let content_lines = content.len() as u16;
let height = (content_lines + 6).min(buffer_height - 4); // 6 = top border + 2 padding + bottom padding + instruction + bottom border
// Center on screen
let x = (buffer_width.saturating_sub(width)) / 2;
let y = (buffer_height.saturating_sub(height)) / 2;
Self {
width,
height,
x,
y,
title,
content,
}
}
/// Strip color codes from a string for length calculation
fn strip_color_codes(s: &str) -> String {
let mut result = String::new();
let mut chars = s.chars();
while let Some(ch) = chars.next() {
if ch == '{' {
// Skip until we find '}'
for next in chars.by_ref() {
if next == '}' {
break;
}
}
} else {
result.push(ch);
}
}
result
}
/// Parse color code and return the corresponding Color
fn parse_color_code(code: &str) -> Option<Color> {
match code {
"Y" | "y" => Some(Color::Yellow),
"C" | "c" => Some(Color::Cyan),
"W" | "w" => Some(Color::White),
"G" | "g" => Some(Color::Green),
"R" | "r" => Some(Color::Red),
"M" | "m" => Some(Color::Magenta),
"B" | "b" => Some(Color::Blue),
"DG" | "dg" => Some(Color::DarkGrey),
_ => None,
}
}
/// Render the info window to the video buffer
pub fn render(&self, buffer: &mut VideoBuffer, charset: &Charset, theme: &Theme) {
let title_bg = theme.config_title_bg;
let title_fg = theme.config_title_fg;
let border_color = theme.config_border;
let content_bg = theme.config_content_bg;
let content_fg = theme.config_content_fg;
// Get border characters
let top_left = charset.border_top_left();
let top_right = charset.border_top_right();
let bottom_left = charset.border_bottom_left();
let bottom_right = charset.border_bottom_right();
let horizontal = charset.border_horizontal();
let vertical = charset.border_vertical();
// Draw top border with title
buffer.set(
self.x,
self.y,
Cell::new(top_left, border_color, content_bg),
);
let title_text = format!(" {} ", self.title);
let title_start = self.x + (self.width - title_text.len() as u16) / 2;
// Fill top border before title
for x in 1..title_start - self.x {
buffer.set(
self.x + x,
self.y,
Cell::new(horizontal, border_color, content_bg),
);
}
// Render title with special background
for (i, ch) in title_text.chars().enumerate() {
buffer.set(
title_start + i as u16,
self.y,
Cell::new(ch, title_fg, title_bg),
);
}
// Fill top border after title
for x in (title_start + title_text.len() as u16 - self.x)..(self.width - 1) {
buffer.set(
self.x + x,
self.y,
Cell::new(horizontal, border_color, content_bg),
);
}
buffer.set(
self.x + self.width - 1,
self.y,
Cell::new(top_right, border_color, content_bg),
);
// Draw content area with side borders
for y in 1..(self.height - 1) {
// Left border
buffer.set(
self.x,
self.y + y,
Cell::new(vertical, border_color, content_bg),
);
// Content area
for x in 1..(self.width - 1) {
buffer.set(
self.x + x,
self.y + y,
Cell::new(' ', content_fg, content_bg),
);
}
// Right border
buffer.set(
self.x + self.width - 1,
self.y + y,
Cell::new(vertical, border_color, content_bg),
);
}
// Draw bottom border
buffer.set(
self.x,
self.y + self.height - 1,
Cell::new(bottom_left, border_color, content_bg),
);
for x in 1..(self.width - 1) {
buffer.set(
self.x + x,
self.y + self.height - 1,
Cell::new(horizontal, border_color, content_bg),
);
}
buffer.set(
self.x + self.width - 1,
self.y + self.height - 1,
Cell::new(bottom_right, border_color, content_bg),
);
// Render content lines with color code support
let content_start_y = self.y + 2; // Start after border and padding
let content_x = self.x + 3; // Left padding
for (i, line) in self.content.iter().enumerate() {
let line_y = content_start_y + i as u16;
if line_y >= self.y + self.height - 2 {
break; // Don't draw past the instruction area
}
// Render line with color code parsing
let mut current_x = content_x;
let mut current_color = content_fg;
let mut chars = line.chars();
while let Some(ch) = chars.next() {
if ch == '{' {
// Collect color code
let mut code = String::new();
for next in chars.by_ref() {
if next == '}' {
// Apply color if valid
if let Some(color) = Self::parse_color_code(&code) {
current_color = color;
}
break;
}
code.push(next);
}
} else {
// Render character with current color
if current_x < self.x + self.width - 3 {
buffer.set(current_x, line_y, Cell::new(ch, current_color, content_bg));
current_x += 1;
}
}
}
}
// Render instruction at bottom
let instruction = "Press ESC to close";
let instruction_x = self.x + (self.width - instruction.len() as u16) / 2;
let instruction_y = self.y + self.height - 2;
for (i, ch) in instruction.chars().enumerate() {
buffer.set(
instruction_x + i as u16,
instruction_y,
Cell::new(ch, theme.config_instructions_fg, content_bg),
);
}
// Render shadow
video_buffer::render_shadow(
buffer,
self.x,
self.y,
self.width,
self.height,
charset,
theme,
);
}
/// Check if a point is inside the window bounds
#[allow(dead_code)]
pub fn contains_point(&self, x: u16, y: u16) -> bool {
x >= self.x && x < self.x + self.width && y >= self.y && y < self.y + self.height
}
}