ratkit 0.2.16

A comprehensive collection of reusable TUI components for ratatui including resizable splits, tree views, markdown rendering, toast notifications, dialogs, and terminal embedding
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
//! User message component for AI Chat widget.
//!
//! This module provides the [`UserMessage`] widget for rendering user messages
//! in the chat interface with support for attachments, timestamps, and queued state.

use std::time::SystemTime;

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::Style,
    text::{Line, Span},
    widgets::Widget,
};

use crate::widgets::ai_chat::components::theme::ChatColors;

/// File attachment associated with a message.
#[derive(Debug, Clone, PartialEq)]
pub struct Attachment {
    /// File path
    pub path: String,
    /// MIME type of the attachment
    pub mime_type: String,
}

impl Attachment {
    /// Create a new attachment.
    pub fn new(path: String, mime_type: String) -> Self {
        Self { path, mime_type }
    }

    /// Get the icon for this attachment based on MIME type.
    pub fn icon(&self) -> &'static str {
        if self.mime_type.starts_with("image/") {
            "🖼"
        } else if self.mime_type == "application/pdf" {
            "📄"
        } else if self.mime_type.starts_with("text/") || self.mime_type == "application/json" {
            "📝"
        } else if self.mime_type == "application/x-directory"
            || self.mime_type.contains("directory")
        {
            "📁"
        } else {
            "📎"
        }
    }

    /// Get a short label for the MIME type.
    pub fn label(&self) -> &'static str {
        if self.mime_type.starts_with("image/") {
            "img"
        } else if self.mime_type == "application/pdf" {
            "pdf"
        } else if self.mime_type.starts_with("text/") {
            "txt"
        } else if self.mime_type == "application/json" {
            "json"
        } else if self.mime_type.contains("directory") {
            "dir"
        } else {
            "file"
        }
    }
}

/// User message widget for displaying user-originated chat messages.
///
/// Renders messages with:
/// - Left border colored by agent
/// - Text content in theme colors
/// - File attachment badges
/// - Optional "QUEUED" badge for pending messages
/// - Optional timestamp
/// - Hover effect when focused
pub struct UserMessage<'a> {
    /// Message content
    content: &'a str,
    /// File attachments
    attachments: &'a [Attachment],
    /// Optional timestamp
    timestamp: Option<SystemTime>,
    /// Whether message is queued/pending
    queued: bool,
    /// Agent-specific border color
    agent_color: ratatui::style::Color,
    /// Whether this message is compacted
    compacted: bool,
    /// Whether the widget has focus (for hover effect)
    focused: bool,
}

impl<'a> UserMessage<'a> {
    /// Create a new user message widget.
    pub fn new(content: &'a str) -> Self {
        Self {
            content,
            attachments: &[],
            timestamp: None,
            queued: false,
            agent_color: ratatui::style::Color::Cyan,
            compacted: false,
            focused: false,
        }
    }

    /// Set attachments.
    pub fn attachments(mut self, attachments: &'a [Attachment]) -> Self {
        self.attachments = attachments;
        self
    }

    /// Set timestamp.
    pub fn timestamp(mut self, timestamp: SystemTime) -> Self {
        self.timestamp = Some(timestamp);
        self
    }

    /// Set queued state.
    pub fn queued(mut self, queued: bool) -> Self {
        self.queued = queued;
        self
    }

    /// Set agent color.
    pub fn agent_color(mut self, color: ratatui::style::Color) -> Self {
        self.agent_color = color;
        self
    }

    /// Set compacted state.
    pub fn compacted(mut self, compacted: bool) -> Self {
        self.compacted = compacted;
        self
    }

    /// Set focus state.
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Format timestamp as HH:MM.
    fn format_timestamp(&self) -> String {
        if let Some(ts) = self.timestamp {
            let duration = ts
                .duration_since(SystemTime::UNIX_EPOCH)
                .ok()
                .map(|d| d.as_secs())
                .unwrap_or(0);
            let hours = (duration / 3600) % 24;
            let minutes = (duration / 60) % 60;
            format!("{:02}:{:02}", hours, minutes)
        } else {
            String::new()
        }
    }

    /// Render the widget.
    fn render_widget(&self, area: Rect, buf: &mut Buffer, colors: &ChatColors) {
        let border_width = 1;
        let content_area = Rect {
            x: area.x + border_width,
            y: area.y,
            width: area.width.saturating_sub(border_width * 2),
            height: area.height,
        };

        // Draw left border with agent color
        for y in area.y..area.y + area.height {
            buf.get_mut(area.x, y)
                .set_style(Style::default().fg(self.agent_color));
        }

        // Determine background style based on focus
        let bg_color = if self.focused {
            colors.background_element
        } else {
            colors.background_panel
        };

        // Fill background
        for x in (area.x + 1)..(area.x + area.width) {
            for y in area.y..area.y + area.height {
                buf.get_mut(x, y).set_bg(bg_color);
            }
        }

        // Render content
        let mut y_offset = 0;
        let max_y = area.y + area.height;

        // Render text content
        let text_style = Style::default().fg(colors.text);
        for line in self.content.lines() {
            if area.y + y_offset >= max_y {
                break;
            }
            let span = Span::styled(line, text_style);
            buf.set_span(area.x + 2, area.y + y_offset, &span, content_area.width - 2);
            y_offset += 1;
        }

        // Render attachment badges if present
        if !self.attachments.is_empty() {
            if area.y + y_offset < max_y {
                let badge_parts: Vec<Span> = self
                    .attachments
                    .iter()
                    .map(|att| {
                        let icon = att.icon();
                        let label = att.label();
                        Span::styled(
                            format!(" {} {} ", icon, label),
                            Style::default()
                                .fg(colors.text)
                                .bg(colors.background_element)
                                .add_modifier(ratatui::style::Modifier::DIM),
                        )
                    })
                    .collect();

                // Render badges on the same line or wrap
                let mut x_pos = area.x + 2;
                for span in badge_parts {
                    let span_width = span.content.chars().count();
                    if x_pos + span_width as u16 > area.x + content_area.width {
                        y_offset += 1;
                        x_pos = area.x + 2;
                        if area.y + y_offset >= max_y {
                            break;
                        }
                    }
                    buf.set_span(x_pos, area.y + y_offset, &span, span_width as u16);
                    x_pos += span_width as u16 + 1;
                }
                y_offset += 1;
            }
        }

        // Render QUEUED badge
        if self.queued {
            if area.y + y_offset < max_y {
                let queued_badge = Span::styled(
                    " QUEUED ",
                    Style::default()
                        .fg(ratatui::style::Color::Black)
                        .bg(colors.warning)
                        .add_modifier(ratatui::style::Modifier::BOLD),
                );
                buf.set_span(area.x + 2, area.y + y_offset, &queued_badge, 7);
                y_offset += 1;
            }
        }

        // Render timestamp if present
        if let Some(_) = self.timestamp {
            let ts = self.format_timestamp();
            if !ts.is_empty() && area.y + y_offset < max_y {
                let ts_span = Span::styled(ts.as_str(), Style::default().fg(colors.text_muted));
                buf.set_span(area.x + 2, area.y + y_offset, &ts_span, ts.len() as u16);
            }
        }
    }
}

impl<'a> Widget for UserMessage<'a> {
    fn render(self, area: Rect, buf: &mut Buffer)
    where
        Self: Sized,
    {
        let colors = ChatColors::default();
        self.render_widget(area, buf, &colors);
    }
}

/// Builder for UserMessage with custom colors.
pub struct UserMessageRenderer<'a> {
    content: &'a str,
    attachments: &'a [Attachment],
    timestamp: Option<SystemTime>,
    queued: bool,
    agent_color: ratatui::style::Color,
    compacted: bool,
    focused: bool,
    colors: ChatColors,
}

impl<'a> UserMessageRenderer<'a> {
    /// Create a new renderer.
    pub fn new(content: &'a str) -> Self {
        Self {
            content,
            attachments: &[],
            timestamp: None,
            queued: false,
            agent_color: ratatui::style::Color::Cyan,
            compacted: false,
            focused: false,
            colors: ChatColors::default(),
        }
    }

    /// Set attachments.
    pub fn attachments(mut self, attachments: &'a [Attachment]) -> Self {
        self.attachments = attachments;
        self
    }

    /// Set timestamp.
    pub fn timestamp(mut self, timestamp: SystemTime) -> Self {
        self.timestamp = Some(timestamp);
        self
    }

    /// Set queued state.
    pub fn queued(mut self, queued: bool) -> Self {
        self.queued = queued;
        self
    }

    /// Set agent color.
    pub fn agent_color(mut self, color: ratatui::style::Color) -> Self {
        self.agent_color = color;
        self
    }

    /// Set compacted state.
    pub fn compacted(mut self, compacted: bool) -> Self {
        self.compacted = compacted;
        self
    }

    /// Set focus state.
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Set custom colors.
    pub fn colors(mut self, colors: ChatColors) -> Self {
        self.colors = colors;
        self
    }

    /// Render the message.
    pub fn render(self, area: Rect, buf: &mut Buffer) {
        let widget = UserMessage {
            content: self.content,
            attachments: self.attachments,
            timestamp: self.timestamp,
            queued: self.queued,
            agent_color: self.agent_color,
            compacted: self.compacted,
            focused: self.focused,
        };
        widget.render_widget(area, buf, &self.colors);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_attachment_icon() {
        let img = Attachment::new("test.png".to_string(), "image/png".to_string());
        assert_eq!(img.icon(), "🖼");

        let pdf = Attachment::new("doc.pdf".to_string(), "application/pdf".to_string());
        assert_eq!(pdf.icon(), "📄");

        let txt = Attachment::new("readme.txt".to_string(), "text/plain".to_string());
        assert_eq!(txt.icon(), "📝");

        let dir = Attachment::new("folder".to_string(), "application/x-directory".to_string());
        assert_eq!(dir.icon(), "📁");
    }

    #[test]
    fn test_attachment_label() {
        let img = Attachment::new("test.png".to_string(), "image/png".to_string());
        assert_eq!(img.label(), "img");

        let pdf = Attachment::new("doc.pdf".to_string(), "application/pdf".to_string());
        assert_eq!(pdf.label(), "pdf");

        let txt = Attachment::new("readme.txt".to_string(), "text/plain".to_string());
        assert_eq!(txt.label(), "txt");

        let json = Attachment::new("data.json".to_string(), "application/json".to_string());
        assert_eq!(json.label(), "json");

        let dir = Attachment::new("folder".to_string(), "application/x-directory".to_string());
        assert_eq!(dir.label(), "dir");
    }

    #[test]
    fn test_user_message_builder() {
        let msg = UserMessage::new("Hello, world!");
        assert_eq!(msg.content, "Hello, world!");
        assert!(msg.attachments.is_empty());
        assert!(!msg.queued);
        assert!(!msg.compacted);
    }

    #[test]
    fn test_user_message_with_options() {
        let attachments = vec![Attachment::new(
            "test.png".to_string(),
            "image/png".to_string(),
        )];
        let timestamp = SystemTime::now();

        let msg = UserMessage::new("Test message")
            .attachments(&attachments)
            .timestamp(timestamp)
            .queued(true)
            .agent_color(ratatui::style::Color::Magenta)
            .compacted(false)
            .focused(true);

        assert_eq!(msg.content, "Test message");
        assert_eq!(msg.attachments.len(), 1);
        assert!(msg.queued);
        assert!(msg.focused);
    }
}