trusty_code_tui/text.rs
1//! Pure text-shaping helpers shared by [`crate::app`]'s chat mutators and
2//! [`crate::widgets::scrollback`]'s renderer.
3//!
4//! Why: LLM/agent responses routinely arrive with extra leading/trailing
5//! blank lines (a model's closing paragraph break) and interior `\n\n`
6//! paragraph gaps that read as wasted vertical space once rendered into a
7//! fixed-height terminal pane. Both problems are pure string transforms with
8//! no dependency on `ReplApp` or ratatui, so they live in their own module
9//! rather than being duplicated between the state mutator that trims stored
10//! text ([`crate::app::ReplApp::push_assistant`]) and any future consumer
11//! that needs the same shaping. Extracted verbatim (behavior-preserving) from
12//! `crates/trusty-agents/src/repl/tui/helpers.rs`'s `trim_surrounding_blank_lines`/
13//! `strip_interior_blank_lines` (DOC-50 §3.1/§5 Slice 4 migration).
14//!
15//! # Spec References
16//! - [`SPEC-TTUI-05~draft`](docs/specs/DOC-50-tcode-tui-claude-code-clone.md#SPEC-TTUI-05~draft) — Slice 4 deliverable (§5, Slice 4).
17
18/// Strip leading and trailing whitespace-only lines from a multi-line string.
19///
20/// Why: Assistant responses regularly include extra `\n\n` at the head or
21/// tail. Rendering them verbatim leaves visible blank rows in the chat
22/// scrollback, which reads as a UI bug. Trimming once at the boundary keeps
23/// interior blank lines (which carry paragraph-break meaning) intact.
24/// What: Trims trailing whitespace first, then drops leading whitespace-only
25/// lines one at a time. Interior blank lines are untouched.
26/// Test: `tests::trim_surrounding_blank_lines_strips_leading_and_trailing`,
27/// `tests::trim_surrounding_blank_lines_preserves_when_no_blanks`,
28/// `tests::trim_surrounding_blank_lines_empty_input`.
29pub fn trim_surrounding_blank_lines(s: &str) -> String {
30 let trimmed_end = s.trim_end();
31 let mut start = 0usize;
32 let bytes = trimmed_end.as_bytes();
33 while start < bytes.len() {
34 let nl = bytes[start..]
35 .iter()
36 .position(|&b| b == b'\n')
37 .map(|p| start + p);
38 let line_end = nl.unwrap_or(bytes.len());
39 let line = &trimmed_end[start..line_end];
40 if line.trim().is_empty() {
41 start = match nl {
42 Some(p) => p + 1,
43 None => bytes.len(),
44 };
45 } else {
46 break;
47 }
48 }
49 trimmed_end[start..].to_string()
50}
51
52/// Drop every whitespace-only line from within a response.
53///
54/// Why: Markdown-style double-newline paragraph breaks accumulate into
55/// wasted vertical space in a terminal chat panel — the user already reads
56/// consecutive paragraphs as one flowing thought, so the gap just pushes
57/// later content off-screen sooner. Removing every interior blank produces a
58/// tight, compact response block.
59/// What: Returns a string where every whitespace-only line is dropped;
60/// non-blank lines are preserved verbatim and rejoined with single `\n`s.
61/// Test: `tests::strip_interior_blank_lines_drops_all_blanks`,
62/// `tests::strip_interior_blank_lines_drops_single_blank`,
63/// `tests::strip_interior_blank_lines_treats_whitespace_only_as_blank`.
64pub fn strip_interior_blank_lines(s: &str) -> String {
65 let mut out = String::with_capacity(s.len());
66 let mut first = true;
67 for line in s.split('\n') {
68 if line.trim().is_empty() {
69 continue;
70 }
71 if !first {
72 out.push('\n');
73 }
74 out.push_str(line);
75 first = false;
76 }
77 out
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn trim_surrounding_blank_lines_strips_leading_and_trailing() {
86 let input = "\n\n \nhello\n\nworld\n\n \n";
87 let out = trim_surrounding_blank_lines(input);
88 assert_eq!(out, "hello\n\nworld");
89 }
90
91 #[test]
92 fn trim_surrounding_blank_lines_preserves_when_no_blanks() {
93 assert_eq!(trim_surrounding_blank_lines("hi"), "hi");
94 assert_eq!(trim_surrounding_blank_lines("a\nb"), "a\nb");
95 }
96
97 #[test]
98 fn trim_surrounding_blank_lines_empty_input() {
99 assert_eq!(trim_surrounding_blank_lines(""), "");
100 assert_eq!(trim_surrounding_blank_lines("\n\n \n"), "");
101 }
102
103 #[test]
104 fn strip_interior_blank_lines_drops_all_blanks() {
105 let input = "para1\n\n\npara2\n\n\n\npara3";
106 let out = strip_interior_blank_lines(input);
107 assert_eq!(out, "para1\npara2\npara3");
108 }
109
110 #[test]
111 fn strip_interior_blank_lines_drops_single_blank() {
112 let input = "para1\n\npara2";
113 assert_eq!(strip_interior_blank_lines(input), "para1\npara2");
114 }
115
116 #[test]
117 fn strip_interior_blank_lines_treats_whitespace_only_as_blank() {
118 let input = "a\n \n\t\nb";
119 assert_eq!(strip_interior_blank_lines(input), "a\nb");
120 }
121}