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, Wrap},
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 = if area.width >= 80 { 4 } else { 2 };
60        let inner_x = area.x + pad;
61        let inner_width = area.width.saturating_sub(pad as u16 + 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        let body_inner_w = inner_width.saturating_sub(4);
88        for raw in body_lines {
89            let text = if raw.len() > body_inner_w {
90                format!("{}…", &raw[..body_inner_w.saturating_sub(1)])
91            } else {
92                raw.trim_end().to_string()
93            };
94            lines.push(Line::from(Span::styled(
95                format!("    {text}"),
96                Style::default()
97                    .fg(theme::MUTED())
98                    .add_modifier(Modifier::ITALIC),
99            )));
100        }
101
102        Paragraph::new(lines)
103            .wrap(Wrap { trim: false })
104            .render(inner_area, buf);
105    }
106}