Skip to main content

stynx_code_tui/widgets/
thinking_panel.rs

1use ratatui::{
2    buffer::Buffer,
3    layout::Rect,
4    style::{Modifier, Style},
5    text::{Line, Span},
6    widgets::{Paragraph, Widget},
7};
8
9use crate::theme;
10use crate::widgets::spinner::FRAMES;
11
12pub struct ThinkingPanel<'a> {
13    pub text: &'a str,
14    pub spinner_frame: usize,
15}
16
17impl<'a> ThinkingPanel<'a> {
18    pub fn new(text: &'a str, spinner_frame: usize) -> Self {
19        Self { text, spinner_frame }
20    }
21}
22
23impl<'a> Widget for ThinkingPanel<'a> {
24    fn render(self, area: Rect, buf: &mut Buffer) {
25        if self.text.trim().is_empty() || area.height == 0 || area.width < 4 {
26            return;
27        }
28
29        let frame = FRAMES[self.spinner_frame % FRAMES.len()];
30
31        let visible_body = (area.height as usize).saturating_sub(1);
32        let body_lines: Vec<&str> = {
33            let mut v: Vec<&str> = self
34                .text
35                .lines()
36                .filter(|l| !l.trim().is_empty())
37                .collect();
38            if v.len() > visible_body {
39                v = v[v.len() - visible_body..].to_vec();
40            }
41            v
42        };
43
44        for y in area.y..area.y + area.height {
45            for x in area.x..area.x + area.width {
46                buf[(x, y)].set_style(Style::default().bg(theme::BACKGROUND()));
47            }
48        }
49
50        let accent = theme::IRIS();
51        let bar_col = theme::OVERLAY();
52
53        for y in area.y..area.y + area.height {
54            buf[(area.x, y)]
55                .set_char('▌')
56                .set_style(Style::default().fg(accent).bg(theme::BACKGROUND()));
57        }
58
59        let pad: u16 = 1;
60        let inner_x = area.x + pad;
61        let inner_width = area.width.saturating_sub(pad + 1) as usize;
62        let _ = bar_col;
63
64        let header = Line::from(vec![
65            Span::styled(
66                format!("  {frame} "),
67                Style::default()
68                    .fg(accent)
69                    .add_modifier(Modifier::BOLD),
70            ),
71            Span::styled(
72                "thinking",
73                Style::default()
74                    .fg(theme::SUBTLE())
75                    .add_modifier(Modifier::ITALIC),
76            ),
77        ]);
78
79        let inner_area = Rect {
80            x: inner_x,
81            y: area.y,
82            width: inner_width as u16,
83            height: area.height,
84        };
85
86        let mut lines: Vec<Line<'static>> = vec![header];
87        for raw in body_lines {
88            lines.extend(wrap_thinking_line(raw.trim_end(), inner_width));
89        }
90
91        Paragraph::new(lines).render(inner_area, buf);
92    }
93}
94
95/// Word-wrap a thinking body line to `width` columns with a hanging indent, so
96/// continuation lines align under the first line's text instead of overflowing
97/// to the left edge.
98fn wrap_thinking_line(raw: &str, width: usize) -> Vec<Line<'static>> {
99    let width = width.max(1);
100    let trimmed = raw.trim_start();
101    let indent_w = raw.len() - trimmed.len();
102    let base_indent = 4 + indent_w;
103
104    let (prefix, body, prefix_style) = if let Some(rest) = trimmed.strip_prefix("### ") {
105        ("### ".to_string(), rest, Style::default().fg(theme::SUBTLE()).add_modifier(Modifier::BOLD))
106    } else if let Some(rest) = trimmed.strip_prefix("## ") {
107        ("## ".to_string(), rest, Style::default().fg(theme::SUBTLE()).add_modifier(Modifier::BOLD))
108    } else if let Some(rest) = trimmed.strip_prefix("# ") {
109        ("# ".to_string(), rest, Style::default().fg(theme::SUBTLE()).add_modifier(Modifier::BOLD))
110    } else if let Some(rest) = trimmed.strip_prefix("- ").or_else(|| trimmed.strip_prefix("* ")) {
111        ("• ".to_string(), rest, Style::default().fg(theme::SUBTLE()))
112    } else if let Some((num, rest)) = split_numbered(trimmed) {
113        (format!("{num}. "), rest, Style::default().fg(theme::SUBTLE()))
114    } else {
115        (String::new(), trimmed, Style::default())
116    };
117
118    // Continuation lines indent to the first line's text column (hanging indent).
119    let hang = base_indent + prefix.chars().count();
120    let avail = width.saturating_sub(hang).max(1);
121
122    // Tokenize the styled body into words, collapsing runs of whitespace.
123    let mut words: Vec<(String, Style)> = Vec::new();
124    for span in parse_thinking_inline(body) {
125        let style = span.style;
126        for w in span.content.split(' ') {
127            if !w.is_empty() {
128                words.push((w.to_string(), style));
129            }
130        }
131    }
132
133    let new_line = |first: bool| -> Vec<Span<'static>> {
134        if first {
135            let mut v = vec![Span::styled(" ".repeat(base_indent), Style::default())];
136            if !prefix.is_empty() {
137                v.push(Span::styled(prefix.clone(), prefix_style));
138            }
139            v
140        } else {
141            vec![Span::styled(" ".repeat(hang), Style::default())]
142        }
143    };
144
145    let mut out: Vec<Line<'static>> = Vec::new();
146    let mut spans = new_line(true);
147    let mut text_w = 0usize; // columns of wrappable text on the current line
148
149    for (word, style) in words {
150        let wlen = word.chars().count();
151        let need = if text_w == 0 { wlen } else { wlen + 1 };
152        if text_w > 0 && text_w + need > avail {
153            out.push(Line::from(std::mem::take(&mut spans)));
154            spans = new_line(false);
155            text_w = 0;
156        }
157        if text_w > 0 {
158            spans.push(Span::styled(" ".to_string(), Style::default()));
159            text_w += 1;
160        }
161        spans.push(Span::styled(word, style));
162        text_w += wlen;
163    }
164    out.push(Line::from(spans));
165    out
166}
167
168fn split_numbered(s: &str) -> Option<(String, &str)> {
169    let bytes = s.as_bytes();
170    let mut i = 0;
171    while i < bytes.len() && bytes[i].is_ascii_digit() { i += 1; }
172    if i == 0 || i >= bytes.len() { return None; }
173    if bytes[i] == b'.' && i + 1 < bytes.len() && bytes[i + 1] == b' ' {
174        let num = s[..i].to_string();
175        let rest = &s[i + 2..];
176        Some((num, rest))
177    } else {
178        None
179    }
180}
181
182fn parse_thinking_inline(text: &str) -> Vec<Span<'static>> {
183    let mut spans: Vec<Span<'static>> = Vec::new();
184    let mut buf = String::new();
185    let chars: Vec<char> = text.chars().collect();
186    let mut i = 0;
187    let base = Style::default().fg(theme::MUTED()).add_modifier(Modifier::ITALIC);
188    let bold = Style::default().fg(theme::SUBTLE()).add_modifier(Modifier::BOLD);
189    let code = Style::default().fg(theme::FOAM()).add_modifier(Modifier::ITALIC);
190
191    let flush = |buf: &mut String, spans: &mut Vec<Span<'static>>| {
192        if !buf.is_empty() {
193            spans.push(Span::styled(std::mem::take(buf), base));
194        }
195    };
196
197    while i < chars.len() {
198        if i + 1 < chars.len() && chars[i] == '*' && chars[i + 1] == '*' {
199            flush(&mut buf, &mut spans);
200            i += 2;
201            let mut inner = String::new();
202            while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '*') {
203                inner.push(chars[i]);
204                i += 1;
205            }
206            spans.push(Span::styled(inner, bold));
207            i += 2;
208        } else if chars[i] == '`' {
209            flush(&mut buf, &mut spans);
210            i += 1;
211            let mut inner = String::new();
212            while i < chars.len() && chars[i] != '`' {
213                inner.push(chars[i]);
214                i += 1;
215            }
216            spans.push(Span::styled(inner, code));
217            if i < chars.len() { i += 1; }
218        } else {
219            buf.push(chars[i]);
220            i += 1;
221        }
222    }
223    flush(&mut buf, &mut spans);
224    spans
225}