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
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
//! Stream handler trait and implementations for processing Claude stream events.
//!
//! The `StreamHandler` trait abstracts over how stream events are displayed,
//! allowing for different output strategies (console, quiet, TUI, etc.).
use ansi_to_tui::IntoText;
use crossterm::{
QueueableCommand,
style::{self, Color},
};
use ratatui::{
style::{Color as RatatuiColor, Style},
text::{Line, Span},
};
use std::{
borrow::Cow,
io::{self, Write},
sync::{Arc, Mutex},
};
use termimad::MadSkin;
/// Detects if text contains ANSI escape sequences.
///
/// Checks for the common ANSI escape sequence prefix `\x1b[` (ESC + `[`)
/// which is used for colors, formatting, and cursor control.
#[inline]
pub(crate) fn contains_ansi(text: &str) -> bool {
text.contains("\x1b[")
}
/// Normalizes terminal control characters that commonly break ratatui rendering.
///
/// In particular:
/// - `\r` (carriage return) is used by many CLIs (git, cargo, etc.) to render
/// progress updates on a single line. When embedded in ratatui content it can
/// move the cursor and corrupt layout.
/// - Some other C0 controls (bell, backspace, vertical tab, form feed) can also
/// cause display corruption or odd glyphs.
///
/// We keep `\n` and `\t` intact.
fn sanitize_tui_block_text(text: &str) -> Cow<'_, str> {
let has_cr = text.contains('\r');
let has_other_ctrl = text
.chars()
.any(|c| matches!(c, '\u{0007}' | '\u{0008}' | '\u{000b}' | '\u{000c}'));
if !has_cr && !has_other_ctrl {
return Cow::Borrowed(text);
}
let mut s = if has_cr {
// Normalize CRLF and bare CR to LF.
text.replace("\r\n", "\n").replace('\r', "\n")
} else {
text.to_string()
};
if has_other_ctrl {
s.retain(|c| !matches!(c, '\u{0007}' | '\u{0008}' | '\u{000b}' | '\u{000c}'));
}
Cow::Owned(s)
}
/// Sanitizes text that must stay on a *single* TUI line (tool summaries, errors).
/// Removes embedded newlines and carriage returns entirely.
fn sanitize_tui_inline_text(text: &str) -> String {
let mut s = text.replace("\r\n", " ").replace(['\r', '\n'], " ");
// Drop other control characters that can corrupt the terminal.
s.retain(|c| !matches!(c, '\u{0007}' | '\u{0008}' | '\u{000b}' | '\u{000c}'));
s
}
/// Session completion result data.
#[derive(Debug, Clone)]
pub struct SessionResult {
pub duration_ms: u64,
pub total_cost_usd: f64,
pub num_turns: u32,
pub is_error: bool,
}
/// Renders streaming output with colors and markdown.
pub struct PrettyStreamHandler {
stdout: io::Stdout,
verbose: bool,
/// Buffer for accumulating text before markdown rendering
text_buffer: String,
/// Skin for markdown rendering
skin: MadSkin,
}
impl PrettyStreamHandler {
/// Creates a new pretty handler.
pub fn new(verbose: bool) -> Self {
Self {
stdout: io::stdout(),
verbose,
text_buffer: String::new(),
skin: MadSkin::default(),
}
}
/// Flush buffered text as rendered markdown.
fn flush_text_buffer(&mut self) {
if self.text_buffer.is_empty() {
return;
}
// Render markdown to string, then write
let rendered = self.skin.term_text(&self.text_buffer);
let _ = self.stdout.write(rendered.to_string().as_bytes());
let _ = self.stdout.flush();
self.text_buffer.clear();
}
}
impl StreamHandler for PrettyStreamHandler {
fn on_text(&mut self, text: &str) {
// Buffer text for markdown rendering
// Text is flushed when: tool calls arrive, on_complete is called, or on_error is called
// This works well for StreamJson backends (Claude) which have natural flush points
// Text format backends should use ConsoleStreamHandler for immediate output
self.text_buffer.push_str(text);
}
fn on_tool_result(&mut self, _id: &str, output: &str) {
if self.verbose {
let _ = self
.stdout
.queue(style::SetForegroundColor(Color::DarkGrey));
let _ = self
.stdout
.write(format!(" \u{2713} {}\n", truncate(output, 200)).as_bytes());
let _ = self.stdout.queue(style::ResetColor);
let _ = self.stdout.flush();
}
}
fn on_error(&mut self, error: &str) {
let _ = self.stdout.queue(style::SetForegroundColor(Color::Red));
let _ = self
.stdout
.write(format!("\n\u{2717} Error: {}\n", error).as_bytes());
let _ = self.stdout.queue(style::ResetColor);
let _ = self.stdout.flush();
}
fn on_complete(&mut self, result: &SessionResult) {
// Flush any remaining buffered text
self.flush_text_buffer();
let _ = self.stdout.write(b"\n");
let color = if result.is_error {
Color::Red
} else {
Color::Green
};
let _ = self.stdout.queue(style::SetForegroundColor(color));
let _ = self.stdout.write(
format!(
"Duration: {}ms | Est. cost: ${:.4} | Turns: {}\n",
result.duration_ms, result.total_cost_usd, result.num_turns
)
.as_bytes(),
);
let _ = self.stdout.queue(style::ResetColor);
let _ = self.stdout.flush();
}
fn on_tool_call(&mut self, name: &str, _id: &str, input: &serde_json::Value) {
// Flush any buffered text before showing tool call
self.flush_text_buffer();
// ⚙️ [ToolName]
let _ = self.stdout.queue(style::SetForegroundColor(Color::Blue));
let _ = self.stdout.write(format!("\u{2699} [{}]", name).as_bytes());
if let Some(summary) = format_tool_summary(name, input) {
let _ = self
.stdout
.queue(style::SetForegroundColor(Color::DarkGrey));
let _ = self.stdout.write(format!(" {}\n", summary).as_bytes());
} else {
let _ = self.stdout.write(b"\n");
}
let _ = self.stdout.queue(style::ResetColor);
let _ = self.stdout.flush();
}
}
/// Handler for streaming output events from Claude.
///
/// Implementors receive events as Claude processes and can format/display
/// them in various ways (console output, TUI updates, logging, etc.).
pub trait StreamHandler: Send {
/// Called when Claude emits text.
fn on_text(&mut self, text: &str);
/// Called when Claude invokes a tool.
///
/// # Arguments
/// * `name` - Tool name (e.g., "Read", "Bash", "Grep")
/// * `id` - Unique tool invocation ID
/// * `input` - Tool input parameters as JSON (file paths, commands, patterns, etc.)
fn on_tool_call(&mut self, name: &str, id: &str, input: &serde_json::Value);
/// Called when a tool returns results.
fn on_tool_result(&mut self, id: &str, output: &str);
/// Called when an error occurs.
fn on_error(&mut self, error: &str);
/// Called when session completes (verbose only).
fn on_complete(&mut self, result: &SessionResult);
}
/// Writes streaming output to stdout/stderr.
///
/// In normal mode, displays assistant text and tool invocations.
/// In verbose mode, also displays tool results and session summary.
pub struct ConsoleStreamHandler {
verbose: bool,
stdout: io::Stdout,
stderr: io::Stderr,
/// Tracks whether last output ended with a newline
last_was_newline: bool,
}
impl ConsoleStreamHandler {
/// Creates a new console handler.
///
/// # Arguments
/// * `verbose` - If true, shows tool results and session summary.
pub fn new(verbose: bool) -> Self {
Self {
verbose,
stdout: io::stdout(),
stderr: io::stderr(),
last_was_newline: true, // Start true so first output doesn't get extra newline
}
}
/// Ensures output starts on a new line if the previous output didn't end with one.
fn ensure_newline(&mut self) {
if !self.last_was_newline {
let _ = writeln!(self.stdout);
self.last_was_newline = true;
}
}
}
impl StreamHandler for ConsoleStreamHandler {
fn on_text(&mut self, text: &str) {
let _ = write!(self.stdout, "{}", text);
self.last_was_newline = text.ends_with('\n');
}
fn on_tool_call(&mut self, name: &str, _id: &str, input: &serde_json::Value) {
self.ensure_newline();
match format_tool_summary(name, input) {
Some(summary) => {
let _ = writeln!(self.stdout, "[Tool] {}: {}", name, summary);
}
None => {
let _ = writeln!(self.stdout, "[Tool] {}", name);
}
}
// writeln always ends with newline
self.last_was_newline = true;
}
fn on_tool_result(&mut self, _id: &str, output: &str) {
if self.verbose {
let _ = writeln!(self.stdout, "[Result] {}", truncate(output, 200));
}
}
fn on_error(&mut self, error: &str) {
// Write to both stdout (inline) and stderr (for separation)
let _ = writeln!(self.stdout, "[Error] {}", error);
let _ = writeln!(self.stderr, "[Error] {}", error);
}
fn on_complete(&mut self, result: &SessionResult) {
if self.verbose {
let _ = writeln!(
self.stdout,
"\n--- Session Complete ---\nDuration: {}ms | Est. cost: ${:.4} | Turns: {}",
result.duration_ms, result.total_cost_usd, result.num_turns
);
}
}
}
/// Suppresses all streaming output (for CI/silent mode).
pub struct QuietStreamHandler;
impl StreamHandler for QuietStreamHandler {
fn on_text(&mut self, _: &str) {}
fn on_tool_call(&mut self, _: &str, _: &str, _: &serde_json::Value) {}
fn on_tool_result(&mut self, _: &str, _: &str) {}
fn on_error(&mut self, _: &str) {}
fn on_complete(&mut self, _: &SessionResult) {}
}
/// Converts text to styled ratatui Lines, handling both ANSI and markdown.
///
/// When text contains ANSI escape sequences (e.g., from CLI tools like Kiro),
/// uses `ansi_to_tui` to preserve colors and formatting. Otherwise, uses
/// `termimad` to parse markdown (matching non-TUI mode behavior), then
/// converts the ANSI output via `ansi_to_tui`.
///
/// Using `termimad` ensures parity between TUI and non-TUI modes, as both
/// use the same markdown processing engine with the same line-breaking rules.
fn text_to_lines(text: &str) -> Vec<Line<'static>> {
if text.is_empty() {
return Vec::new();
}
// Ratatui content must not contain control characters like carriage returns.
// See sanitize_tui_block_text() for rationale.
let text = sanitize_tui_block_text(text);
let text = text.as_ref();
if text.is_empty() {
return Vec::new();
}
// Convert text to ANSI-styled string
// - If already contains ANSI: use as-is
// - If plain/markdown: process through termimad (matches non-TUI behavior)
let ansi_text = if contains_ansi(text) {
text.to_string()
} else {
// Use termimad to process markdown - this matches PrettyStreamHandler behavior
// and ensures consistent line-breaking between TUI and non-TUI modes
let skin = MadSkin::default();
skin.term_text(text).to_string()
};
// Parse ANSI codes to ratatui Text
match ansi_text.as_str().into_text() {
Ok(parsed_text) => {
// Convert Text to owned Lines
parsed_text
.lines
.into_iter()
.map(|line| {
let owned_spans: Vec<Span<'static>> = line
.spans
.into_iter()
.map(|span| Span::styled(span.content.into_owned(), span.style))
.collect();
Line::from(owned_spans)
})
.collect()
}
Err(_) => {
// Fallback: split on newlines and treat as plain text
text.split('\n')
.map(|line| Line::from(line.to_string()))
.collect()
}
}
}
/// A content block in the chronological stream.
///
/// Used to preserve ordering between text and non-text content (tool calls, errors).
#[derive(Clone)]
enum ContentBlock {
/// Markdown/ANSI text that was accumulated before being frozen
Text(String),
/// A single non-text line (tool call, error, completion summary, etc.)
NonText(Line<'static>),
}
/// Renders streaming output as ratatui Lines for TUI display.
///
/// This handler produces output visually equivalent to `PrettyStreamHandler`
/// but stores it as `Line<'static>` objects for rendering in a ratatui-based TUI.
///
/// Text content is parsed as markdown, producing styled output for bold, italic,
/// code, headers, etc. Tool calls and errors bypass markdown parsing to preserve
/// their explicit styling.
///
/// **Chronological ordering**: When a tool call arrives, the current text buffer
/// is "frozen" into a content block, preserving the order in which events arrived.
pub struct TuiStreamHandler {
/// Buffer for accumulating current markdown text (not yet frozen)
current_text_buffer: String,
/// Chronological sequence of content blocks (frozen text + non-text events)
blocks: Vec<ContentBlock>,
/// Reserved for parity with non-TUI handlers.
_verbose: bool,
/// Collected output lines for rendering
lines: Arc<Mutex<Vec<Line<'static>>>>,
}
impl TuiStreamHandler {
/// Creates a new TUI handler.
///
/// # Arguments
/// * `verbose` - If true, shows session summary.
pub fn new(verbose: bool) -> Self {
Self {
current_text_buffer: String::new(),
blocks: Vec::new(),
_verbose: verbose,
lines: Arc::new(Mutex::new(Vec::new())),
}
}
/// Creates a TUI handler with shared lines storage.
///
/// Use this to share output lines with the TUI application.
pub fn with_lines(verbose: bool, lines: Arc<Mutex<Vec<Line<'static>>>>) -> Self {
Self {
current_text_buffer: String::new(),
blocks: Vec::new(),
_verbose: verbose,
lines,
}
}
/// Returns a clone of the collected lines.
pub fn get_lines(&self) -> Vec<Line<'static>> {
self.lines.lock().unwrap().clone()
}
/// Flushes any buffered markdown text by re-parsing and updating lines.
pub fn flush_text_buffer(&mut self) {
self.update_lines();
}
/// Freezes the current text buffer into a content block.
///
/// This is called when a non-text event (tool call, error) arrives,
/// ensuring that text before the event stays before it in the output.
fn freeze_current_text(&mut self) {
if !self.current_text_buffer.is_empty() {
self.blocks
.push(ContentBlock::Text(self.current_text_buffer.clone()));
self.current_text_buffer.clear();
}
}
/// Re-renders all content blocks and updates the shared lines.
///
/// Iterates through frozen blocks in chronological order, then appends
/// any current (unfrozen) text buffer content. This preserves the
/// interleaved ordering of text and non-text content.
fn update_lines(&mut self) {
let mut all_lines = Vec::new();
// Render frozen blocks in chronological order
for block in &self.blocks {
match block {
ContentBlock::Text(text) => {
all_lines.extend(text_to_lines(text));
}
ContentBlock::NonText(line) => {
all_lines.push(line.clone());
}
}
}
// Render current (unfrozen) text buffer for real-time updates
if !self.current_text_buffer.is_empty() {
all_lines.extend(text_to_lines(&self.current_text_buffer));
}
// Note: Long lines are NOT truncated here. The TUI's ContentPane widget
// handles soft-wrapping at viewport boundaries, preserving full content.
// Update shared lines
*self.lines.lock().unwrap() = all_lines;
}
/// Adds a non-text line (tool call, error, etc.) and updates display.
///
/// First freezes any pending text buffer to preserve chronological order.
fn add_non_text_line(&mut self, line: Line<'static>) {
self.freeze_current_text();
self.blocks.push(ContentBlock::NonText(line));
self.update_lines();
}
}
impl StreamHandler for TuiStreamHandler {
fn on_text(&mut self, text: &str) {
// Append text to current buffer
self.current_text_buffer.push_str(text);
// Re-parse and update lines on each text chunk
// This handles streaming markdown correctly
self.update_lines();
}
fn on_tool_call(&mut self, name: &str, _id: &str, input: &serde_json::Value) {
// Build spans: ⚙️ [ToolName] summary
let mut spans = vec![Span::styled(
format!("\u{2699} [{}]", name),
Style::default().fg(RatatuiColor::Blue),
)];
if let Some(summary) = format_tool_summary(name, input) {
let summary = sanitize_tui_inline_text(&summary);
spans.push(Span::styled(
format!(" {}", summary),
Style::default().fg(RatatuiColor::DarkGray),
));
}
self.add_non_text_line(Line::from(spans));
}
fn on_tool_result(&mut self, _id: &str, output: &str) {
let clean = sanitize_tui_inline_text(output);
let line = Line::from(Span::styled(
format!(" \u{2713} {}", truncate(&clean, 200)),
Style::default().fg(RatatuiColor::DarkGray),
));
self.add_non_text_line(line);
}
fn on_error(&mut self, error: &str) {
let clean = sanitize_tui_inline_text(error);
let line = Line::from(Span::styled(
format!("\u{2717} Error: {}", clean),
Style::default().fg(RatatuiColor::Red),
));
self.add_non_text_line(line);
}
fn on_complete(&mut self, result: &SessionResult) {
// Flush any remaining buffered text
self.flush_text_buffer();
// Add blank line
self.add_non_text_line(Line::from(""));
// Add summary with color based on error status
let color = if result.is_error {
RatatuiColor::Red
} else {
RatatuiColor::Green
};
let summary = format!(
"Duration: {}ms | Est. cost: ${:.4} | Turns: {}",
result.duration_ms, result.total_cost_usd, result.num_turns
);
let line = Line::from(Span::styled(summary, Style::default().fg(color)));
self.add_non_text_line(line);
}
}
/// Extracts the most relevant field from tool input for display.
///
/// Returns a human-readable summary (file path, command, pattern, etc.) based on the tool type.
/// Returns `None` for unknown tools or if the expected field is missing.
fn format_tool_summary(name: &str, input: &serde_json::Value) -> Option<String> {
match name {
"Read" | "Edit" | "Write" => input.get("file_path")?.as_str().map(|s| s.to_string()),
"Bash" => {
let cmd = input.get("command")?.as_str()?;
Some(truncate(cmd, 60))
}
"Grep" => input.get("pattern")?.as_str().map(|s| s.to_string()),
"Glob" => input.get("pattern")?.as_str().map(|s| s.to_string()),
"Task" => input.get("description")?.as_str().map(|s| s.to_string()),
"WebFetch" => input.get("url")?.as_str().map(|s| s.to_string()),
"WebSearch" => input.get("query")?.as_str().map(|s| s.to_string()),
"LSP" => {
let op = input.get("operation")?.as_str()?;
let file = input.get("filePath")?.as_str()?;
Some(format!("{} @ {}", op, file))
}
"NotebookEdit" => input.get("notebook_path")?.as_str().map(|s| s.to_string()),
"TodoWrite" => Some("updating todo list".to_string()),
_ => None,
}
}
/// Truncates a string to approximately `max_len` characters, adding "..." if truncated.
///
/// Uses `char_indices` to find a valid UTF-8 boundary, ensuring we never slice
/// in the middle of a multi-byte character.
fn truncate(s: &str, max_len: usize) -> String {
if s.chars().count() <= max_len {
s.to_string()
} else {
// Find the byte index of the max_len-th character
let byte_idx = s
.char_indices()
.nth(max_len)
.map(|(idx, _)| idx)
.unwrap_or(s.len());
format!("{}...", &s[..byte_idx])
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_console_handler_verbose_shows_results() {
let mut handler = ConsoleStreamHandler::new(true);
let bash_input = json!({"command": "ls -la"});
// These calls should not panic
handler.on_text("Hello");
handler.on_tool_call("Bash", "tool_1", &bash_input);
handler.on_tool_result("tool_1", "output");
handler.on_complete(&SessionResult {
duration_ms: 1000,
total_cost_usd: 0.01,
num_turns: 1,
is_error: false,
});
}
#[test]
fn test_console_handler_normal_skips_results() {
let mut handler = ConsoleStreamHandler::new(false);
let read_input = json!({"file_path": "src/main.rs"});
// These should not show tool results
handler.on_text("Hello");
handler.on_tool_call("Read", "tool_1", &read_input);
handler.on_tool_result("tool_1", "output"); // Should be silent
handler.on_complete(&SessionResult {
duration_ms: 1000,
total_cost_usd: 0.01,
num_turns: 1,
is_error: false,
}); // Should be silent
}
#[test]
fn test_quiet_handler_is_silent() {
let mut handler = QuietStreamHandler;
let empty_input = json!({});
// All of these should be no-ops
handler.on_text("Hello");
handler.on_tool_call("Read", "tool_1", &empty_input);
handler.on_tool_result("tool_1", "output");
handler.on_error("Something went wrong");
handler.on_complete(&SessionResult {
duration_ms: 1000,
total_cost_usd: 0.01,
num_turns: 1,
is_error: false,
});
}
#[test]
fn test_truncate_helper() {
assert_eq!(truncate("short", 10), "short");
assert_eq!(truncate("this is a long string", 10), "this is a ...");
}
#[test]
fn test_truncate_utf8_boundaries() {
// Arrow → is 3 bytes (U+2192: E2 86 92)
let with_arrows = "→→→→→→→→→→";
// Should truncate at character boundary, not byte boundary
assert_eq!(truncate(with_arrows, 5), "→→→→→...");
// Mixed ASCII and multi-byte
let mixed = "a→b→c→d→e";
assert_eq!(truncate(mixed, 5), "a→b→c...");
// Emoji (4-byte characters)
let emoji = "🎉🎊🎁🎈🎄";
assert_eq!(truncate(emoji, 3), "🎉🎊🎁...");
}
#[test]
fn test_sanitize_tui_inline_text_removes_newlines_and_carriage_returns() {
let s = "hello\r\nworld\nbye\rok";
let clean = sanitize_tui_inline_text(s);
assert!(!clean.contains('\r'));
assert!(!clean.contains('\n'));
}
#[test]
fn test_text_to_lines_sanitizes_carriage_returns() {
let lines = text_to_lines("alpha\rbravo\ncharlie");
for line in lines {
for span in line.spans {
assert!(
!span.content.contains('\r'),
"Span content should not contain carriage returns: {:?}",
span.content
);
}
}
}
#[test]
fn test_format_tool_summary_file_tools() {
assert_eq!(
format_tool_summary("Read", &json!({"file_path": "src/main.rs"})),
Some("src/main.rs".to_string())
);
assert_eq!(
format_tool_summary("Edit", &json!({"file_path": "/path/to/file.txt"})),
Some("/path/to/file.txt".to_string())
);
assert_eq!(
format_tool_summary("Write", &json!({"file_path": "output.json"})),
Some("output.json".to_string())
);
}
#[test]
fn test_format_tool_summary_bash_truncates() {
let short_cmd = json!({"command": "ls -la"});
assert_eq!(
format_tool_summary("Bash", &short_cmd),
Some("ls -la".to_string())
);
let long_cmd = json!({"command": "this is a very long command that should be truncated because it exceeds sixty characters"});
let result = format_tool_summary("Bash", &long_cmd).unwrap();
assert!(result.ends_with("..."));
assert!(result.len() <= 70); // 60 chars + "..."
}
#[test]
fn test_format_tool_summary_search_tools() {
assert_eq!(
format_tool_summary("Grep", &json!({"pattern": "TODO"})),
Some("TODO".to_string())
);
assert_eq!(
format_tool_summary("Glob", &json!({"pattern": "**/*.rs"})),
Some("**/*.rs".to_string())
);
}
#[test]
fn test_format_tool_summary_unknown_tool_returns_none() {
assert_eq!(
format_tool_summary("UnknownTool", &json!({"some_field": "value"})),
None
);
}
#[test]
fn test_format_tool_summary_missing_field_returns_none() {
// Read without file_path
assert_eq!(
format_tool_summary("Read", &json!({"wrong_field": "value"})),
None
);
// Bash without command
assert_eq!(format_tool_summary("Bash", &json!({})), None);
}
// ========================================================================
// TuiStreamHandler Tests
// ========================================================================
mod tui_stream_handler {
use super::*;
use ratatui::style::{Color, Modifier};
/// Helper to collect lines from TuiStreamHandler
fn collect_lines(handler: &TuiStreamHandler) -> Vec<ratatui::text::Line<'static>> {
handler.lines.lock().unwrap().clone()
}
#[test]
fn text_creates_line_on_newline() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text("hello\n") is called
handler.on_text("hello\n");
// Then a Line with "hello" content is produced
// Note: termimad (like non-TUI mode) doesn't create empty line for trailing \n
let lines = collect_lines(&handler);
assert_eq!(
lines.len(),
1,
"termimad doesn't create trailing empty line"
);
assert_eq!(lines[0].to_string(), "hello");
}
#[test]
fn partial_text_buffering() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text("hel") then on_text("lo\n") is called
// Note: With markdown parsing, partial text is rendered immediately
// (markdown doesn't require newlines for paragraphs)
handler.on_text("hel");
handler.on_text("lo\n");
// Then the combined "hello" text is present
let lines = collect_lines(&handler);
let full_text: String = lines.iter().map(|l| l.to_string()).collect();
assert!(
full_text.contains("hello"),
"Combined text should contain 'hello'. Lines: {:?}",
lines
);
}
#[test]
fn tool_call_produces_formatted_line() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_tool_call("Read", "id", &json!({"file_path": "src/main.rs"})) is called
handler.on_tool_call("Read", "tool_1", &json!({"file_path": "src/main.rs"}));
// Then a Line starting with "⚙️" and containing "Read" and file path is produced
let lines = collect_lines(&handler);
assert_eq!(lines.len(), 1);
let line_text = lines[0].to_string();
assert!(
line_text.contains('\u{2699}'),
"Should contain gear emoji: {}",
line_text
);
assert!(
line_text.contains("Read"),
"Should contain tool name: {}",
line_text
);
assert!(
line_text.contains("src/main.rs"),
"Should contain file path: {}",
line_text
);
}
#[test]
fn tool_result_verbose_shows_content() {
// Given TuiStreamHandler with verbose=true
let mut handler = TuiStreamHandler::new(true);
// When on_tool_result(...) is called
handler.on_tool_result("tool_1", "file contents here");
// Then result content appears in output
let lines = collect_lines(&handler);
assert_eq!(lines.len(), 1);
let line_text = lines[0].to_string();
assert!(
line_text.contains('\u{2713}'),
"Should contain checkmark: {}",
line_text
);
assert!(
line_text.contains("file contents here"),
"Should contain result content: {}",
line_text
);
}
#[test]
fn tool_result_quiet_shows_content() {
// Given TuiStreamHandler with verbose=false
let mut handler = TuiStreamHandler::new(false);
// When on_tool_result(...) is called
handler.on_tool_result("tool_1", "file contents here");
// Then result content appears in output
let lines = collect_lines(&handler);
assert_eq!(lines.len(), 1);
let line_text = lines[0].to_string();
assert!(
line_text.contains('\u{2713}'),
"Should contain checkmark: {}",
line_text
);
assert!(
line_text.contains("file contents here"),
"Should contain result content: {}",
line_text
);
}
#[test]
fn error_produces_red_styled_line() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_error("fail") is called
handler.on_error("Something went wrong");
// Then a Line with red foreground style is produced
let lines = collect_lines(&handler);
assert_eq!(lines.len(), 1);
let line_text = lines[0].to_string();
assert!(
line_text.contains('\u{2717}'),
"Should contain X mark: {}",
line_text
);
assert!(
line_text.contains("Error"),
"Should contain 'Error': {}",
line_text
);
assert!(
line_text.contains("Something went wrong"),
"Should contain error message: {}",
line_text
);
// Check style is red
let first_span = &lines[0].spans[0];
assert_eq!(
first_span.style.fg,
Some(Color::Red),
"Error line should have red foreground"
);
}
#[test]
fn long_lines_preserved_without_truncation() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text() receives a very long string (500+ chars)
let long_string: String = "a".repeat(500) + "\n";
handler.on_text(&long_string);
// Then content is preserved fully (termimad may wrap at terminal width)
// Note: termimad wraps at ~80 chars by default, so 500 chars = multiple lines
let lines = collect_lines(&handler);
// Verify total content is preserved (all 500 'a's present)
let total_content: String = lines.iter().map(|l| l.to_string()).collect();
let a_count = total_content.chars().filter(|c| *c == 'a').count();
assert_eq!(
a_count, 500,
"All 500 'a' chars should be preserved. Got {}",
a_count
);
// Should not have truncation ellipsis
assert!(
!total_content.contains("..."),
"Content should not have ellipsis truncation"
);
}
#[test]
fn multiple_lines_in_single_text_call() {
// When text contains multiple newlines
let mut handler = TuiStreamHandler::new(false);
handler.on_text("line1\nline2\nline3\n");
// Then all text content is present
// Note: Markdown parsing may combine lines into paragraphs differently
let lines = collect_lines(&handler);
let full_text: String = lines
.iter()
.map(|l| l.to_string())
.collect::<Vec<_>>()
.join(" ");
assert!(
full_text.contains("line1")
&& full_text.contains("line2")
&& full_text.contains("line3"),
"All lines should be present. Lines: {:?}",
lines
);
}
#[test]
fn termimad_parity_with_non_tui_mode() {
// Verify that TUI mode (using termimad) matches non-TUI mode output
// This ensures the "★ Insight" box renders consistently in both modes
let text = "Some text before:★ Insight ─────\nKey point here";
let mut handler = TuiStreamHandler::new(false);
handler.on_text(text);
let lines = collect_lines(&handler);
// termimad wraps after "★ Insight " putting dashes on their own line
// This matches PrettyStreamHandler (non-TUI) behavior
assert!(
lines.len() >= 2,
"termimad should produce multiple lines. Got: {:?}",
lines.iter().map(|l| l.to_string()).collect::<Vec<_>>()
);
// Content should be preserved
let full_text: String = lines.iter().map(|l| l.to_string()).collect();
assert!(
full_text.contains("★ Insight"),
"Content should contain insight marker"
);
}
#[test]
fn tool_call_flushes_text_buffer() {
// Given buffered text
let mut handler = TuiStreamHandler::new(false);
handler.on_text("partial text");
// When tool call arrives
handler.on_tool_call("Read", "id", &json!({}));
// Then buffered text is flushed as a line before tool call line
let lines = collect_lines(&handler);
assert_eq!(lines.len(), 2);
assert_eq!(lines[0].to_string(), "partial text");
assert!(lines[1].to_string().contains('\u{2699}'));
}
#[test]
fn interleaved_text_and_tools_preserves_chronological_order() {
// Given: text1 → tool1 → text2 → tool2
// Expected output order: text1, tool1, text2, tool2
// NOT: text1 + text2, then tool1 + tool2 (the bug we fixed)
let mut handler = TuiStreamHandler::new(false);
// Simulate Claude's streaming output pattern
handler.on_text("I'll start by reviewing the scratchpad.\n");
handler.on_tool_call("Read", "id1", &json!({"file_path": "scratchpad.md"}));
handler.on_text("I found the task. Now checking the code.\n");
handler.on_tool_call("Read", "id2", &json!({"file_path": "main.rs"}));
handler.on_text("Done reviewing.\n");
let lines = collect_lines(&handler);
// Find indices of key content
let text1_idx = lines
.iter()
.position(|l| l.to_string().contains("reviewing the scratchpad"));
let tool1_idx = lines
.iter()
.position(|l| l.to_string().contains("scratchpad.md"));
let text2_idx = lines
.iter()
.position(|l| l.to_string().contains("checking the code"));
let tool2_idx = lines.iter().position(|l| l.to_string().contains("main.rs"));
let text3_idx = lines
.iter()
.position(|l| l.to_string().contains("Done reviewing"));
// All content should be present
assert!(text1_idx.is_some(), "text1 should be present");
assert!(tool1_idx.is_some(), "tool1 should be present");
assert!(text2_idx.is_some(), "text2 should be present");
assert!(tool2_idx.is_some(), "tool2 should be present");
assert!(text3_idx.is_some(), "text3 should be present");
// Chronological order must be preserved
let text1_idx = text1_idx.unwrap();
let tool1_idx = tool1_idx.unwrap();
let text2_idx = text2_idx.unwrap();
let tool2_idx = tool2_idx.unwrap();
let text3_idx = text3_idx.unwrap();
assert!(
text1_idx < tool1_idx,
"text1 ({}) should come before tool1 ({}). Lines: {:?}",
text1_idx,
tool1_idx,
lines.iter().map(|l| l.to_string()).collect::<Vec<_>>()
);
assert!(
tool1_idx < text2_idx,
"tool1 ({}) should come before text2 ({}). Lines: {:?}",
tool1_idx,
text2_idx,
lines.iter().map(|l| l.to_string()).collect::<Vec<_>>()
);
assert!(
text2_idx < tool2_idx,
"text2 ({}) should come before tool2 ({}). Lines: {:?}",
text2_idx,
tool2_idx,
lines.iter().map(|l| l.to_string()).collect::<Vec<_>>()
);
assert!(
tool2_idx < text3_idx,
"tool2 ({}) should come before text3 ({}). Lines: {:?}",
tool2_idx,
text3_idx,
lines.iter().map(|l| l.to_string()).collect::<Vec<_>>()
);
}
#[test]
fn on_complete_flushes_buffer_and_shows_summary() {
// Given buffered text and verbose mode
let mut handler = TuiStreamHandler::new(true);
handler.on_text("final output");
// When on_complete is called
handler.on_complete(&SessionResult {
duration_ms: 1500,
total_cost_usd: 0.0025,
num_turns: 3,
is_error: false,
});
// Then buffer is flushed and summary line appears
let lines = collect_lines(&handler);
assert!(lines.len() >= 2, "Should have at least 2 lines");
assert_eq!(lines[0].to_string(), "final output");
// Find summary line
let summary = lines.last().unwrap().to_string();
assert!(
summary.contains("1500"),
"Should contain duration: {}",
summary
);
assert!(
summary.contains("0.0025"),
"Should contain cost: {}",
summary
);
assert!(summary.contains('3'), "Should contain turns: {}", summary);
}
#[test]
fn on_complete_error_uses_red_style() {
let mut handler = TuiStreamHandler::new(true);
handler.on_complete(&SessionResult {
duration_ms: 1000,
total_cost_usd: 0.01,
num_turns: 1,
is_error: true,
});
let lines = collect_lines(&handler);
assert!(!lines.is_empty());
// Last line should be red styled for error
let last_line = lines.last().unwrap();
assert_eq!(
last_line.spans[0].style.fg,
Some(Color::Red),
"Error completion should have red foreground"
);
}
#[test]
fn on_complete_success_uses_green_style() {
let mut handler = TuiStreamHandler::new(true);
handler.on_complete(&SessionResult {
duration_ms: 1000,
total_cost_usd: 0.01,
num_turns: 1,
is_error: false,
});
let lines = collect_lines(&handler);
assert!(!lines.is_empty());
// Last line should be green styled for success
let last_line = lines.last().unwrap();
assert_eq!(
last_line.spans[0].style.fg,
Some(Color::Green),
"Success completion should have green foreground"
);
}
#[test]
fn tool_call_with_no_summary_shows_just_name() {
let mut handler = TuiStreamHandler::new(false);
handler.on_tool_call("UnknownTool", "id", &json!({}));
let lines = collect_lines(&handler);
assert_eq!(lines.len(), 1);
let line_text = lines[0].to_string();
assert!(line_text.contains("UnknownTool"));
// Should not crash or show "null" for missing summary
}
#[test]
fn get_lines_returns_clone_of_internal_lines() {
let mut handler = TuiStreamHandler::new(false);
handler.on_text("test\n");
let lines1 = handler.get_lines();
let lines2 = handler.get_lines();
// Both should have same content
assert_eq!(lines1.len(), lines2.len());
assert_eq!(lines1[0].to_string(), lines2[0].to_string());
}
// =====================================================================
// Markdown Rendering Tests
// =====================================================================
#[test]
fn markdown_bold_text_renders_with_bold_modifier() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text("**important**\n") is called
handler.on_text("**important**\n");
// Then the text "important" appears with BOLD modifier
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
// Find a span containing "important" and check it's bold
let has_bold = lines.iter().any(|line| {
line.spans.iter().any(|span| {
span.content.contains("important")
&& span.style.add_modifier.contains(Modifier::BOLD)
})
});
assert!(
has_bold,
"Should have bold 'important' span. Lines: {:?}",
lines
);
}
#[test]
fn markdown_italic_text_renders_with_italic_modifier() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text("*emphasized*\n") is called
handler.on_text("*emphasized*\n");
// Then the text "emphasized" appears with ITALIC modifier
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_italic = lines.iter().any(|line| {
line.spans.iter().any(|span| {
span.content.contains("emphasized")
&& span.style.add_modifier.contains(Modifier::ITALIC)
})
});
assert!(
has_italic,
"Should have italic 'emphasized' span. Lines: {:?}",
lines
);
}
#[test]
fn markdown_inline_code_renders_with_distinct_style() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text("`code`\n") is called
handler.on_text("`code`\n");
// Then the text "code" appears with distinct styling (different from default)
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_code_style = lines.iter().any(|line| {
line.spans.iter().any(|span| {
span.content.contains("code")
&& (span.style.fg.is_some() || span.style.bg.is_some())
})
});
assert!(
has_code_style,
"Should have styled 'code' span. Lines: {:?}",
lines
);
}
#[test]
fn markdown_header_renders_content() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text("## Section Title\n") is called
handler.on_text("## Section Title\n");
// Then "Section Title" appears in the output
// Note: termimad applies ANSI styling to headers
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_header_content = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.content.contains("Section Title"))
});
assert!(
has_header_content,
"Should have header content. Lines: {:?}",
lines
);
}
#[test]
fn markdown_streaming_continuity_handles_split_formatting() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When markdown arrives in chunks: "**bo" then "ld**\n"
handler.on_text("**bo");
handler.on_text("ld**\n");
// Then the complete "bold" text renders with BOLD modifier
let lines = collect_lines(&handler);
let has_bold = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.add_modifier.contains(Modifier::BOLD))
});
assert!(
has_bold,
"Split markdown should still render bold. Lines: {:?}",
lines
);
}
#[test]
fn markdown_mixed_content_renders_correctly() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text() receives mixed markdown
handler.on_text("Normal **bold** and *italic* text\n");
// Then appropriate spans have appropriate styling
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_bold = lines.iter().any(|line| {
line.spans.iter().any(|span| {
span.content.contains("bold")
&& span.style.add_modifier.contains(Modifier::BOLD)
})
});
let has_italic = lines.iter().any(|line| {
line.spans.iter().any(|span| {
span.content.contains("italic")
&& span.style.add_modifier.contains(Modifier::ITALIC)
})
});
assert!(has_bold, "Should have bold span. Lines: {:?}", lines);
assert!(has_italic, "Should have italic span. Lines: {:?}", lines);
}
#[test]
fn markdown_tool_call_styling_preserved() {
// Given TuiStreamHandler with markdown text then tool call
let mut handler = TuiStreamHandler::new(false);
// When markdown text followed by tool call
handler.on_text("**bold**\n");
handler.on_tool_call("Read", "id", &json!({"file_path": "src/main.rs"}));
// Then tool call still has blue styling
let lines = collect_lines(&handler);
assert!(lines.len() >= 2, "Should have at least 2 lines");
// Last line should be the tool call with blue color
let tool_line = lines.last().unwrap();
let has_blue = tool_line
.spans
.iter()
.any(|span| span.style.fg == Some(Color::Blue));
assert!(
has_blue,
"Tool call should preserve blue styling. Line: {:?}",
tool_line
);
}
#[test]
fn markdown_error_styling_preserved() {
// Given TuiStreamHandler with markdown text then error
let mut handler = TuiStreamHandler::new(false);
// When markdown text followed by error
handler.on_text("**bold**\n");
handler.on_error("Something went wrong");
// Then error still has red styling
let lines = collect_lines(&handler);
assert!(lines.len() >= 2, "Should have at least 2 lines");
// Last line should be the error with red color
let error_line = lines.last().unwrap();
let has_red = error_line
.spans
.iter()
.any(|span| span.style.fg == Some(Color::Red));
assert!(
has_red,
"Error should preserve red styling. Line: {:?}",
error_line
);
}
#[test]
fn markdown_partial_formatting_does_not_crash() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When incomplete markdown is sent and flushed
handler.on_text("**unclosed bold");
handler.flush_text_buffer();
// Then no panic occurs and text is present
let lines = collect_lines(&handler);
// Should have some output (either the partial text or nothing)
// Main assertion is that we didn't panic
let _ = lines; // Use the variable to avoid warning
}
// =====================================================================
// ANSI Color Preservation Tests
// =====================================================================
#[test]
fn ansi_green_text_produces_green_style() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives ANSI green text
handler.on_text("\x1b[32mgreen text\x1b[0m\n");
// Then the text should have green foreground color
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_green = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.fg == Some(Color::Green))
});
assert!(
has_green,
"Should have green styled span. Lines: {:?}",
lines
);
}
#[test]
fn ansi_bold_text_produces_bold_modifier() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives ANSI bold text
handler.on_text("\x1b[1mbold text\x1b[0m\n");
// Then the text should have BOLD modifier
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_bold = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.add_modifier.contains(Modifier::BOLD))
});
assert!(has_bold, "Should have bold styled span. Lines: {:?}", lines);
}
#[test]
fn ansi_mixed_styles_preserved() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives mixed ANSI styles (bold + green)
handler.on_text("\x1b[1;32mbold green\x1b[0m normal\n");
// Then the text should have appropriate styles
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
// Check for green color
let has_styled = lines.iter().any(|line| {
line.spans.iter().any(|span| {
span.style.fg == Some(Color::Green)
|| span.style.add_modifier.contains(Modifier::BOLD)
})
});
assert!(
has_styled,
"Should have styled span with color or bold. Lines: {:?}",
lines
);
}
#[test]
fn ansi_plain_text_renders_without_crash() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives plain text (no ANSI)
handler.on_text("plain text without ansi\n");
// Then text renders normally (fallback to markdown)
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let full_text: String = lines.iter().map(|l| l.to_string()).collect();
assert!(
full_text.contains("plain text"),
"Should contain the text. Lines: {:?}",
lines
);
}
#[test]
fn ansi_red_error_text_produces_red_style() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives ANSI red text (like error output)
handler.on_text("\x1b[31mError: something failed\x1b[0m\n");
// Then the text should have red foreground color
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_red = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.fg == Some(Color::Red))
});
assert!(has_red, "Should have red styled span. Lines: {:?}", lines);
}
#[test]
fn ansi_cyan_text_produces_cyan_style() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives ANSI cyan text
handler.on_text("\x1b[36mcyan text\x1b[0m\n");
// Then the text should have cyan foreground color
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_cyan = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.fg == Some(Color::Cyan))
});
assert!(has_cyan, "Should have cyan styled span. Lines: {:?}", lines);
}
#[test]
fn ansi_underline_produces_underline_modifier() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives ANSI underlined text
handler.on_text("\x1b[4munderlined\x1b[0m\n");
// Then the text should have UNDERLINED modifier
let lines = collect_lines(&handler);
assert!(!lines.is_empty(), "Should have at least one line");
let has_underline = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.add_modifier.contains(Modifier::UNDERLINED))
});
assert!(
has_underline,
"Should have underlined styled span. Lines: {:?}",
lines
);
}
#[test]
fn ansi_multiline_preserves_colors() {
// Given TuiStreamHandler
let mut handler = TuiStreamHandler::new(false);
// When on_text receives multiple ANSI-colored lines
handler.on_text("\x1b[32mline 1 green\x1b[0m\n\x1b[31mline 2 red\x1b[0m\n");
// Then both colors should be present
let lines = collect_lines(&handler);
assert!(lines.len() >= 2, "Should have at least two lines");
let has_green = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.fg == Some(Color::Green))
});
let has_red = lines.iter().any(|line| {
line.spans
.iter()
.any(|span| span.style.fg == Some(Color::Red))
});
assert!(has_green, "Should have green line. Lines: {:?}", lines);
assert!(has_red, "Should have red line. Lines: {:?}", lines);
}
}
}
// =========================================================================
// ANSI Detection Tests (module-level)
// =========================================================================
#[cfg(test)]
mod ansi_detection_tests {
use super::*;
#[test]
fn contains_ansi_with_color_code() {
assert!(contains_ansi("\x1b[32mgreen\x1b[0m"));
}
#[test]
fn contains_ansi_with_bold() {
assert!(contains_ansi("\x1b[1mbold\x1b[0m"));
}
#[test]
fn contains_ansi_plain_text_returns_false() {
assert!(!contains_ansi("hello world"));
}
#[test]
fn contains_ansi_markdown_returns_false() {
assert!(!contains_ansi("**bold** and *italic*"));
}
#[test]
fn contains_ansi_empty_string_returns_false() {
assert!(!contains_ansi(""));
}
#[test]
fn contains_ansi_with_escape_in_middle() {
assert!(contains_ansi("prefix \x1b[31mred\x1b[0m suffix"));
}
}