chabeau 0.7.3

A full-screen terminal chat interface that connects to various AI APIs for real-time conversations
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
use crate::ui::span::SpanKind;
use ratatui::{style::Style, text::Span};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

/// Wrap spans to the provided width while preserving styles and word boundaries.
/// Shared between markdown rendering and range computation so downstream
/// consumers stay in sync. A continuation indent width can be supplied to
/// account for prefixes added after wrapping (e.g., hanging indents).
pub(crate) fn wrap_spans_to_width_generic_shared(
    spans: &[(Span<'static>, SpanKind)],
    max_width: usize,
    continuation_indent_width: usize,
) -> Vec<Vec<(Span<'static>, SpanKind)>> {
    const MAX_UNBREAKABLE_LENGTH: usize = 30;
    if spans.is_empty() {
        return vec![Vec::new()];
    }
    let mut wrapped_lines = Vec::new();
    let mut current_line: Vec<(Span<'static>, SpanKind)> = Vec::new();
    let mut current_width = 0usize;
    let continuation_width = max_width
        .saturating_sub(continuation_indent_width)
        .max(1 /* prevent zero width */);
    let mut line_limit = max_width;
    // Break incoming spans into owned (text, style) parts
    let mut parts: Vec<(String, Style, SpanKind)> = spans
        .iter()
        .map(|(s, kind)| (s.content.to_string(), s.style, kind.clone()))
        .collect();
    for (mut text, style, kind) in parts.drain(..) {
        while !text.is_empty() {
            let mut chars_to_fit = 0usize;
            let mut width_so_far = 0usize;
            let mut last_break_pos: Option<(usize, usize)> = None;
            for (grapheme_start, grapheme) in text.grapheme_indices(true) {
                let grapheme_end = grapheme_start + grapheme.len();
                let gw = UnicodeWidthStr::width(grapheme);
                if current_width + width_so_far + gw <= line_limit {
                    width_so_far += gw;
                    chars_to_fit = grapheme_end;
                    if grapheme.chars().all(|c| c.is_whitespace()) {
                        last_break_pos = Some((grapheme_end, width_so_far));
                    }
                } else {
                    break;
                }
            }
            if chars_to_fit == 0 {
                // Nothing fits on this line
                if !current_line.is_empty() {
                    // Look back at the previous span to find natural word boundary.
                    // When styled text (code, emphasis) fills the line and the next span
                    // starts with whitespace/punctuation, we want to recognize that as
                    // a word boundary between the spans, not as content of the next span.
                    if let Some((_last_span, _)) = current_line.last() {
                        // Extract any leading punctuation to keep with previous line.
                        // Trim any whitespace. Never lose characters.
                        // Examples: ") today" → extract ")", trim " ", wrap "today"
                        //           "))) more" → extract ")))", trim " ", wrap "more"
                        //           " useful" → trim " ", wrap "useful"
                        //           " )" → trim " ", wrap ")" (standalone, don't backtrack)
                        let mut punct_start = None; // Where punctuation begins (None if not found)
                        let mut punct_end = 0;
                        let mut ws_end = 0;

                        for (idx, ch) in text.char_indices() {
                            if punct_start.is_none() {
                                if ch.is_whitespace() {
                                    ws_end = idx + ch.len_utf8();
                                } else if !ch.is_alphanumeric() && ch != '_' {
                                    // Found first punctuation character
                                    punct_start = Some(idx);
                                    punct_end = idx + ch.len_utf8();
                                    ws_end = punct_end;
                                } else {
                                    // Hit word character, stop
                                    break;
                                }
                            } else {
                                // We already found punctuation, continue scanning
                                if ch.is_whitespace() {
                                    ws_end = idx + ch.len_utf8();
                                } else if !ch.is_alphanumeric() && ch != '_' {
                                    // Found additional punctuation character
                                    punct_end = idx + ch.len_utf8();
                                    ws_end = punct_end;
                                } else {
                                    // Hit word character, stop
                                    break;
                                }
                            }
                        }

                        // Check if punctuation fits on current line
                        if punct_end > 0 {
                            let punct_width = UnicodeWidthStr::width(&text[..punct_end]);
                            if current_width + punct_width <= line_limit {
                                // Punctuation fits, add it to current line and trim trailing space
                                let punct_text = text[..punct_end].to_string();
                                current_line.push((Span::styled(punct_text, style), kind.clone()));
                                // Note: current_width update not needed - will be reset when wrapping
                                text = text[ws_end..].to_string();

                                if text.is_empty() {
                                    // After processing boundary characters, nothing remains
                                    wrapped_lines.push(std::mem::take(&mut current_line));
                                    current_width = 0;
                                    line_limit = continuation_width;
                                    continue;
                                }
                            } else if punct_start == Some(0) {
                                // Punctuation doesn't fit AND it's directly adjacent (no leading space).
                                // Before backtracking, check if we'd make progress:
                                // If styled span + punctuation won't fit on a new line, don't backtrack.
                                let punct_width = UnicodeWidthStr::width(&text[..punct_end]);
                                let last_span_width = current_line
                                    .last()
                                    .map(|(span, _)| UnicodeWidthStr::width(span.content.as_ref()))
                                    .unwrap_or(0);

                                // Only backtrack if styled word + punctuation will fit on continuation line
                                if last_span_width + punct_width <= continuation_width {
                                    // Backtrack: pop styled span, wrap current line, then add styled span
                                    // to next line followed by current text. This preserves the styling.
                                    if let Some((last_span, last_kind)) = current_line.pop() {
                                        // Wrap the current line (without the styled span)
                                        wrapped_lines.push(std::mem::take(&mut current_line));

                                        // Start new line with the styled span (preserving its style/kind)
                                        current_line.push((last_span, last_kind));
                                        current_width = UnicodeWidthStr::width(
                                            current_line[0].0.content.as_ref(),
                                        );
                                        line_limit = continuation_width;

                                        // Restart the while loop to reprocess current text on the new line
                                        continue;
                                    }
                                }
                                // else: backtracking won't help, let normal wrapping handle it
                            } else if punct_start.is_some() {
                                // Punctuation doesn't fit but "stands alone" (has leading space).
                                // Trim the leading space and let punctuation wrap normally.
                                text = text.trim_start().to_string();
                            }
                        } else if ws_end > 0 {
                            // Just whitespace, trim it
                            text = text[ws_end..].to_string();

                            if text.is_empty() {
                                // After trimming whitespace, nothing remains
                                wrapped_lines.push(std::mem::take(&mut current_line));
                                current_width = 0;
                                line_limit = continuation_width;
                                continue;
                            }
                        }
                    }

                    wrapped_lines.push(std::mem::take(&mut current_line));
                    current_width = 0;
                    line_limit = continuation_width;
                    continue;
                } else {
                    // Consider unbreakable word
                    let next_word_end = text.find(char::is_whitespace).unwrap_or(text.len());
                    let next_word = &text[..next_word_end];
                    let ww = UnicodeWidthStr::width(next_word);
                    if ww <= MAX_UNBREAKABLE_LENGTH {
                        current_line
                            .push((Span::styled(next_word.to_string(), style), kind.clone()));
                        current_width += ww;
                        if next_word_end < text.len() {
                            text = text[next_word_end..].to_string();
                        } else {
                            break;
                        }
                    } else {
                        // Hard break the very long token
                        let mut forced_width = 0usize;
                        let mut forced_end = 0usize;
                        for (grapheme_start, grapheme) in text.grapheme_indices(true) {
                            let grapheme_end = grapheme_start + grapheme.len();
                            let gw = UnicodeWidthStr::width(grapheme);
                            if forced_width + gw > line_limit {
                                if forced_end == 0 {
                                    forced_end = grapheme_end;
                                    forced_width += gw;
                                }
                                break;
                            }
                            forced_width += gw;
                            forced_end = grapheme_end;
                        }
                        if forced_end == 0 {
                            forced_end = text.len();
                            forced_width = UnicodeWidthStr::width(text.as_str());
                        }
                        let chunk = text[..forced_end].to_string();
                        current_line.push((Span::styled(chunk, style), kind.clone()));
                        current_width = forced_width;
                        text = text[forced_end..].to_string();
                        if !text.is_empty() {
                            wrapped_lines.push(std::mem::take(&mut current_line));
                            current_width = 0;
                            line_limit = continuation_width;
                        }
                    }
                }
            } else if chars_to_fit >= text.len() {
                current_line.push((Span::styled(text.clone(), style), kind.clone()));
                current_width += width_so_far;
                break;
            } else {
                let (break_pos, _bw) = last_break_pos.unwrap_or((chars_to_fit, width_so_far));
                if last_break_pos.is_none() && current_width > 0 {
                    // No natural break inside the incoming span; start it on the next line so
                    // multi-word links and long tokens stay intact.
                    // BUT FIRST: apply lookahead to find word boundaries between spans.
                    if let Some((_last_span, _)) = current_line.last() {
                        // Extract any leading punctuation to keep with previous line.
                        // Trim any whitespace. Never lose characters.
                        //           " )" → trim " ", wrap ")" (standalone, don't backtrack)
                        //           "))) more" → extract ")))", wrap "more"
                        let mut punct_start = None; // Where punctuation begins (None if not found)
                        let mut punct_end = 0;
                        let mut ws_end = 0;

                        for (idx, ch) in text.char_indices() {
                            if punct_start.is_none() {
                                if ch.is_whitespace() {
                                    ws_end = idx + ch.len_utf8();
                                } else if !ch.is_alphanumeric() && ch != '_' {
                                    // Found first punctuation character
                                    punct_start = Some(idx);
                                    punct_end = idx + ch.len_utf8();
                                    ws_end = punct_end;
                                } else {
                                    // Hit word character, stop
                                    break;
                                }
                            } else {
                                // We already found punctuation, continue scanning
                                if ch.is_whitespace() {
                                    ws_end = idx + ch.len_utf8();
                                } else if !ch.is_alphanumeric() && ch != '_' {
                                    // Found additional punctuation character
                                    punct_end = idx + ch.len_utf8();
                                    ws_end = punct_end;
                                } else {
                                    // Hit word character, stop
                                    break;
                                }
                            }
                        }

                        // Check if punctuation fits on current line
                        if punct_end > 0 {
                            let punct_width = UnicodeWidthStr::width(&text[..punct_end]);
                            if current_width + punct_width <= line_limit {
                                // Punctuation fits, add it to current line and trim trailing space
                                let punct_text = text[..punct_end].to_string();
                                current_line.push((Span::styled(punct_text, style), kind.clone()));
                                // Note: current_width update not needed - will be reset when wrapping
                                text = text[ws_end..].to_string();

                                if text.is_empty() {
                                    // After processing boundary characters, nothing remains
                                    wrapped_lines.push(std::mem::take(&mut current_line));
                                    current_width = 0;
                                    line_limit = continuation_width;
                                    continue;
                                }
                            } else if punct_start == Some(0) {
                                // Punctuation doesn't fit AND it's directly adjacent (no leading space).
                                // Before backtracking, check if we'd make progress:
                                // If styled span + punctuation won't fit on a new line, don't backtrack.
                                let punct_width = UnicodeWidthStr::width(&text[..punct_end]);
                                let last_span_width = current_line
                                    .last()
                                    .map(|(span, _)| UnicodeWidthStr::width(span.content.as_ref()))
                                    .unwrap_or(0);

                                // Only backtrack if styled word + punctuation will fit on continuation line
                                if last_span_width + punct_width <= continuation_width {
                                    // Backtrack: pop styled span, wrap current line, then add styled span
                                    // to next line followed by current text. This preserves the styling.
                                    if let Some((last_span, last_kind)) = current_line.pop() {
                                        // Wrap the current line (without the styled span)
                                        wrapped_lines.push(std::mem::take(&mut current_line));

                                        // Start new line with the styled span (preserving its style/kind)
                                        current_line.push((last_span, last_kind));
                                        current_width = UnicodeWidthStr::width(
                                            current_line[0].0.content.as_ref(),
                                        );
                                        line_limit = continuation_width;

                                        // Restart the while loop to reprocess current text on the new line
                                        continue;
                                    }
                                }
                                // else: backtracking won't help, let normal wrapping handle it
                            } else if punct_start.is_some() {
                                // Punctuation doesn't fit but "stands alone" (has leading space).
                                // Trim the leading space and let punctuation wrap normally.
                                text = text.trim_start().to_string();
                            }
                        } else if ws_end > 0 {
                            // Just whitespace, trim it
                            text = text[ws_end..].to_string();

                            if text.is_empty() {
                                // After trimming whitespace, nothing remains
                                wrapped_lines.push(std::mem::take(&mut current_line));
                                current_width = 0;
                                line_limit = continuation_width;
                                continue;
                            }
                        }
                    }

                    wrapped_lines.push(std::mem::take(&mut current_line));
                    current_width = 0;
                    line_limit = continuation_width;
                    continue;
                }
                let left = text[..break_pos].trim_end();
                if !left.is_empty() {
                    let left_width = UnicodeWidthStr::width(left);
                    if current_width > 0 && current_width + left_width > line_limit {
                        wrapped_lines.push(std::mem::take(&mut current_line));
                        current_width = 0;
                        line_limit = continuation_width;
                    }
                    if left_width > 0 {
                        current_line.push((Span::styled(left.to_string(), style), kind.clone()));
                        current_width += left_width;
                    }
                }
                text = text[break_pos..].trim_start().to_string();
                if !text.is_empty() {
                    wrapped_lines.push(std::mem::take(&mut current_line));
                    current_width = 0;
                    line_limit = continuation_width;
                }
            }
        }
    }
    if !current_line.is_empty() {
        wrapped_lines.push(current_line);
    }
    if wrapped_lines.is_empty() {
        vec![Vec::new()]
    } else {
        wrapped_lines
    }
}

#[cfg(test)]
mod tests {
    use super::wrap_spans_to_width_generic_shared;
    use crate::ui::span::SpanKind;
    use ratatui::text::Span;

    #[test]
    fn wrap_splits_at_spaces() {
        let spans = vec![(Span::raw("word boundary test"), SpanKind::Text)];
        let wrapped = wrap_spans_to_width_generic_shared(&spans, 6, 0);
        let lines: Vec<String> = wrapped
            .into_iter()
            .map(|line| {
                line.iter()
                    .map(|(s, _)| s.content.as_ref())
                    .collect::<String>()
            })
            .filter(|s| !s.is_empty())
            .collect();
        assert_eq!(lines, vec!["word", "bounda", "ry", "test"]);
    }

    #[test]
    fn wrap_preserves_zwj_clusters() {
        let spans = vec![(Span::raw("👩‍💻👩‍💻"), SpanKind::Text)];
        let wrapped = wrap_spans_to_width_generic_shared(&spans, 2, 0);
        let lines: Vec<String> = wrapped
            .into_iter()
            .map(|line| {
                line.iter()
                    .map(|(s, _)| s.content.as_ref())
                    .collect::<String>()
            })
            .filter(|s| !s.is_empty())
            .collect();
        assert_eq!(lines, vec!["👩‍💻", "👩‍💻"]);
    }

    #[test]
    fn wrap_preserves_skin_tone_modifiers() {
        let spans = vec![(Span::raw("👍🏽👍🏽"), SpanKind::Text)];
        let wrapped = wrap_spans_to_width_generic_shared(&spans, 2, 0);
        let lines: Vec<String> = wrapped
            .into_iter()
            .map(|line| {
                line.iter()
                    .map(|(s, _)| s.content.as_ref())
                    .collect::<String>()
            })
            .filter(|s| !s.is_empty())
            .collect();
        assert_eq!(lines, vec!["👍🏽", "👍🏽"]);
    }
}