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
use crate::cells::CellBuffer;
use crate::styles::*;
use fltk::app;
use fltk::{
draw,
enums::{Align, Color, Font, FrameType},
group,
prelude::*,
};
use std::sync::{Arc, Mutex};
#[derive(Clone, Copy, Debug, Default)]
pub struct Selection {
pub start: Option<(usize, usize)>,
pub end: Option<(usize, usize)>,
}
pub struct TermCanvas {
f: group::Group,
buffer: Arc<Mutex<CellBuffer>>,
scroll: Option<group::Scroll>,
blink: Arc<Mutex<bool>>,
selection: Arc<Mutex<Selection>>,
}
impl TermCanvas {
pub fn new<L: Into<Option<&'static str>>>(x: i32, y: i32, w: i32, h: i32, label: L) -> Self {
let mut f = group::Group::new(x, y, w, h, label);
f.set_frame(FrameType::FlatBox);
let default_bg = BLACK;
let default_fg = WHITE;
Self {
f,
buffer: Arc::new(Mutex::new(CellBuffer::new(2000, default_bg, default_fg))),
scroll: None,
blink: Arc::new(Mutex::new(true)),
selection: Arc::new(Mutex::new(Selection::default())),
}
}
pub fn set_size(&mut self, w: i32, h: i32) {
self.f.set_size(w, h);
}
pub fn widget(&self) -> &group::Group {
&self.f
}
pub fn set_buffer(&mut self, buffer: Arc<Mutex<CellBuffer>>) {
self.buffer = buffer.clone();
let blink_state = self.blink.clone();
let selection = self.selection.clone();
// Rebind draw closure to capture buffer and optional scroll.
self.f.draw(move |f| {
let x = f.x();
let y = f.y();
let w = f.w();
let h = f.h();
draw::set_draw_color(BLACK);
draw::draw_rectf(x, y, w, h);
let mut font = Font::Courier;
let font_size = 14;
draw::set_font(font, font_size);
let line_h = draw::height().max(14);
let pad_x = 6;
let mut yy = y + line_h;
let char_w = ((draw::width("M") as f32).ceil() as i32).max(1);
let cols = ((w - 2 * pad_x).max(1)) / char_w.max(1);
// Snapshot selection range (visual coords)
let sel = {
let se = selection.lock().ok();
let (s, e) = if let Some(ref g) = se {
(g.start, g.end)
} else {
(None, None)
};
match (s, e) {
(Some(mut a), Some(mut b)) => {
if b < a {
std::mem::swap(&mut a, &mut b);
}
Some((a, b))
}
_ => None,
}
};
if let Ok(buf) = buffer.lock() {
draw::set_draw_color(buf.default_bg);
draw::draw_rectf(x, y, w, h);
let mut vline_idx: usize = 0;
for line in buf.snapshot().iter() {
// wrap-aware iteration
let total_cols = line.len() as i32;
let mut col_used = 0i32;
let mut run: String = String::new();
let mut cur_fg = WHITE;
let mut cur_bg = BLACK;
let mut cur_bold = false;
let mut cur_underline = false;
let mut first = true;
let mut xx = x + pad_x;
let mut run_start_col: i32 = 0; // visual column where current run starts
// draw selection outline for this visual line
if let Some(((ls, cs), (le, ce))) = sel {
if vline_idx >= ls && vline_idx <= le {
let start_col = if vline_idx == ls { cs as i32 } else { 0 };
let end_col = if vline_idx == le { ce as i32 } else { cols - 1 };
if end_col >= start_col {
let sx = x + pad_x + start_col * char_w;
let sw = (end_col - start_col + 1) * char_w;
draw::set_draw_color(BLUE);
draw::draw_rect(sx, yy - line_h, sw, line_h);
}
}
}
let mut draw_run = |run: &str,
fg: Color,
bg: Color,
bold: bool,
underline: bool,
xx: &mut i32,
yy: i32,
w: i32,
col_pos: i32,
vline: usize| {
if run.is_empty() {
return;
}
if *xx == x + pad_x {
// clear whole visual line background at start of segment
draw::set_draw_color(buf.default_bg);
draw::draw_rectf(x + pad_x, yy - line_h, w - 2 * pad_x, line_h);
}
let (tw, _th) = draw::measure(run, false);
// background for this run
draw::set_draw_color(bg);
draw::draw_rectf(*xx, yy - line_h, tw, line_h);
// selection overlay (behind text)
if let Some(((ls, cs), (le, ce))) = sel {
if vline >= ls && vline <= le {
let sel_start = if vline == ls { cs as i32 } else { 0 };
let sel_end = if vline == le { ce as i32 } else { cols - 1 };
let run_cols = run.chars().count() as i32;
let overlap_start = sel_start.max(col_pos);
let overlap_end = sel_end.min(col_pos + run_cols - 1);
if overlap_end >= overlap_start {
let sx = x + pad_x + overlap_start * char_w;
let sw = (overlap_end - overlap_start + 1) * char_w;
draw::set_draw_color(BLUE);
draw::draw_rectf(sx, yy - line_h, sw, line_h);
}
}
}
// font weight
font = if bold {
Font::CourierBold
} else {
Font::Courier
};
draw::set_draw_color(fg);
draw::draw_text2(run, *xx, yy - line_h, w - *xx, line_h, Align::Left);
if underline {
let underline_y = yy - 2;
draw::set_draw_color(fg);
draw::draw_line(*xx, underline_y, *xx + tw, underline_y);
}
*xx += tw;
};
for c in line.iter() {
let (fg, bg) = (c.style.fg, c.style.bg);
let bold = c.style.bold;
let underline = c.style.underline;
let (fg_eff, bg_eff) = if c.style.inverse { (bg, fg) } else { (fg, bg) };
if first
|| fg_eff != cur_fg
|| bg_eff != cur_bg
|| bold != cur_bold
|| underline != cur_underline
{
if !first {
draw_run(
&run,
cur_fg,
cur_bg,
cur_bold,
cur_underline,
&mut xx,
yy,
w,
run_start_col,
vline_idx,
);
run.clear();
}
cur_fg = fg_eff;
cur_bg = bg_eff;
cur_bold = bold;
cur_underline = underline;
first = false;
// New run starts at current visual column
run_start_col = (col_used % cols) as i32;
}
run.push(c.ch);
col_used += 1;
// soft-wrap when reaching cols
if cols > 0 && col_used % cols == 0 {
// flush current run and move to next visual line
draw_run(
&run,
cur_fg,
cur_bg,
cur_bold,
cur_underline,
&mut xx,
yy,
w,
run_start_col,
vline_idx,
);
run.clear();
yy += line_h;
vline_idx += 1;
run_start_col = 0;
xx = x + pad_x;
if yy > y + h {
break;
}
}
}
if !run.is_empty() && yy <= y + h {
draw_run(
&run,
cur_fg,
cur_bg,
cur_bold,
cur_underline,
&mut xx,
yy,
w,
run_start_col,
vline_idx,
);
}
if total_cols == 0 {
// clear empty visual line
draw::set_draw_color(buf.default_bg);
draw::draw_rectf(x + pad_x, yy - line_h, w - 2 * pad_x, line_h);
yy += line_h;
vline_idx += 1;
} else if total_cols % cols != 0 {
yy += line_h;
vline_idx += 1;
}
if yy > y + h {
break;
}
}
// Cursor rendering (blinking block)
if let Ok(b) = blink_state.lock() {
if *b {
let (crow, ccol) = buf.cursor();
// Map to visual position
let cols = ((w - 2 * pad_x).max(1))
/ ((draw::width("M") as f32).ceil() as i32).max(1);
let before_lines = buf
.snapshot()
.iter()
.take(crow)
.map(|l| (l.len().max(1) as i32 + cols - 1) / cols)
.sum::<i32>();
let seg = (ccol as i32) / cols;
let col = (ccol as i32) % cols;
let cx = x + pad_x + col * ((draw::width("M") as f32).ceil() as i32);
let cy = y + line_h + (before_lines + seg) * line_h;
if cy - line_h >= y && cy <= y + h {
draw::set_draw_color(buf.default_fg);
draw::draw_rectf(
cx,
cy - line_h,
(draw::width("M") as f32).ceil() as i32,
line_h,
);
}
}
}
return;
}
// Fallback
draw::set_draw_color(WHITE);
});
}
pub fn set_scroll(&mut self, scroll: group::Scroll) {
self.scroll = Some(scroll.clone());
// If buffer already attached, rebind draw closure to capture scroll
let buf = self.buffer.clone();
self.set_buffer(buf);
}
pub fn start_blink(&mut self, interval: f64) {
let blink_state = self.blink.clone();
let mut frame = self.f.clone();
app::add_timeout3(interval, move |h| {
if let Ok(mut b) = blink_state.lock() {
*b = !*b;
}
frame.redraw();
app::repeat_timeout3(interval, h);
});
}
fn mouse_to_vpos(&self, x: i32, y: i32) -> (usize, usize) {
let line_h = draw::height().max(14);
let char_w = ((draw::width("M") as f32).ceil() as i32).max(1);
let pad_x = 6;
let col = ((x - self.f.x() - pad_x).max(0) / char_w) as usize;
let row = ((y - self.f.y()).max(0) / line_h) as usize;
(row, col)
}
pub fn begin_selection_at(&mut self, x: i32, y: i32) {
let vpos = self.mouse_to_vpos(x, y);
if let Ok(mut sel) = self.selection.lock() {
sel.start = Some(vpos);
sel.end = Some(vpos);
}
self.f.redraw();
}
pub fn update_selection_at(&mut self, x: i32, y: i32) {
let vpos = self.mouse_to_vpos(x, y);
if let Ok(mut sel) = self.selection.lock() {
sel.end = Some(vpos);
}
self.f.redraw();
}
pub fn clear_selection(&mut self) {
if let Ok(mut sel) = self.selection.lock() {
sel.start = None;
sel.end = None;
}
self.f.redraw();
}
pub fn copy_selection_to_clipboard(&self) {
let buf_arc = self.buffer.clone();
if let Ok(buf) = buf_arc.lock() {
let snap = buf.snapshot();
let char_w = ((draw::width("M") as f32).ceil() as i32).max(1);
let pad_x = 6;
let cols = ((self.f.w() - 2 * pad_x).max(1) / char_w).max(1) as usize;
// Build visual lines
let mut visual: Vec<Vec<char>> = Vec::new();
for line in snap.iter() {
let row: Vec<char> = line.iter().map(|c| c.ch).collect();
if row.is_empty() {
visual.push(Vec::new());
continue;
}
for chunk in row.chunks(cols) {
visual.push(chunk.to_vec());
}
}
let (s, e) = if let Ok(sel) = self.selection.lock() {
(sel.start, sel.end)
} else {
(None, None)
};
if let (Some(mut a), Some(mut b)) = (s, e) {
if b < a {
std::mem::swap(&mut a, &mut b);
}
let mut out = String::new();
for v in a.0..=b.0 {
if v >= visual.len() {
break;
}
let line = &visual[v];
let start = if v == a.0 { a.1.min(line.len()) } else { 0 };
let end = if v == b.0 {
(b.1 + 1).min(line.len())
} else {
line.len()
};
if start < end {
for ch in &line[start..end] {
out.push(*ch);
}
}
if v != b.0 {
out.push('\n');
}
}
if !out.is_empty() {
app::copy(&out);
}
}
};
}
#[allow(clippy::type_complexity)]
pub fn selection_handle(&self) -> Arc<Mutex<Selection>> {
self.selection.clone()
}
pub fn buffer_arc(&self) -> Arc<Mutex<CellBuffer>> {
self.buffer.clone()
}
}
fltk::widget_extends!(TermCanvas, group::Group, f);