thndrs 0.1.0

Terminal AI pair programmer with local tools, sessions, MCP, and ACP support
Documentation
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
//! Width-aware layout helpers for the row model.
//!
//! These functions operate on the renderer's own [`Span`] and [`CellStyle`]
//! types. They are the single source of truth for wrapping, padding, and
//! truncation so that cursor placement and snapshots stay deterministic.

use crate::utils;

use super::style::{CellStyle, Span};
use unicode_segmentation::UnicodeSegmentation;

/// Maximum width usable for body content inside a padded block.
///
/// Uses two-cell left / two-cell right block padding used so
/// transcript rows line up with the live region.
pub fn content_width(max_width: usize) -> usize {
    let left_pad = max_width.min(2);
    let right_pad = max_width.saturating_sub(left_pad).min(2);
    max_width.saturating_sub(left_pad + right_pad)
}

/// Word-wrap plain text into rows no wider than `width`.
///
/// Existing `\n` line breaks are preserved. Overlong words are hard-split.
pub fn wrap_text(text: &str, width: usize) -> Vec<String> {
    let width = width.max(1);
    let mut rows = Vec::new();

    for raw_line in text.split('\n') {
        if raw_line.is_empty() {
            rows.push(String::new());
            continue;
        }

        let mut current = String::new();
        let mut current_width = 0usize;
        for word in raw_line.split_whitespace() {
            let word_len = utils::text_width(word);

            if current_width == 0 {
                if word_len <= width {
                    current.push_str(word);
                    current_width = word_len;
                } else {
                    rows.extend(split_long_word(word, width));
                }
            } else if current_width + 1 + word_len <= width {
                current.push(' ');
                current.push_str(word);
                current_width += 1 + word_len;
            } else {
                rows.push(std::mem::take(&mut current));
                current_width = 0;
                if word_len <= width {
                    current.push_str(word);
                    current_width = word_len;
                } else {
                    rows.extend(split_long_word(word, width));
                }
            }
        }

        if !current.is_empty() {
            rows.push(current);
        }
    }

    rows
}

/// Wrap plain text without normalizing whitespace.
///
/// Existing `\n` line breaks, indentation, and repeated spaces are preserved.
/// Overlong lines are hard-split at grapheme boundaries.
pub fn wrap_text_preserving_whitespace(text: &str, width: usize) -> Vec<String> {
    let width = width.max(1);
    let mut rows = Vec::new();

    for raw_line in text.split('\n') {
        if raw_line.is_empty() {
            rows.push(String::new());
            continue;
        }

        let mut current = String::new();
        let mut current_width = 0usize;
        for grapheme in raw_line.graphemes(true) {
            let g_width = utils::grapheme_width(grapheme);
            if current_width > 0 && current_width + g_width > width {
                rows.push(std::mem::take(&mut current));
                current_width = 0;
            }
            current.push_str(grapheme);
            current_width += g_width;
        }

        rows.push(current);
    }

    rows
}

/// Wrap styled spans into rows no wider than `width`, preserving explicit
/// `\n` newlines inside span text.
///
/// Spans are split at character boundaries; adjacent spans keep their
/// individual styles. A single logical row can contain multiple spans. Blank
/// (empty-text) input produces a single empty row so callers always get at
/// least one row.
pub fn wrap_spans(spans: &[Span], width: usize) -> Vec<Vec<Span>> {
    let width = width.max(1);
    let mut rows: Vec<Vec<Span>> = Vec::new();
    let mut current: Vec<Span> = Vec::new();
    let mut current_width = 0usize;

    for span in spans {
        for grapheme in span.text.graphemes(true) {
            if grapheme.contains('\n') {
                rows.push(std::mem::take(&mut current));
                current_width = 0;
                continue;
            }
            let g_width = utils::grapheme_width(grapheme);
            if current_width > 0 && current_width + g_width > width {
                rows.push(std::mem::take(&mut current));
                current_width = 0;
            }
            if let Some(last) = current.last_mut()
                && last.style == span.style
            {
                last.text.push_str(grapheme);
            } else {
                current.push(Span { text: grapheme.to_string(), style: span.style });
            }
            current_width += g_width;
        }
    }

    if !current.is_empty() || rows.is_empty() {
        rows.push(current);
    }
    rows
}

/// Left-pad a row's spans with `count` cells using `pad_style`.
pub fn pad_left(spans: Vec<Span>, count: usize, pad_style: CellStyle) -> Vec<Span> {
    if count == 0 {
        return spans;
    }
    let mut out = Vec::with_capacity(spans.len() + 1);
    out.push(Span::styled(" ".repeat(count), pad_style));
    out.extend(spans);
    out
}

/// Right-pad a row's spans with `count` cells using `pad_style`.
pub fn pad_right(spans: Vec<Span>, count: usize, pad_style: CellStyle) -> Vec<Span> {
    if count == 0 {
        return spans;
    }
    let mut out = spans;
    out.push(Span::styled(" ".repeat(count), pad_style));
    out
}

/// Pad a row on both sides to reach exactly `width` columns.
///
/// Left pad is `min(2)`, right pad absorbs the remainder. Matches the
/// renderer's transcript block padding so committed rows align with the live
/// region.
pub fn pad_row(spans: Vec<Span>, width: usize, pad_style: CellStyle) -> Vec<Span> {
    if width == 0 {
        return Vec::new();
    }
    let used = spans_width(&spans);
    let left_pad = width.min(2);
    let right_pad = width.saturating_sub(left_pad + used).min(2);
    let mut out = pad_left(spans, left_pad, pad_style);
    let body = width.saturating_sub(left_pad + right_pad);
    let fill = body.saturating_sub(used);
    if fill > 0 {
        out.push(Span::styled(" ".repeat(fill), pad_style));
    }
    out = pad_right(out, right_pad, pad_style);
    out
}

/// Truncate spans to `width` columns, appending `…` if anything was cut.
///
/// The ellipsis occupies one column, so the maximum retained content is
/// `width - 1` when truncation occurs.
pub fn truncate_spans(spans: &[Span], width: usize, ellipsis_style: CellStyle) -> Vec<Span> {
    if width == 0 {
        return Vec::new();
    }
    if spans_width(spans) <= width {
        return spans.to_vec();
    }

    let keep_width = width.saturating_sub(1);
    let mut out = Vec::new();
    let mut used = 0usize;

    for span in spans {
        if used >= keep_width {
            break;
        }
        let mut taken = String::new();
        for grapheme in span.text.graphemes(true) {
            let g_width = utils::grapheme_width(grapheme);
            if used + g_width > keep_width {
                break;
            }
            taken.push_str(grapheme);
            used += g_width;
        }
        if !taken.is_empty() {
            out.push(Span { text: taken, style: span.style });
        }
    }

    out.push(Span::styled("".to_string(), ellipsis_style));
    out
}

/// Width (column count) of a span slice.
pub fn spans_width(spans: &[Span]) -> usize {
    spans.iter().map(|s| utils::text_width(&s.text)).sum()
}

/// Hard-split a single word into chunks no wider than `width`.
fn split_long_word(word: &str, width: usize) -> Vec<String> {
    let mut chunks = Vec::new();
    let mut current = String::new();
    let mut current_width = 0usize;
    for grapheme in word.graphemes(true) {
        let g_width = utils::grapheme_width(grapheme);
        if current_width > 0 && current_width + g_width > width {
            chunks.push(std::mem::take(&mut current));
            current_width = 0;
        }
        current.push_str(grapheme);
        current_width += g_width;
    }
    if !current.is_empty() {
        chunks.push(current);
    }
    chunks
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::renderer::style::Color;

    #[test]
    fn content_width_subtracts_block_padding() {
        assert_eq!(content_width(80), 76);
        assert_eq!(content_width(4), 0);
        assert_eq!(content_width(0), 0);
        assert_eq!(content_width(6), 2);
    }

    #[test]
    fn wrap_text_preserves_newlines() {
        let rows = wrap_text("hello\nworld", 80);
        assert_eq!(rows, vec!["hello", "world"]);
    }

    #[test]
    fn wrap_text_empty_line_preserved() {
        let rows = wrap_text("a\n\nb", 80);
        assert_eq!(rows, vec!["a", "", "b"]);
    }

    #[test]
    fn wrap_text_preserving_whitespace_keeps_indentation() {
        let rows = wrap_text_preserving_whitespace("src/main.rs:2:    println!(\"hello\");", 80);
        assert_eq!(rows, vec!["src/main.rs:2:    println!(\"hello\");"]);
    }

    #[test]
    fn wrap_text_preserving_whitespace_keeps_repeated_spaces() {
        let rows = wrap_text_preserving_whitespace("alpha    beta", 80);
        assert_eq!(rows, vec!["alpha    beta"]);
    }

    #[test]
    fn wrap_text_preserving_whitespace_hard_splits_lines() {
        let rows = wrap_text_preserving_whitespace("  abcdef", 4);
        assert_eq!(rows, vec!["  ab", "cdef"]);
    }

    #[test]
    fn wrap_text_word_wraps() {
        let rows = wrap_text("aa bb cc", 5);
        assert_eq!(rows, vec!["aa bb", "cc"]);
    }

    #[test]
    fn wrap_text_hard_splits_long_words() {
        let rows = wrap_text("abcdef", 3);
        assert_eq!(rows, vec!["abc", "def"]);
    }

    #[test]
    fn wrap_text_uses_display_width() {
        let rows = wrap_text("ab中cd", 4);
        assert_eq!(rows, vec!["ab中", "cd"]);
    }

    #[test]
    fn wrap_text_keeps_emoji_zwj_grapheme_together() {
        let family = "👨\u{200d}👩\u{200d}👧";
        let rows = wrap_text(&format!("a{family}b"), 3);
        assert_eq!(rows, vec![format!("a{family}"), "b".to_string()]);
    }

    #[test]
    fn wrap_spans_preserves_styles() {
        let spans = vec![
            Span::styled("red", CellStyle::new().fg(Color::Red)),
            Span::styled("blue", CellStyle::new().fg(Color::Blue)),
        ];
        let rows = wrap_spans(&spans, 10);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].len(), 2);
        assert_eq!(rows[0][0].text, "red");
        assert_eq!(rows[0][1].text, "blue");
    }

    #[test]
    fn wrap_spans_preserves_newlines() {
        let spans = vec![Span::plain("ab\ncd")];
        let rows = wrap_spans(&spans, 80);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0].text, "ab");
        assert_eq!(rows[1][0].text, "cd");
    }

    #[test]
    fn wrap_spans_empty_input_produces_empty_row() {
        let rows = wrap_spans(&[], 10);
        assert_eq!(rows, vec![vec![]]);
    }

    #[test]
    fn wrap_spans_splits_at_width() {
        let spans = vec![Span::plain("abcdef")];
        let rows = wrap_spans(&spans, 3);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0].text, "abc");
        assert_eq!(rows[1][0].text, "def");
    }

    #[test]
    fn wrap_spans_uses_display_width() {
        let spans = vec![Span::plain("a中b")];
        let rows = wrap_spans(&spans, 3);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0].text, "a中");
        assert_eq!(rows[1][0].text, "b");
    }

    #[test]
    fn wrap_spans_keeps_combining_mark_with_base() {
        let spans = vec![Span::plain("ab\u{0327}")];
        let rows = wrap_spans(&spans, 2);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0].text, "ab\u{0327}");
    }

    #[test]
    fn wrap_spans_keeps_emoji_zwj_grapheme_together() {
        let family = "👨\u{200d}👩\u{200d}👧";
        let spans = vec![Span::plain(format!("a{family}b"))];
        let rows = wrap_spans(&spans, 3);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0].text, format!("a{family}"));
        assert_eq!(rows[1][0].text, "b");
    }

    #[test]
    fn pad_row_adds_left_and_right_padding() {
        let spans = vec![Span::plain("hi")];
        let padded = pad_row(spans, 10, CellStyle::new());
        assert_eq!(spans_width(&padded), 10);
        assert_eq!(padded[0].text, "  ");
        assert_eq!(padded.last().unwrap().text, "  ");
    }

    #[test]
    fn pad_row_zero_width_returns_empty() {
        let spans = vec![Span::plain("hi")];
        let padded = pad_row(spans, 0, CellStyle::new());
        assert!(padded.is_empty());
    }

    #[test]
    fn truncate_spans_adds_ellipsis() {
        let spans = vec![Span::plain("hello world")];
        let out = truncate_spans(&spans, 8, CellStyle::default());
        assert_eq!(spans_width(&out), 8);
        assert!(out.last().unwrap().text.contains(''));
    }

    #[test]
    fn truncate_spans_short_unchanged() {
        let spans = vec![Span::plain("hi")];
        let out = truncate_spans(&spans, 10, CellStyle::default());
        assert_eq!(spans_width(&out), 2);
        assert_eq!(out[0].text, "hi");
    }

    #[test]
    fn truncate_spans_zero_width_empty() {
        let spans = vec![Span::plain("hi")];
        let out = truncate_spans(&spans, 0, CellStyle::default());
        assert!(out.is_empty());
    }

    #[test]
    fn truncate_spans_keeps_emoji_zwj_grapheme_together() {
        let family = "👨\u{200d}👩\u{200d}👧";
        let spans = vec![Span::plain(format!("{family}abc"))];
        let out = truncate_spans(&spans, 4, CellStyle::default());
        assert_eq!(spans_width(&out), 4);
        assert_eq!(out[0].text, format!("{family}a"));
        assert_eq!(out.last().unwrap().text, "");
    }

    #[test]
    fn wrap_text_wraps_long_url() {
        let url = "https://github.com/stormlight-labs/thndrs/blob/main/src/renderer/layout.rs";
        let rows = wrap_text(url, 30);
        assert!(rows.len() > 1, "long URL should wrap to multiple rows");
        assert!(
            rows.iter().all(|r| utils::text_width(r) <= 30),
            "no wrapped row should exceed width 30"
        );

        let joined = rows.join("");
        assert!(joined.contains("thndrs"), "URL content should survive wrapping");
    }

    #[test]
    fn wrap_text_hard_splits_unbroken_url() {
        let url = "https://example.com/very/long/path/that/exceeds/the/width";
        let rows = wrap_text(url, 20);
        assert!(rows.len() > 1, "long unbroken URL should hard-split");
        assert!(
            rows.iter().all(|r| utils::text_width(r) <= 20),
            "no row should exceed width 20"
        );
    }

    #[test]
    fn wrap_text_wraps_prose_paragraph() {
        let prose = "The renderer must keep display width and text boundaries separate. \
                     Display width decides cell budgets and cursor columns. Grapheme \
                     boundaries decide edit, wrap, truncate, and backend clipping steps.";
        let rows = wrap_text(prose, 40);
        assert!(rows.len() > 2, "prose paragraph should wrap to multiple rows");
        assert!(
            rows.iter().all(|r| utils::text_width(r) <= 40),
            "no wrapped row should exceed width 40"
        );
        assert!(
            rows[0].contains("renderer"),
            "first row should start with the beginning of the prose"
        );
    }

    #[test]
    fn wrap_spans_wraps_mixed_styled_spans() {
        let spans = vec![
            Span::styled("error: ", CellStyle::new().fg(Color::Red).bold()),
            Span::styled("mismatched types", CellStyle::new().fg(Color::Yellow)),
            Span::plain(" in src/main.rs at line 42"),
        ];
        let rows = wrap_spans(&spans, 20);
        assert!(rows.len() > 1, "mixed styled spans should wrap to multiple rows");
        assert!(
            rows.iter().all(|r| spans_width(r) <= 20),
            "no wrapped row should exceed width 20"
        );
        assert!(rows[0][0].text.contains("error"));
    }

    #[test]
    fn wrap_spans_preserves_style_boundaries_across_wrap() {
        let spans = vec![
            Span::styled("red-text-here", CellStyle::new().fg(Color::Red)),
            Span::styled("blue-text-here", CellStyle::new().fg(Color::Blue)),
        ];
        let rows = wrap_spans(&spans, 14);
        assert!(rows.len() > 1, "should wrap at width 14");
        for row in &rows {
            let styles: Vec<_> = row.iter().map(|s| s.style).collect();
            for window in styles.windows(2) {
                assert_ne!(window[0], window[1], "adjacent spans should have distinct styles");
            }
        }
    }

    #[test]
    fn wrap_text_terminal_cell_clipping_cjk_at_boundary() {
        let rows = wrap_text("ab中c", 3);
        assert_eq!(rows, vec!["ab".to_string(), "中c".to_string()]);
    }

    #[test]
    fn wrap_spans_clips_wide_grapheme_at_boundary() {
        let flag = "🇺🇸";
        let spans = vec![Span::plain(format!("a{flag}b"))];
        let rows = wrap_spans(&spans, 2);
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0][0].text, "a");
        assert_eq!(rows[1][0].text, flag);
        assert_eq!(rows[2][0].text, "b");
    }
}