ratkit 0.2.15

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
//! Edit tool (←) display component.
//!
//! This module provides rendering for file editing tools with diff view
//! (split vs unified based on width).

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

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

use super::inline_tool::ToolStatus;
use super::tool_write::{Diagnostic, DiagnosticSeverity};

/// Edit mode for displaying diffs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiffMode {
    /// Unified diff view (side by side)
    Unified,
    /// Split diff view (old | new)
    Split,
}

/// Edit tool display for file modifications.
///
/// Renders:
/// - File path
/// - Diff view (unified or split based on width)
/// - Line numbers
/// - Diff highlighting (additions/deletions)
pub struct ToolEdit<'a> {
    /// Path to the file being edited
    file_path: &'a str,
    /// Original content being replaced
    old_content: &'a str,
    /// New content replacing the old
    new_content: &'a str,
    /// Diagnostic messages
    diagnostics: Vec<Diagnostic>,
    /// Status of the operation
    status: ToolStatus,
    /// Whether output is expanded
    expanded: bool,
    /// Current diff mode
    diff_mode: DiffMode,
}

impl<'a> ToolEdit<'a> {
    /// Create a new ToolEdit.
    pub fn new(file_path: &'a str, old_content: &'a str, new_content: &'a str) -> Self {
        Self {
            file_path,
            old_content,
            new_content,
            diagnostics: Vec::new(),
            status: ToolStatus::Pending,
            expanded: false,
            diff_mode: DiffMode::Unified,
        }
    }

    /// Add a diagnostic message.
    pub fn add_diagnostic(mut self, diagnostic: Diagnostic) -> Self {
        self.diagnostics.push(diagnostic);
        self
    }

    /// Set the status.
    pub fn status(mut self, status: ToolStatus) -> Self {
        self.status = status;
        self
    }

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

    /// Set diff mode.
    pub fn diff_mode(mut self, mode: DiffMode) -> Self {
        self.diff_mode = mode;
        self
    }

    /// Determine diff mode based on available width.
    pub fn auto_diff_mode(&mut self, width: u16) {
        self.diff_mode = if width >= 80 {
            DiffMode::Split
        } else {
            DiffMode::Unified
        };
    }

    /// Get border color based on status.
    fn border_color(&self, colors: &ChatColors) -> Color {
        match self.status {
            ToolStatus::Pending => colors.warning,
            ToolStatus::Complete => colors.success,
            ToolStatus::Error => colors.error,
            ToolStatus::PermissionPending => Color::Rgb(255, 165, 0),
        }
    }

    /// Compute simple line-by-line diff.
    fn compute_diff(&self) -> Vec<DiffLine> {
        let old_lines: Vec<&str> = self.old_content.lines().collect();
        let new_lines: Vec<&str> = self.new_content.lines().collect();
        let mut diff = Vec::new();

        // Simple LCS-based diff algorithm
        let old_len = old_lines.len();
        let new_len = new_lines.len();

        // Very simple diff: find matching lines
        let mut old_idx = 0;
        let mut new_idx = 0;

        while old_idx < old_len || new_idx < new_len {
            if old_idx >= old_len {
                // Remaining new lines are additions
                diff.push(DiffLine {
                    line_num_old: None,
                    line_num_new: Some(new_idx + 1),
                    content: new_lines[new_idx].to_string(),
                    diff_type: DiffLineType::Addition,
                });
                new_idx += 1;
            } else if new_idx >= new_len {
                // Remaining old lines are deletions
                diff.push(DiffLine {
                    line_num_old: Some(old_idx + 1),
                    line_num_new: None,
                    content: old_lines[old_idx].to_string(),
                    diff_type: DiffLineType::Deletion,
                });
                old_idx += 1;
            } else if old_lines[old_idx] == new_lines[new_idx] {
                // Matching line
                diff.push(DiffLine {
                    line_num_old: Some(old_idx + 1),
                    line_num_new: Some(new_idx + 1),
                    content: old_lines[old_idx].to_string(),
                    diff_type: DiffLineType::Context,
                });
                old_idx += 1;
                new_idx += 1;
            } else {
                // Check if it's a simple replacement
                diff.push(DiffLine {
                    line_num_old: Some(old_idx + 1),
                    line_num_new: None,
                    content: old_lines[old_idx].to_string(),
                    diff_type: DiffLineType::Deletion,
                });
                diff.push(DiffLine {
                    line_num_old: None,
                    line_num_new: Some(new_idx + 1),
                    content: new_lines[new_idx].to_string(),
                    diff_type: DiffLineType::Addition,
                });
                old_idx += 1;
                new_idx += 1;
            }
        }

        diff
    }

    /// Render the edit tool to a buffer.
    pub fn render(&self, area: Rect, buf: &mut Buffer, colors: &ChatColors) {
        if area.height < 1 {
            return;
        }

        let max_y = area.y + area.height;
        let mut y = area.y;
        let border_color = self.border_color(colors);

        // Draw left border
        for y_pos in area.y..max_y {
            buf.get_mut(area.x, y_pos)
                .set_char('')
                .set_style(Style::default().fg(border_color));
        }

        // === Header ===
        let expand_icon = if self.expanded { "" } else { "" };
        let mode_icon = match self.diff_mode {
            DiffMode::Unified => "",
            DiffMode::Split => "⬚⬚",
        };
        let header = format!("← Edit {} {} [{}]", expand_icon, mode_icon, self.file_path);
        let header_span = Span::styled(
            header,
            Style::default()
                .fg(border_color)
                .add_modifier(Modifier::BOLD),
        );
        buf.set_span(area.x + 2, y, &header_span, area.width.saturating_sub(3));
        y += 1;

        // Separator
        if y < max_y {
            buf.set_span(
                area.x + 2,
                y,
                &Span::styled(
                    "".repeat(30),
                    Style::default()
                        .fg(border_color)
                        .add_modifier(Modifier::DIM),
                ),
                area.width.saturating_sub(3),
            );
            y += 1;
        }

        // === Diff Content ===
        let diff = self.compute_diff();
        let max_lines = if self.expanded {
            diff.len()
                .min(area.height.saturating_sub(y - area.y) as usize)
        } else {
            diff.len().min(15)
        };

        for (i, line) in diff.iter().take(max_lines).enumerate() {
            if y >= max_y {
                break;
            }

            let (prefix, style) = match line.diff_type {
                DiffLineType::Addition => ("+ ", colors.diff_added_style()),
                DiffLineType::Deletion => ("- ", colors.diff_removed_style()),
                DiffLineType::Context => ("  ", Style::default().fg(colors.text)),
            };

            // Line numbers
            let old_num = line
                .line_num_old
                .map(|n| format!("{:4}", n))
                .unwrap_or_else(|| "    ".to_string());
            let new_num = line
                .line_num_new
                .map(|n| format!("{:4}", n))
                .unwrap_or_else(|| "    ".to_string());

            let line_num_span = Span::styled(
                format!("{}{}", old_num, new_num),
                Style::default()
                    .fg(colors.text_muted)
                    .add_modifier(Modifier::DIM),
            );
            buf.set_span(area.x + 2, y, &line_num_span, 11);

            // Content
            let display_line = if line.content.len() > area.width as usize - 14 {
                format!("{}...", &line.content[..area.width as usize - 17])
            } else {
                line.content.clone()
            };

            buf.set_span(
                area.x + 13,
                y,
                &Span::styled(format!("{}{}", prefix, display_line), style),
                area.width.saturating_sub(14),
            );
            y += 1;
        }

        // Show more indicator
        if !self.expanded && diff.len() > 15 && y < max_y {
            buf.set_span(
                area.x + 2,
                y,
                &Span::styled(
                    format!("... {} more changes (click to expand)", diff.len() - 15),
                    Style::default()
                        .fg(colors.text_muted)
                        .add_modifier(Modifier::ITALIC),
                ),
                area.width.saturating_sub(3),
            );
            y += 1;
        }

        // === Stats ===
        if y < max_y {
            let additions = diff
                .iter()
                .filter(|l| l.diff_type == DiffLineType::Addition)
                .count();
            let deletions = diff
                .iter()
                .filter(|l| l.diff_type == DiffLineType::Deletion)
                .count();
            let stats = format!("{} additions, {} deletions", additions, deletions);
            let stats_span = Span::styled(stats, Style::default().fg(colors.text_muted));
            buf.set_span(area.x + 2, y, &stats_span, area.width.saturating_sub(3));
            y += 1;
        }

        // === Diagnostics ===
        for diag in &self.diagnostics {
            if y >= max_y {
                break;
            }

            let (symbol, diag_color) = match diag.severity {
                DiagnosticSeverity::Error => ("", colors.error),
                DiagnosticSeverity::Warning => ("", colors.warning),
                DiagnosticSeverity::Info => ("", colors.primary),
            };

            let diag_text = format!("{} {}", symbol, diag.message);
            let diag_span = Span::styled(diag_text, Style::default().fg(diag_color));
            buf.set_span(area.x + 2, y, &diag_span, area.width.saturating_sub(3));
            y += 1;
        }

        // === Status ===
        if y < max_y {
            let status_text = match self.status {
                ToolStatus::Pending => "⏳ Pending...",
                ToolStatus::Complete => "✓ Edited successfully",
                ToolStatus::Error => "✗ Edit failed",
                ToolStatus::PermissionPending => "⚠ Permission required",
            };
            let status_span = Span::styled(
                status_text,
                Style::default()
                    .fg(border_color)
                    .add_modifier(Modifier::BOLD),
            );
            buf.set_span(area.x + 2, y, &status_span, area.width.saturating_sub(3));
        }
    }

    /// Toggle expanded state.
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Cycle through diff modes.
    pub fn cycle_diff_mode(&mut self) {
        self.diff_mode = match self.diff_mode {
            DiffMode::Unified => DiffMode::Split,
            DiffMode::Split => DiffMode::Unified,
        };
    }
}

/// Represents a single line in a diff.
#[derive(Debug, Clone)]
pub struct DiffLine {
    /// Line number in old file (if applicable)
    line_num_old: Option<u32>,
    /// Line number in new file (if applicable)
    line_num_new: Option<u32>,
    /// Content of the line
    content: String,
    /// Type of diff line
    diff_type: DiffLineType,
}

/// Type of diff line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiffLineType {
    /// Line was added
    Addition,
    /// Line was deleted
    Deletion,
    /// Context (unchanged) line
    Context,
}

impl Widget for ToolEdit<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let colors = ChatColors::default();
        self.render(area, buf, &colors);
    }
}

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

    #[test]
    fn test_tool_edit_basic() {
        let edit = ToolEdit::new("src/main.rs", "old content", "new content");
        assert_eq!(edit.file_path, "src/main.rs");
    }

    #[test]
    fn test_tool_edit_diff() {
        let edit = ToolEdit::new("test.rs", "line 1\nline 2", "line 1\nmodified");
        let diff = edit.compute_diff();
        assert!(!diff.is_empty());
    }

    #[test]
    fn test_tool_edit_status() {
        let pending = ToolEdit::new("test.rs", "old", "new").status(ToolStatus::Pending);
        assert_eq!(pending.status, ToolStatus::Pending);
    }
}