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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// (C) 2025 - Enzo Lombardi
//! Terminal Widget - scrolling output viewer for program output, logs, and build results.
//!
//! Matches Borland: TTerminal (from Turbo Vision Professional)
//!
//! This is different from the Terminal backend - this is a UI widget for displaying
//! scrolling text output like:
//! - Build output from compilers
//! - Program execution logs
//! - Debug console output
//! - Command line tool output
//!
//! Key features:
//! - Auto-scroll to bottom when new lines are added
//! - Large scrollback buffer (configurable)
//! - Efficient append operations
//! - Optional ANSI color code support
//! - Read-only (unlike Editor)
use crate::core::geometry::Rect;
use crate::core::event::{Event, EventType, KB_UP, KB_DOWN, KB_PGUP, KB_PGDN, KB_HOME, KB_END};
use crate::core::draw::DrawBuffer;
use crate::core::palette::{colors, Attr};
use crate::core::state::StateFlags;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
use super::scrollbar::ScrollBar;
/// A line of output with optional color attributes
#[derive(Clone, Debug)]
pub struct OutputLine {
/// The text content
pub text: String,
/// Optional color attribute (if None, uses default)
pub attr: Option<Attr>,
}
impl OutputLine {
/// Create a new output line with default color
pub fn new(text: String) -> Self {
Self { text, attr: None }
}
/// Create a new output line with specific color
pub fn with_attr(text: String, attr: Attr) -> Self {
Self { text, attr: Some(attr) }
}
}
/// Terminal Widget - scrolling output viewer
/// Matches Borland: TTerminal
pub struct TerminalWidget {
bounds: Rect,
state: StateFlags,
/// Output lines buffer
lines: Vec<OutputLine>,
/// Maximum number of lines to keep (scrollback buffer)
max_lines: usize,
/// Current scroll position (top visible line)
top_line: usize,
/// Auto-scroll to bottom when new lines added
auto_scroll: bool,
/// Vertical scrollbar
v_scrollbar: Option<Box<ScrollBar>>,
owner: Option<*const dyn View>,
owner_type: super::view::OwnerType,
}
impl TerminalWidget {
/// Create a new terminal widget
pub fn new(bounds: Rect) -> Self {
Self {
bounds,
state: 0,
lines: Vec::new(),
max_lines: 10000, // Default: 10k lines scrollback
top_line: 0,
auto_scroll: true,
v_scrollbar: None,
owner: None,
owner_type: super::view::OwnerType::None,
}
}
/// Create with vertical scrollbar
pub fn with_scrollbar(mut self) -> Self {
let v_bounds = Rect::new(
self.bounds.b.x - 1,
self.bounds.a.y,
self.bounds.b.x,
self.bounds.b.y,
);
self.v_scrollbar = Some(Box::new(ScrollBar::new_vertical(v_bounds)));
self
}
/// Set the maximum scrollback buffer size
pub fn set_max_lines(&mut self, max_lines: usize) {
self.max_lines = max_lines;
self.trim_buffer();
}
/// Enable/disable auto-scroll to bottom
pub fn set_auto_scroll(&mut self, auto_scroll: bool) {
self.auto_scroll = auto_scroll;
}
/// Append a line of output
pub fn append_line(&mut self, text: String) {
self.lines.push(OutputLine::new(text));
self.trim_buffer();
if self.auto_scroll {
self.scroll_to_bottom();
}
self.update_scrollbar();
}
/// Append a line with specific color
pub fn append_line_colored(&mut self, text: String, attr: Attr) {
self.lines.push(OutputLine::with_attr(text, attr));
self.trim_buffer();
if self.auto_scroll {
self.scroll_to_bottom();
}
self.update_scrollbar();
}
/// Append multiple lines at once
pub fn append_lines(&mut self, lines: Vec<String>) {
for line in lines {
self.lines.push(OutputLine::new(line));
}
self.trim_buffer();
if self.auto_scroll {
self.scroll_to_bottom();
}
self.update_scrollbar();
}
/// Append text, splitting on newlines
pub fn append_text(&mut self, text: &str) {
for line in text.lines() {
self.lines.push(OutputLine::new(line.to_string()));
}
self.trim_buffer();
if self.auto_scroll {
self.scroll_to_bottom();
}
self.update_scrollbar();
}
/// Clear all output
pub fn clear(&mut self) {
self.lines.clear();
self.top_line = 0;
self.update_scrollbar();
}
/// Get the number of lines
pub fn line_count(&self) -> usize {
self.lines.len()
}
/// Scroll to the bottom
pub fn scroll_to_bottom(&mut self) {
let visible_rows = self.get_visible_rows();
if self.lines.len() > visible_rows {
self.top_line = self.lines.len() - visible_rows;
} else {
self.top_line = 0;
}
}
/// Scroll to the top
pub fn scroll_to_top(&mut self) {
self.top_line = 0;
}
/// Trim buffer to max_lines
fn trim_buffer(&mut self) {
if self.lines.len() > self.max_lines {
let excess = self.lines.len() - self.max_lines;
self.lines.drain(0..excess);
// Adjust scroll position
if self.top_line >= excess {
self.top_line -= excess;
} else {
self.top_line = 0;
}
}
}
/// Get the number of visible rows
fn get_visible_rows(&self) -> usize {
let mut height = self.bounds.height_clamped() as usize;
if self.v_scrollbar.is_some() {
// Account for scrollbar taking up space
height = height.saturating_sub(0); // scrollbar doesn't reduce height
}
height
}
/// Get the visible width
fn get_visible_width(&self) -> usize {
let mut width = self.bounds.width_clamped() as usize;
if self.v_scrollbar.is_some() {
width = width.saturating_sub(1); // scrollbar takes 1 column
}
width
}
/// Update scrollbar state
fn update_scrollbar(&mut self) {
// Compute all values before borrowing v_scrollbar mutably
let visible_rows = self.get_visible_rows();
let total_lines = self.lines.len();
let top_line = self.top_line;
let max_scroll = if total_lines > visible_rows {
total_lines - visible_rows
} else {
0
};
if let Some(ref mut v_bar) = self.v_scrollbar {
v_bar.set_params(
top_line as i32,
0,
max_scroll as i32,
visible_rows as i32,
1,
);
}
}
/// Scroll up by one line
fn scroll_up(&mut self) {
if self.top_line > 0 {
self.top_line -= 1;
self.auto_scroll = false; // Disable auto-scroll when user scrolls
self.update_scrollbar();
}
}
/// Scroll down by one line
fn scroll_down(&mut self) {
let visible_rows = self.get_visible_rows();
if self.top_line + visible_rows < self.lines.len() {
self.top_line += 1;
self.update_scrollbar();
// Re-enable auto-scroll if at bottom
if self.top_line + visible_rows >= self.lines.len() {
self.auto_scroll = true;
}
}
}
/// Page up
fn page_up(&mut self) {
let visible_rows = self.get_visible_rows();
self.top_line = self.top_line.saturating_sub(visible_rows);
self.auto_scroll = false; // Disable auto-scroll when user scrolls
self.update_scrollbar();
}
/// Page down
fn page_down(&mut self) {
let visible_rows = self.get_visible_rows();
let max_scroll = if self.lines.len() > visible_rows {
self.lines.len() - visible_rows
} else {
0
};
self.top_line = (self.top_line + visible_rows).min(max_scroll);
self.update_scrollbar();
// Re-enable auto-scroll if at bottom
if self.top_line + visible_rows >= self.lines.len() {
self.auto_scroll = true;
}
}
}
impl View for TerminalWidget {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
// Update scrollbar bounds
if self.v_scrollbar.is_some() {
let v_bounds = Rect::new(
bounds.b.x - 1,
bounds.a.y,
bounds.b.x,
bounds.b.y,
);
self.v_scrollbar = Some(Box::new(ScrollBar::new_vertical(v_bounds)));
}
self.update_scrollbar();
}
fn draw(&mut self, terminal: &mut Terminal) {
let visible_rows = self.get_visible_rows();
let visible_width = self.get_visible_width();
// Use EDITOR_NORMAL for display (editor uses same color regardless of focus)
let default_color = colors::EDITOR_NORMAL;
// Draw visible lines
for i in 0..visible_rows {
let line_idx = self.top_line + i;
let mut buf = DrawBuffer::new(visible_width);
if line_idx < self.lines.len() {
let line = &self.lines[line_idx];
let color = line.attr.unwrap_or(default_color);
// Truncate or pad line to fit width
let text = if line.text.len() > visible_width {
&line.text[..visible_width]
} else {
&line.text
};
buf.move_str(0, text, color);
// Fill rest with spaces
if text.len() < visible_width {
buf.move_char(text.len(), ' ', color, visible_width - text.len());
}
} else {
// Empty line
buf.move_char(0, ' ', default_color, visible_width);
}
write_line_to_terminal(terminal, self.bounds.a.x, self.bounds.a.y + i as i16, &buf);
}
// Draw scrollbar if present
if let Some(ref mut v_bar) = self.v_scrollbar {
v_bar.draw(terminal);
}
}
fn handle_event(&mut self, event: &mut Event) {
match event.what {
EventType::Keyboard => {
match event.key_code {
KB_UP => {
self.scroll_up();
event.clear();
}
KB_DOWN => {
self.scroll_down();
event.clear();
}
KB_PGUP => {
self.page_up();
event.clear();
}
KB_PGDN => {
self.page_down();
event.clear();
}
KB_HOME => {
self.scroll_to_top();
self.auto_scroll = false;
self.update_scrollbar();
event.clear();
}
KB_END => {
self.scroll_to_bottom();
self.auto_scroll = true;
self.update_scrollbar();
event.clear();
}
_ => {}
}
}
EventType::MouseWheelUp => {
if self.bounds.contains(event.mouse.pos) {
self.scroll_up();
event.clear();
}
}
EventType::MouseWheelDown => {
if self.bounds.contains(event.mouse.pos) {
self.scroll_down();
event.clear();
}
}
_ => {}
}
}
fn state(&self) -> StateFlags {
self.state
}
fn set_state(&mut self, state: StateFlags) {
self.state = state;
}
fn can_focus(&self) -> bool {
true
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{palettes, Palette};
Some(Palette::from_slice(palettes::CP_SCROLLER))
}
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_owner_type(&self) -> super::view::OwnerType {
self.owner_type
}
fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
self.owner_type = owner_type;
}
}
/// Builder for creating terminal widgets with a fluent API.
pub struct TerminalWidgetBuilder {
bounds: Option<Rect>,
with_scrollbar: bool,
max_lines: usize,
auto_scroll: bool,
}
impl TerminalWidgetBuilder {
pub fn new() -> Self {
Self {
bounds: None,
with_scrollbar: false,
max_lines: 10000,
auto_scroll: true,
}
}
#[must_use]
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = Some(bounds);
self
}
#[must_use]
pub fn with_scrollbar(mut self, with_scrollbar: bool) -> Self {
self.with_scrollbar = with_scrollbar;
self
}
#[must_use]
pub fn max_lines(mut self, max_lines: usize) -> Self {
self.max_lines = max_lines;
self
}
#[must_use]
pub fn auto_scroll(mut self, auto_scroll: bool) -> Self {
self.auto_scroll = auto_scroll;
self
}
pub fn build(self) -> TerminalWidget {
let bounds = self.bounds.expect("TerminalWidget bounds must be set");
let mut widget = TerminalWidget::new(bounds);
if self.with_scrollbar {
widget = widget.with_scrollbar();
}
widget.set_max_lines(self.max_lines);
widget.set_auto_scroll(self.auto_scroll);
widget
}
pub fn build_boxed(self) -> Box<TerminalWidget> {
Box::new(self.build())
}
}
impl Default for TerminalWidgetBuilder {
fn default() -> Self {
Self::new()
}
}