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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
use ratatui::{
buffer::Buffer,
layout::{Alignment, Rect},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Paragraph, Widget, Wrap},
};
use std::path::PathBuf;
const BUTTON_BG_RGB: (u8, u8, u8) = (0x09, 0x67, 0xb8);
const BUTTON_FG_RGB: (u8, u8, u8) = (0xff, 0xff, 0xff);
const BODY_FG_RGB: (u8, u8, u8) = (0xb0, 0xb8, 0xc8);
const TITLE_FG_RGB: (u8, u8, u8) = (0xff, 0xff, 0xff);
const FOCUS_BORDER_RGB: (u8, u8, u8) = (0x4e, 0x9a, 0xff);
// --- Debug-panel (Variant A "Darcula") palette. Fixed darks chosen to read
// well on both the Black (#000) and Dark Blue (#1e222e) theme backgrounds. ---
const DBG_BAR_BG: Color = Color::Rgb(0x1c, 0x27, 0x33); // section-header bar fill
const DBG_BAR_FG: Color = Color::Rgb(0x8f, 0xd9, 0xcf); // section-header text
const DBG_FRAME_BG: Color = Color::Rgb(0x20, 0x2a, 0x37); // selected-frame fill
const DBG_ACCENT: Color = Color::Rgb(0x5c, 0xd6, 0xc8); // brand teal stripe / caret
const DBG_SCOPE: Color = Color::Rgb(0xc8, 0xa0, 0x6a); // Locals / Globals (gold)
const DBG_NAME: Color = Color::Rgb(0xbc, 0xbe, 0xc4); // identifiers / var names
const DBG_EQ: Color = Color::Rgb(0x6b, 0x72, 0x80); // "="
const DBG_TYPE: Color = Color::Rgb(0x78, 0x7c, 0x80); // ": type"
const DBG_STR: Color = Color::Rgb(0x6a, 0xab, 0x73); // strings / collections
const DBG_NUM: Color = Color::Rgb(0x2a, 0xac, 0xb8); // numbers
const DBG_KW: Color = Color::Rgb(0xcf, 0x8e, 0x6d); // None / bool
const DBG_CHEV: Color = Color::Rgb(0x5c, 0x66, 0x75); // expand chevrons
const DBG_LOC: Color = Color::Rgb(0x6b, 0x76, 0x89); // file:line / dim console
const DBG_PILL_BG: Color = Color::Rgb(0x0e, 0x7e, 0x76); // status pill
const DBG_PILL_FG: Color = Color::Rgb(0xdf, 0xfd, 0xf7);
const DBG_FRAME_SEL_FG: Color = Color::Rgb(0xff, 0xff, 0xff);
const DBG_FIELD_BG: Color = Color::Rgb(0x0d, 0x11, 0x17); // REPL input field fill
const DBG_PLACEHOLDER: Color = Color::Rgb(0x4b, 0x55, 0x63); // ghost hint text
/// Write `s` at `(x, y)` styled, clipped to the half-open column range
/// `[x, right)`, and return the column just past what was drawn. Lets the
/// renderer lay coloured segments left-to-right without overflowing the panel.
fn put(buf: &mut Buffer, x: u16, y: u16, right: u16, s: &str, style: Style) -> u16 {
if x >= right {
return x;
}
let budget = (right - x) as usize;
let clipped: String = s.chars().take(budget).collect();
let drawn = clipped.chars().count() as u16;
buf.set_string(x, y, &clipped, style);
x + drawn
}
/// Add a background colour to `style` when `bg` is `Some` (so a selected frame's
/// segments share its highlight fill).
fn with_bg(style: Style, bg: Option<Color>) -> Style {
match bg {
Some(c) => style.bg(c),
None => style,
}
}
/// Colour a Python value by its `type_name` (PyCharm/Darcula-ish): strings and
/// collections green, numbers cyan, None/bool orange, everything else (objects)
/// the neutral name grey.
fn value_color(type_name: &str, theme: crate::theme::Theme) -> Color {
match type_name {
"str" => theme.ui(DBG_STR),
"int" | "float" | "complex" => theme.ui(DBG_NUM),
"bool" | "NoneType" => theme.ui(DBG_KW),
"list" | "tuple" | "dict" | "set" | "frozenset" | "bytes" | "bytearray" => {
theme.ui(DBG_STR)
}
_ => theme.ui(DBG_NAME),
}
}
/// Cells reserved above the headline for the OSC-1337 debug-alt icon
/// overlay. Six cells wide × three cells tall lands a roughly 60×60-pixel
/// codicon at typical iTerm2 cell sizes (10×20 px), matching the VS Code
/// empty-state proportions the user supplied. The image is painted post-
/// frame by `App::flush_run_debug_icon_overlay`; on view change away from
/// Run-Debug the App's `terminal.clear()` evicts the cached image cells
/// (the same pipeline the welcome wordmark and editor preview use).
pub const RUN_DEBUG_ICON_CELLS_W: u16 = 6;
pub const RUN_DEBUG_ICON_CELLS_H: u16 = 3;
/// What a [`DebugRow`] represents, so a click can act on it (navigate to a
/// frame, expand a variable) and so the renderer can colour each part. Each
/// variant carries the structured pieces the renderer styles independently
/// (name vs value vs type), PyCharm-style.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DebugRowKind {
/// A section header ("CALL STACK", "VARIABLES") drawn as a filled bar.
Header { title: String },
/// A call-stack frame; `name` is the function, `location` is `file:line`.
Frame {
id: i64,
selected: bool,
name: String,
location: String,
},
/// A scope container row ("Locals", "Globals").
Scope { name: String },
/// A variable. `reference > 0` means expandable; `value`/`type_name` are
/// coloured by type.
Variable {
reference: i64,
expandable: bool,
expanded: bool,
name: String,
value: String,
type_name: String,
},
/// A watch expression (#112). `available` false renders `<not available>`
/// (the adapter rejected it); `changed` highlights a value that differs
/// from the previous stop; `pending` shows a placeholder until the
/// evaluate response lands.
Watch {
/// Index into the App's expression list — carried IN the row so the
/// remove `✕` maps to the right expression even when `debug_scroll`
/// has skipped earlier Watch rows (a render-order counter restarted
/// at the first VISIBLE row and removed the wrong one).
index: usize,
expression: String,
value: String,
available: bool,
changed: bool,
pending: bool,
},
/// The trailing "+ Add Expression" affordance row of the WATCH section.
WatchAdd,
}
/// One rendered line of the paused-state debug tree. The app builds these from
/// the session; the panel renders them and maps clicks back to them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DebugRow {
pub indent: u8,
pub kind: DebugRowKind,
}
pub struct RunDebugPanel {
pub focused: bool,
/// Active color theme; chrome colors route through `Theme::ui` so the
/// panel follows light themes. Set by the app's focus/theme sync.
pub theme: crate::theme::Theme,
/// True under the Black theme: overpaint the focused outer border with the
/// orange→green brand gradient instead of the legacy solid blue. Set by the
/// app's focus/theme sync.
pub focus_gradient: bool,
pub active_file: Option<PathBuf>,
/// Whether the active file is something croft can run or debug. When false
/// (e.g. an extensionless `.git/config`) the empty state shows a "can't run
/// this file" note instead of an actionable button. Set by the app.
pub runnable: bool,
/// Whether the button's action is debug (compiled source: build + lldb)
/// rather than run-in-terminal (scripts / executables). Drives both the
/// button verb ("Debug" vs "Run") and the click dispatch. Set by the app.
pub run_is_debug: bool,
pub last_area: Rect,
pub last_button_area: Rect,
/// When a debug session is paused, the app fills these rows (call stack +
/// variables) and sets `debug_active`; the panel then renders the tree
/// instead of the empty-state Run button.
pub debug_active: bool,
/// Remove-`✕` hit cells for Watch rows painted this frame:
/// `(y, x, nth watch row)`. Cleared at every tree render.
watch_remove_rects: Vec<(u16, u16, usize)>,
pub debug_status: String,
pub debug_rows: Vec<DebugRow>,
pub debug_scroll: usize,
/// Screen y of the first rendered debug row and how many rows were drawn,
/// recorded each frame so a click can be mapped to a row index.
pub last_debug_row_y0: u16,
pub last_debug_rows_shown: usize,
/// Tail of the debug console (program output + REPL echoes/results), set by
/// the app each refresh; rendered below the variables tree.
pub console_tail: Vec<String>,
/// Current REPL input line, rendered as a `❯` prompt at the panel bottom.
pub repl_input: String,
/// Top-left cell of the OSC-1337 icon overlay block. The post-draw
/// flush in `App` reads this to emit the rasterised debug-alt PNG
/// above the headline. `None` when the panel hasn't been laid out,
/// or when the panel is too short for the icon to fit alongside
/// the rest of the cluster.
pub last_icon_cell: Option<(u16, u16)>,
pub feedback: Option<String>,
pub feedback_is_error: bool,
/// True from when a debug session ends until the next one starts. While set
/// (and `console_tail` is non-empty) the idle panel keeps the just-ended
/// session's console output on screen instead of wiping straight back to the
/// Run button, so a fast-exiting program's output/errors stay readable.
pub session_ended: bool,
}
impl RunDebugPanel {
pub fn new() -> Self {
Self {
focused: false,
theme: crate::theme::Theme::default(),
focus_gradient: false,
active_file: None,
runnable: false,
run_is_debug: false,
last_area: Rect::default(),
last_button_area: Rect::default(),
debug_active: false,
watch_remove_rects: Vec::new(),
debug_status: String::new(),
debug_rows: Vec::new(),
debug_scroll: 0,
last_debug_row_y0: 0,
last_debug_rows_shown: 0,
console_tail: Vec::new(),
repl_input: String::new(),
last_icon_cell: None,
feedback: None,
feedback_is_error: false,
session_ended: false,
}
}
/// Map a click at screen row `y` to the index of the debug row drawn there,
/// or `None` if the click missed the rendered rows.
/// Map a click to the watch row whose remove `✕` sits at `(x, y)` —
/// returns the index into the ORDER the Watch rows were rendered in,
/// which the app maps back to its expression list.
pub fn watch_remove_at(&self, x: u16, y: u16) -> Option<usize> {
self.watch_remove_rects
.iter()
.find(|&&(ry, rx, _)| ry == y && (x == rx || x == rx + 1))
.map(|&(_, _, idx)| idx)
}
pub fn debug_row_at(&self, y: u16) -> Option<usize> {
if !self.debug_active || self.last_debug_rows_shown == 0 || y < self.last_debug_row_y0 {
return None;
}
let offset = (y - self.last_debug_row_y0) as usize;
if offset < self.last_debug_rows_shown {
Some(self.debug_scroll + offset)
} else {
None
}
}
pub fn set_active_file(&mut self, path: Option<PathBuf>) {
self.active_file = path;
}
pub fn click_button(&self, x: u16, y: u16) -> bool {
let r = self.last_button_area;
r.width > 0
&& r.height > 0
&& x >= r.x
&& x < r.x + r.width
&& y >= r.y
&& y < r.y + r.height
}
/// Render the paused-state tree (Variant A / Darcula): a status pill, the
/// call-stack + variables tree with type-coloured values, then the console
/// and a REPL bar pinned to the bottom. Records `last_debug_row_y0` /
/// `last_debug_rows_shown` for click mapping.
fn render_debug_tree(&mut self, inner: Rect, buf: &mut Buffer) {
self.watch_remove_rects.clear();
let right = inner.x + inner.width;
let mut y = inner.y;
// Status pill, e.g. " ⏸ Paused (breakpoint) ".
if !self.debug_status.is_empty() {
let pill = format!(" ⏸ {} ", self.debug_status);
let pill: String = pill.chars().take(inner.width as usize).collect();
buf.set_string(
inner.x + 1,
y,
&pill,
Style::default()
.fg(self.theme.ui(DBG_PILL_FG))
.bg(self.theme.ui(DBG_PILL_BG))
.add_modifier(Modifier::BOLD),
);
y += 2;
}
if y >= inner.y + inner.height {
return;
}
// Reserve the bottom for the console + the REPL input area. The input
// area is 2 rows: a "DEBUG CONSOLE" label and the filled input field.
// The console is sized to its actual line count (capped at 6) so the
// newest line sits directly above the label and output grows UPWARD,
// like a terminal — never floating at the top of a fixed-height box.
let bottom = inner.y + inner.height;
let input_h: u16 = 2;
let console_avail = bottom.saturating_sub(y).saturating_sub(input_h + 2);
let console_h: u16 = (self.console_tail.len() as u16).min(6).min(console_avail);
let rows_bottom = bottom.saturating_sub(input_h + console_h);
let rows_area_h = rows_bottom.saturating_sub(y) as usize;
self.last_debug_row_y0 = y;
let mut shown = 0usize;
for row in self
.debug_rows
.iter()
.skip(self.debug_scroll)
.take(rows_area_h)
{
let row_y = y + shown as u16;
match &row.kind {
DebugRowKind::Header { title } => {
// Filled bar across the full width, title in teal.
buf.set_style(
Rect {
x: inner.x,
y: row_y,
width: inner.width,
height: 1,
},
Style::default().bg(self.theme.ui(DBG_BAR_BG)),
);
let t: String = title
.chars()
.take(inner.width.saturating_sub(2) as usize)
.collect();
buf.set_string(
inner.x + 1,
row_y,
&t,
Style::default()
.fg(self.theme.ui(DBG_BAR_FG))
.bg(self.theme.ui(DBG_BAR_BG))
.add_modifier(Modifier::BOLD),
);
}
DebugRowKind::Frame {
selected,
name,
location,
..
} => {
if *selected {
buf.set_style(
Rect {
x: inner.x,
y: row_y,
width: inner.width,
height: 1,
},
Style::default().bg(self.theme.ui(DBG_FRAME_BG)),
);
buf.set_string(
inner.x,
row_y,
"▌",
Style::default()
.fg(self.theme.ui(DBG_ACCENT))
.bg(self.theme.ui(DBG_FRAME_BG)),
);
}
let bg = if *selected {
Some(self.theme.ui(DBG_FRAME_BG))
} else {
None
};
let mut x = inner.x + 2;
let name_style = if *selected {
Style::default()
.fg(self.theme.ui(DBG_FRAME_SEL_FG))
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(self.theme.ui(DBG_NAME))
};
x = put(buf, x, row_y, right, name, with_bg(name_style, bg));
x = put(buf, x, row_y, right, " ", with_bg(Style::default(), bg));
put(
buf,
x,
row_y,
right,
location,
with_bg(Style::default().fg(self.theme.ui(DBG_LOC)), bg),
);
}
DebugRowKind::Scope { name } => {
put(
buf,
inner.x + 1,
row_y,
right,
name,
Style::default()
.fg(self.theme.ui(DBG_SCOPE))
.add_modifier(Modifier::BOLD),
);
}
DebugRowKind::Variable {
expandable,
expanded,
name,
value,
type_name,
..
} => {
let indent = (row.indent as u16) * 2;
let mut x = inner.x + indent;
let chev = if *expandable {
if *expanded { "▾ " } else { "▸ " }
} else {
" "
};
x = put(
buf,
x,
row_y,
right,
chev,
Style::default().fg(self.theme.ui(DBG_CHEV)),
);
x = put(
buf,
x,
row_y,
right,
name,
Style::default().fg(self.theme.ui(DBG_NAME)),
);
x = put(
buf,
x,
row_y,
right,
" = ",
Style::default().fg(self.theme.ui(DBG_EQ)),
);
x = put(
buf,
x,
row_y,
right,
value,
Style::default().fg(value_color(type_name, self.theme)),
);
if !type_name.is_empty() {
put(
buf,
x,
row_y,
right,
&format!(" : {type_name}"),
Style::default()
.fg(self.theme.ui(DBG_TYPE))
.add_modifier(Modifier::ITALIC),
);
}
}
DebugRowKind::Watch {
index,
expression,
value,
available,
changed,
pending,
} => {
let indent = (row.indent as u16) * 2;
let mut x = inner.x + indent;
// Text clips three cells early: the remove `\u{2715}` owns
// the right edge, and a long value ran under it.
let right = right.saturating_sub(3);
x = put(
buf,
x,
row_y,
right,
expression,
Style::default().fg(self.theme.ui(DBG_NAME)),
);
x = put(
buf,
x,
row_y,
right,
" = ",
Style::default().fg(self.theme.ui(DBG_EQ)),
);
let (text, style) = if *pending {
("\u{2026}", Style::default().fg(self.theme.ui(DBG_TYPE)))
} else if !*available {
(
"<not available>",
Style::default()
.fg(self.theme.ui(DBG_TYPE))
.add_modifier(Modifier::ITALIC),
)
} else if *changed {
(
value.as_str(),
Style::default()
.fg(self.theme.ui(Color::Rgb(0xb6, 0xee, 0xc4)))
.add_modifier(Modifier::BOLD),
)
} else {
(
value.as_str(),
Style::default().fg(value_color("", self.theme)),
)
};
put(buf, x, row_y, right, text, style);
// Per-row remove ✕ pinned at the right edge, recorded for
// the click map (cleared at render start — hit rects
// always describe the painted frame).
let right = right + 3;
if right > inner.x + 2 {
let rx = right - 2;
put(
buf,
rx,
row_y,
right,
"\u{2715}",
Style::default().fg(self.theme.ui(DBG_TYPE)),
);
self.watch_remove_rects.push((row_y, rx, *index));
}
}
DebugRowKind::WatchAdd => {
let indent = (row.indent as u16) * 2;
put(
buf,
inner.x + indent,
row_y,
right,
"+ Add Expression",
Style::default()
.fg(self.theme.ui(DBG_TYPE))
.add_modifier(Modifier::ITALIC),
);
}
}
shown += 1;
}
self.last_debug_rows_shown = shown;
// Bottom-up: the input field is the last row, the "DEBUG CONSOLE" label
// the row above it, and any console output sits above that.
let field_y = bottom - 1;
let label_y = bottom - 2;
// Console: program output + REPL echoes, above the label.
if console_h > 0 {
let console_y0 = label_y.saturating_sub(console_h);
for (i, line) in self
.console_tail
.iter()
.rev()
.take(console_h as usize)
.rev()
.enumerate()
{
let style = if line.starts_with('❯') {
Style::default().fg(self.theme.ui(DBG_ACCENT))
} else {
Style::default().fg(self.theme.ui(DBG_LOC))
};
put(buf, inner.x + 1, console_y0 + i as u16, right, line, style);
}
}
// "DEBUG CONSOLE" label above the field.
put(
buf,
inner.x + 1,
label_y,
right,
"DEBUG CONSOLE",
Style::default()
.fg(self.theme.ui(DBG_LOC))
.add_modifier(Modifier::BOLD),
);
// The input field: a subtle full-width fill makes it read as a text box.
// A teal caret, then the input (or a dim-italic placeholder when empty),
// with a teal block cursor marking the insertion point.
buf.set_style(
Rect {
x: inner.x,
y: field_y,
width: inner.width,
height: 1,
},
Style::default().bg(self.theme.ui(DBG_FIELD_BG)),
);
let bg = Some(self.theme.ui(DBG_FIELD_BG));
let caret_end = put(
buf,
inner.x + 1,
field_y,
right,
"❯ ",
with_bg(
Style::default()
.fg(self.theme.ui(DBG_ACCENT))
.add_modifier(Modifier::BOLD),
bg,
),
);
if self.repl_input.is_empty() {
// Block cursor at the insertion point, then the ghost placeholder.
let after_cursor = put(
buf,
caret_end,
field_y,
right,
" ",
Style::default().bg(self.theme.ui(DBG_ACCENT)),
);
put(
buf,
after_cursor,
field_y,
right,
"Evaluate expression",
with_bg(
Style::default()
.fg(self.theme.ui(DBG_PLACEHOLDER))
.add_modifier(Modifier::ITALIC),
bg,
),
);
} else {
let input_end = put(
buf,
caret_end,
field_y,
right,
&self.repl_input,
with_bg(Style::default().fg(self.theme.ui(DBG_NAME)), bg),
);
put(
buf,
input_end,
field_y,
right,
" ",
Style::default().bg(self.theme.ui(DBG_ACCENT)),
);
}
}
pub fn button_label(&self) -> String {
match self.active_file.as_ref().and_then(|p| p.file_name()) {
Some(name) => {
// Compiled source builds + launches the debugger; scripts and
// executables run in a terminal.
let verb = if self.run_is_debug { "Debug" } else { "Run" };
format!("{verb} {}", name.to_string_lossy())
}
None => String::from("Run and Debug"),
}
}
}
impl Default for RunDebugPanel {
fn default() -> Self {
Self::new()
}
}
impl Widget for &mut RunDebugPanel {
fn render(self, area: Rect, buf: &mut Buffer) {
let border_style = if self.focused {
Style::default().fg(Color::Rgb(
FOCUS_BORDER_RGB.0,
FOCUS_BORDER_RGB.1,
FOCUS_BORDER_RGB.2,
))
} else {
Style::default().fg(Color::DarkGray)
};
let block = Block::default()
.borders(Borders::ALL)
.border_style(border_style);
let inner = block.inner(area);
block.render(area, buf);
// Black theme: replace the solid focus border with the brand gradient.
// This panel has no border title, so nothing needs re-stamping.
if self.focused && self.focus_gradient {
crate::gradient::paint_gradient_box(buf, area);
}
self.last_area = area;
self.last_button_area = Rect::default();
self.last_icon_cell = None;
self.last_debug_rows_shown = 0;
if inner.height == 0 || inner.width == 0 {
return;
}
// Paused at a breakpoint: render the call-stack + variables tree instead
// of the empty-state Run button.
if self.debug_active {
self.render_debug_tree(inner, buf);
return;
}
// Vertical cluster, top-to-bottom: icon, gap, headline, gap, body, gap, button.
// Centre the cluster vertically so the panel feels balanced like the mockup.
const GAP_AFTER_ICON: u16 = 2;
const TITLE_H: u16 = 1;
const GAP_AFTER_TITLE: u16 = 1;
const BODY_MAX_H: u16 = 3;
const GAP_AFTER_BODY: u16 = 2;
const BUTTON_H: u16 = 3;
let icon_w = RUN_DEBUG_ICON_CELLS_W.min(inner.width);
let icon_h_full = RUN_DEBUG_ICON_CELLS_H;
let cluster_full = icon_h_full
+ GAP_AFTER_ICON
+ TITLE_H
+ GAP_AFTER_TITLE
+ BODY_MAX_H
+ GAP_AFTER_BODY
+ BUTTON_H;
// If the panel is too short for the full cluster, drop the icon
// first (it's decorative) before sacrificing the body or button.
let (icon_h, gap_after_icon, cluster) = if inner.height >= cluster_full {
(icon_h_full, GAP_AFTER_ICON, cluster_full)
} else {
(
0,
0,
TITLE_H + GAP_AFTER_TITLE + BODY_MAX_H + GAP_AFTER_BODY + BUTTON_H,
)
};
// After a session ends with output, anchor the cluster to the top so the
// retained console (rendered below the button) gets the lower region;
// otherwise centre it like the resting state.
let show_ended_console = self.session_ended && !self.console_tail.is_empty();
let top_pad = if show_ended_console {
0
} else if inner.height > cluster {
(inner.height - cluster) / 2
} else {
0
};
let mut y = inner.y + top_pad;
if icon_h > 0 && y + icon_h <= inner.y + inner.height {
let icon_x = inner.x + (inner.width.saturating_sub(icon_w)) / 2;
self.last_icon_cell = Some((icon_x, y));
// Glyph fallback: paint the cod-debug-alt codicon centred
// inside the reserved icon block so terminals without
// OSC-1337 image support still see a recognisable shape.
// On iTerm2 the post-draw `flush_run_debug_icon_overlay`
// overwrites these cells with the proper rasterised PNG.
let glyph_style = Style::default()
.fg(Color::Rgb(BODY_FG_RGB.0, BODY_FG_RGB.1, BODY_FG_RGB.2))
.add_modifier(Modifier::BOLD);
let glyph_x = icon_x + icon_w / 2;
let glyph_y = y + icon_h / 2;
buf.set_span(
glyph_x,
glyph_y,
&Span::styled(crate::icons::ACTIVITY_RUN_DEBUG.to_string(), glyph_style),
1,
);
y += icon_h + gap_after_icon;
}
if y + TITLE_H > inner.y + inner.height {
return;
}
let title = Paragraph::new(Line::from("RUN AND DEBUG"))
.alignment(Alignment::Center)
.style(
Style::default()
.fg(Color::Rgb(TITLE_FG_RGB.0, TITLE_FG_RGB.1, TITLE_FG_RGB.2))
.add_modifier(Modifier::BOLD),
);
title.render(
Rect {
x: inner.x,
y,
width: inner.width,
height: TITLE_H,
},
buf,
);
y += TITLE_H + GAP_AFTER_TITLE;
if y >= inner.y + inner.height {
return;
}
let body_text = match self.active_file.as_ref() {
Some(_) if self.runnable => {
"Press the button below to run the active file in a new terminal."
}
Some(_) => {
"This file can't be run or debugged. Open a Python, Rust, C/C++, or script file."
}
None => "Open a file that can be run or debugged, then press the button below.",
};
let body_h = BODY_MAX_H.min(inner.y + inner.height - y);
let body_x_pad = (inner.width / 12).clamp(1, 4);
let body_area = Rect {
x: inner.x + body_x_pad,
y,
width: inner.width.saturating_sub(body_x_pad * 2).max(1),
height: body_h,
};
let body = Paragraph::new(body_text)
.style(Style::default().fg(Color::Rgb(BODY_FG_RGB.0, BODY_FG_RGB.1, BODY_FG_RGB.2)))
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
body.render(body_area, buf);
y += BODY_MAX_H + GAP_AFTER_BODY;
// A file is open but croft can't run/debug it: show only the note, no
// actionable button. (`last_button_area` stays default, so a click in
// the panel does nothing.) The no-file case still shows the generic
// "Run and Debug" button.
if self.active_file.is_some() && !self.runnable {
return;
}
if y + BUTTON_H > inner.y + inner.height {
return;
}
let label = self.button_label();
let label_chars = label.chars().count() as u16;
// Pin the button to the panel's interior so it can never be wider
// than the available space; without this clamp `inner.width -
// button_w` underflowed at narrow sidebar widths and ratatui
// panicked indexing the buffer at a wrapped-u16 column.
let button_w = (label_chars + 8).min(inner.width.saturating_sub(2));
if button_w < 4 {
return;
}
let button_x = inner.x + inner.width.saturating_sub(button_w) / 2;
let button_area = Rect {
x: button_x,
y,
width: button_w,
height: BUTTON_H,
};
self.last_button_area = button_area;
// Black theme: brand-teal button fill; Croft Dark keeps VS Code blue.
let button_bg = if self.focus_gradient {
crate::gradient::rgb_color(crate::gradient::PRIMARY_BTN_BG)
} else {
Color::Rgb(BUTTON_BG_RGB.0, BUTTON_BG_RGB.1, BUTTON_BG_RGB.2)
};
crate::widgets::source_control::render_rounded_button(
buf,
button_area,
label.as_str(),
button_bg,
Color::Rgb(BUTTON_FG_RGB.0, BUTTON_FG_RGB.1, BUTTON_FG_RGB.2),
);
let mut next_y = button_area.y + button_area.height + 1;
if let Some(msg) = self.feedback.as_ref()
&& next_y < inner.y + inner.height
{
let style = if self.feedback_is_error {
Style::default().fg(self.theme.ui(Color::Rgb(0xe7, 0x70, 0x70)))
} else {
Style::default().fg(self.theme.ui(Color::Rgb(0xa3, 0xbe, 0x8c)))
};
buf.set_string(inner.x + 1, next_y, msg.as_str(), style);
next_y = next_y.saturating_add(1);
}
// Retained console from the just-ended session: program output / errors
// stay readable after the run instead of being wiped to the Run button.
if show_ended_console && next_y + 1 < inner.y + inner.height {
next_y = next_y.saturating_add(1);
buf.set_string(
inner.x + 1,
next_y,
"DEBUG CONSOLE",
Style::default()
.fg(Color::Rgb(TITLE_FG_RGB.0, TITLE_FG_RGB.1, TITLE_FG_RGB.2))
.add_modifier(Modifier::BOLD),
);
next_y = next_y.saturating_add(1);
let bottom = inner.y + inner.height;
let avail = bottom.saturating_sub(next_y) as usize;
let start = self.console_tail.len().saturating_sub(avail);
let max_w = inner.width.saturating_sub(2) as usize;
let line_style = Style::default().fg(self.theme.ui(Color::Rgb(0x9d, 0xa5, 0xb4)));
for line in &self.console_tail[start..] {
if next_y >= bottom {
break;
}
let shown: String = line.chars().take(max_w).collect();
buf.set_string(inner.x + 1, next_y, &shown, line_style);
next_y = next_y.saturating_add(1);
}
}
let _ = next_y;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paused_state_renders_call_stack_and_variables_and_maps_clicks() {
let mut panel = RunDebugPanel::new();
panel.debug_active = true;
panel.debug_status = String::from("Paused (breakpoint)");
panel.debug_rows = vec![
DebugRow {
indent: 0,
kind: DebugRowKind::Header {
title: "CALL STACK".into(),
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::Frame {
id: 1000,
selected: true,
name: "isValid".into(),
location: "app.py:12".into(),
},
},
DebugRow {
indent: 0,
kind: DebugRowKind::Header {
title: "VARIABLES".into(),
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::Scope {
name: "Locals".into(),
},
},
DebugRow {
indent: 2,
kind: DebugRowKind::Variable {
reference: 0,
expandable: false,
expanded: false,
name: "x".into(),
value: "1".into(),
type_name: "int".into(),
},
},
DebugRow {
indent: 2,
kind: DebugRowKind::Variable {
reference: 9,
expandable: true,
expanded: false,
name: "obj".into(),
value: "<Foo>".into(),
type_name: "Foo".into(),
},
},
];
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let dump = buffer_to_string(&buf);
assert!(dump.contains("CALL STACK"), "call stack header:\n{dump}");
assert!(dump.contains("VARIABLES"), "variables header:\n{dump}");
assert!(dump.contains("isValid"), "frame name:\n{dump}");
assert!(dump.contains("app.py:12"), "frame location:\n{dump}");
assert!(dump.contains("x = 1"), "variable row:\n{dump}");
assert!(
dump.contains("▸ obj = <Foo>"),
"expandable chevron:\n{dump}"
);
// The empty-state Run button must NOT be laid out while debugging.
assert_eq!(panel.last_button_area, Rect::default());
// Click mapping: first row sits at last_debug_row_y0.
let y0 = panel.last_debug_row_y0;
assert_eq!(panel.debug_row_at(y0), Some(0));
assert_eq!(panel.debug_row_at(y0 + 1), Some(1));
assert_eq!(panel.debug_row_at(y0.saturating_sub(1)), None);
}
#[test]
fn paused_state_renders_console_tail_and_repl_prompt() {
let mut panel = RunDebugPanel::new();
panel.debug_active = true;
panel.debug_status = String::from("Paused (breakpoint)");
panel.debug_rows = vec![DebugRow {
indent: 0,
kind: DebugRowKind::Header {
title: "CALL STACK".into(),
},
}];
panel.console_tail = vec!["hello from program".into(), "42".into()];
panel.repl_input = String::from("x + 1");
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let dump = buffer_to_string(&buf);
assert!(
dump.contains("hello from program"),
"console output:\n{dump}"
);
assert!(dump.contains("❯ x + 1"), "repl prompt with input:\n{dump}");
assert!(
dump.contains("DEBUG CONSOLE"),
"input field must carry a label:\n{dump}"
);
// Bottom-anchored: the newest console line ("42") must sit on the row
// directly above the DEBUG CONSOLE label, with no gap floating it up.
let row_text = |y: u16| -> String {
(0..area.width)
.map(|x| buf[(x, y)].symbol())
.collect::<String>()
};
let label_y = (0..area.height)
.find(|&y| row_text(y).contains("DEBUG CONSOLE"))
.expect("label present");
assert!(
row_text(label_y - 1).contains("42"),
"newest console line must hug the label (row above it):\nlabel-1: {:?}",
row_text(label_y - 1)
);
}
#[test]
fn empty_repl_shows_a_ghost_placeholder() {
let mut panel = RunDebugPanel::new();
panel.debug_active = true;
panel.debug_status = String::from("Paused");
panel.debug_rows = vec![DebugRow {
indent: 0,
kind: DebugRowKind::Header {
title: "VARIABLES".into(),
},
}];
// repl_input left empty
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let dump = buffer_to_string(&buf);
assert!(
dump.contains("Evaluate expression"),
"empty REPL must show a placeholder hint:\n{dump}"
);
}
#[test]
fn button_label_says_run_and_debug_when_no_file_is_active() {
let panel = RunDebugPanel::new();
assert_eq!(panel.button_label(), "Run and Debug");
}
#[test]
fn button_label_includes_filename_when_a_file_is_active() {
let mut panel = RunDebugPanel::new();
panel.set_active_file(Some(PathBuf::from("/work/script.py")));
assert_eq!(panel.button_label(), "Run script.py");
}
#[test]
fn button_label_says_debug_for_compiled_source() {
let mut panel = RunDebugPanel::new();
panel.set_active_file(Some(PathBuf::from("/work/main.rs")));
panel.run_is_debug = true;
assert_eq!(panel.button_label(), "Debug main.rs");
}
#[test]
fn click_button_is_inside_recorded_button_area() {
let mut panel = RunDebugPanel::new();
panel.last_button_area = Rect {
x: 10,
y: 5,
width: 12,
height: 3,
};
assert!(panel.click_button(10, 5));
assert!(panel.click_button(21, 7));
assert!(!panel.click_button(22, 5));
assert!(!panel.click_button(15, 8));
}
#[test]
fn rendering_lays_out_button_area_inside_panel() {
let mut panel = RunDebugPanel::new();
panel.set_active_file(Some(PathBuf::from("/work/run_me.rs")));
panel.runnable = true;
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let b = panel.last_button_area;
assert!(b.width > 0 && b.height > 0, "button area must be laid out");
assert!(b.x >= area.x && b.x + b.width <= area.x + area.width);
assert!(b.y >= area.y && b.y < area.y + area.height);
}
#[test]
fn non_runnable_file_shows_a_note_and_no_button() {
// An open file croft can't run/debug (e.g. .git/config) must not offer a
// Run button that would just fail.
let mut panel = RunDebugPanel::new();
panel.set_active_file(Some(PathBuf::from("/repo/.git/config")));
panel.runnable = false;
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
assert_eq!(
panel.last_button_area,
Rect::default(),
"no Run button for a non-runnable file"
);
let mut dump = String::new();
for y in 0..area.height {
for x in 0..area.width {
dump.push_str(buf[(x, y)].symbol());
}
}
// The note wraps across rows in the narrow panel, so assert an
// intra-line fragment rather than the whole phrase.
assert!(
dump.contains("can't be run") && dump.contains("debugged"),
"must explain why there's no button: {dump:?}"
);
}
fn buffer_to_string(buf: &Buffer) -> String {
let mut out = String::new();
for y in 0..buf.area.height {
for x in 0..buf.area.width {
out.push_str(buf[(buf.area.x + x, buf.area.y + y)].symbol());
}
out.push('\n');
}
out
}
#[test]
fn empty_state_renders_centred_title_and_description_and_chunky_button() {
let mut panel = RunDebugPanel::new();
let area = Rect {
x: 0,
y: 0,
width: 36,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let dump = buffer_to_string(&buf);
assert!(
dump.contains("RUN AND DEBUG"),
"headline must be present: \n{dump}"
);
assert!(
dump.contains("Open a file"),
"body copy must be present: \n{dump}"
);
assert!(
!dump.lines().next().unwrap().contains("RUN AND DEBUG"),
"title bar must NOT sit on the top border row (mockup has no title bar): \n{dump}"
);
let button = panel.last_button_area;
assert!(
button.height >= 3,
"button must be at least 3 rows tall to feel chunky like the mockup; got height={}",
button.height
);
// The OSC-1337 icon block must be reserved ABOVE the button.
let icon = panel.last_icon_cell.expect("icon block must be reserved");
assert!(
button.y > icon.1,
"button must sit below the icon (icon top y={}, button.y={})",
icon.1,
button.y
);
}
#[test]
fn icon_block_reserves_an_osc_1337_overlay_cell_with_a_glyph_fallback() {
// The headline icon is a 6x3-cell OSC-1337 image overlay (the
// codicon `debug-alt` PNG, same one the activity-bar slot uses),
// emitted post-frame by App::flush_run_debug_icon_overlay. The
// widget's job is to (1) reserve the icon block via
// last_icon_cell and (2) paint the codicon glyph as a text
// fallback for terminals without OSC-1337 support; iTerm2
// overwrites those cells with the rasterised image.
let mut panel = RunDebugPanel::new();
let area = Rect {
x: 0,
y: 0,
width: 36,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let (ix, iy) = panel
.last_icon_cell
.expect("icon overlay cell must be reserved on a tall enough panel");
let icon_w = RUN_DEBUG_ICON_CELLS_W;
let icon_h = RUN_DEBUG_ICON_CELLS_H;
let glyph_x = ix + icon_w / 2;
let glyph_y = iy + icon_h / 2;
let cell = buf[(glyph_x, glyph_y)].symbol().to_string();
assert_eq!(
cell,
crate::icons::ACTIVITY_RUN_DEBUG.to_string(),
"glyph fallback must be the cod-debug-alt codicon at the centre of the icon block"
);
}
#[test]
fn run_and_debug_button_uses_rounded_border_corners() {
let mut panel = RunDebugPanel::new();
let area = Rect {
x: 0,
y: 0,
width: 36,
height: 24,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let b = panel.last_button_area;
assert!(b.height >= 3 && b.width >= 4, "button must be laid out");
let tl = buf[(b.x, b.y)].symbol().to_string();
let tr = buf[(b.x + b.width - 1, b.y)].symbol().to_string();
let bl = buf[(b.x, b.y + b.height - 1)].symbol().to_string();
let br = buf[(b.x + b.width - 1, b.y + b.height - 1)]
.symbol()
.to_string();
assert_eq!(
tl, "╭",
"top-left button corner must be rounded; got {tl:?}"
);
assert_eq!(
tr, "╮",
"top-right button corner must be rounded; got {tr:?}"
);
assert_eq!(
bl, "╰",
"bottom-left button corner must be rounded; got {bl:?}"
);
assert_eq!(
br, "╯",
"bottom-right button corner must be rounded; got {br:?}"
);
}
#[test]
fn rendering_at_narrow_widths_does_not_panic() {
// Repro for the user-reported crash: dragging the sidebar
// splitter all the way to the left squeezed Run-and-Debug to a
// few cells wide and ratatui panicked with `index outside of
// buffer`. The cause was `inner.width - button_w` wrapping to a
// huge u16 when the "Run and Debug" label (13 chars) was wider
// than `inner.width`. Render the panel at every width from 0 up
// to a reasonable maximum and assert nothing crashes.
for width in 0u16..40 {
for height in 0u16..30 {
let mut panel = RunDebugPanel::new();
panel.set_active_file(Some(PathBuf::from("/work/run_me.rs")));
let area = Rect {
x: 0,
y: 0,
width,
height,
};
if area.width == 0 || area.height == 0 {
continue;
}
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
}
}
}
#[test]
fn small_panel_skips_icon_block_to_keep_button_visible() {
let mut panel = RunDebugPanel::new();
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 12,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
assert!(
panel.last_button_area.height >= 3,
"button must remain laid out when the panel collapses the icon block"
);
}
#[test]
fn watch_section_renders_states_and_maps_remove_clicks() {
let mut panel = RunDebugPanel::new();
panel.debug_active = true;
panel.debug_status = String::from("Paused (breakpoint)");
panel.debug_rows = vec![
DebugRow {
indent: 0,
kind: DebugRowKind::Header {
title: "WATCH".into(),
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::Watch {
index: 0,
expression: "t".into(),
value: "9.99".into(),
available: true,
changed: true,
pending: false,
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::Watch {
index: 1,
expression: "self.missing".into(),
value: "NameError".into(),
available: false,
changed: false,
pending: false,
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::Watch {
index: 2,
expression: "len(cart)".into(),
value: String::new(),
available: true,
changed: false,
pending: true,
},
},
DebugRow {
indent: 1,
kind: DebugRowKind::WatchAdd,
},
];
let area = Rect {
x: 0,
y: 0,
width: 44,
height: 20,
};
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let dump = buffer_to_string(&buf);
assert!(dump.contains("WATCH"), "watch header:\n{dump}");
assert!(dump.contains("t = 9.99"), "watched value:\n{dump}");
assert!(
dump.contains("self.missing = <not available>"),
"a rejected expression must degrade, not show the adapter error:\n{dump}"
);
assert!(
!dump.contains("NameError"),
"the raw adapter error must not leak into the row:\n{dump}"
);
assert!(dump.contains("+ Add Expression"), "add affordance:\n{dump}");
// Every watch row recorded a remove ✕ at the right edge, mapped by
// render order.
assert_eq!(panel.watch_remove_rects.len(), 3);
let (y, x, idx) = panel.watch_remove_rects[1];
assert_eq!(idx, 1, "the second watch row maps to index 1");
assert_eq!(panel.watch_remove_at(x, y), Some(1));
assert_eq!(panel.watch_remove_at(x, y + 5), None);
// Scrolled: the header and the first watch row are skipped. The
// first VISIBLE watch row must still map to expression index 1 —
// a render-order counter restarted at 0 here and the ✕ removed
// the wrong expression (#113 review).
panel.debug_scroll = 2;
let mut buf = Buffer::empty(area);
Widget::render(&mut panel, area, &mut buf);
let (y, x, idx) = panel.watch_remove_rects[0];
assert_eq!(
idx, 1,
"after scrolling past the first watch row, the first visible one keeps its true index"
);
assert_eq!(panel.watch_remove_at(x, y), Some(1));
}
}