koda-cli 0.2.10

A high-performance AI coding agent for macOS and Linux
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
430
431
432
433
434
435
436
437
//! Mouse text selection for fullscreen TUI.
//!
//! When mouse capture is enabled (for scroll wheel), native terminal
//! text selection is unavailable. This module implements click-drag
//! selection in the history panel with automatic clipboard copy.
//!
//! Design: line-granularity selection with character-level endpoints.
//! The user clicks and drags in the history area; selected text is
//! highlighted with inverted colors. On mouse release, the selection
//! is copied to clipboard automatically.

use ratatui::{
    style::{Color, Modifier, Style},
    text::{Line, Span},
};

/// A position in the history panel.
///
/// `row` is in **buffer space** (absolute visual row index across the
/// entire scroll buffer, accounting for line wrapping). This makes
/// selections stable across scroll operations — the anchor doesn't
/// become stale when the viewport moves.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct VisualPos {
    /// Absolute visual row in the scroll buffer (0 = first row).
    pub row: u16,
    /// Column (0-based).
    pub col: u16,
}

/// Active text selection state.
#[derive(Debug, Clone)]
pub(crate) struct Selection {
    /// Where the drag started (anchor).
    pub anchor: VisualPos,
    /// Current drag position (cursor).
    pub cursor: VisualPos,
    /// The scroll-from-top offset captured at MouseDown time.
    ///
    /// Used to convert screen rows → buffer rows consistently across
    /// the entire drag (immune to buffer growth during inference).
    pub scroll_from_top: u16,
}

impl Selection {
    /// Return (start, end) normalized so start ≤ end.
    pub fn ordered(&self) -> (VisualPos, VisualPos) {
        if self.anchor.row < self.cursor.row
            || (self.anchor.row == self.cursor.row && self.anchor.col <= self.cursor.col)
        {
            (self.anchor, self.cursor)
        } else {
            (self.cursor, self.anchor)
        }
    }

    /// Check if a visual row is within the selection range.
    #[allow(dead_code)] // used in render path, will be wired for per-row highlighting
    pub fn contains_row(&self, row: u16) -> bool {
        let (start, end) = self.ordered();
        row >= start.row && row <= end.row
    }
}

/// Build ALL visual rows from logical lines, accounting for line wrapping.
///
/// Returns one String per visual row across the entire scroll buffer.
/// Also returns a parallel vec of gutter widths per visual row.
/// Used for buffer-space text extraction during copy operations.
pub(crate) fn build_all_visual_rows(
    lines: &[Line<'_>],
    gutter_widths: &[u16],
    viewport_width: usize,
) -> (Vec<String>, Vec<u16>) {
    let mut visual_rows: Vec<String> = Vec::new();
    let mut visual_gutters: Vec<u16> = Vec::new();
    let w = viewport_width.max(1);

    for (i, line) in lines.iter().enumerate() {
        let gw = gutter_widths.get(i).copied().unwrap_or(0);
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        if text.is_empty() {
            visual_rows.push(String::new());
            visual_gutters.push(gw);
        } else {
            let chars: Vec<char> = text.chars().collect();
            for (j, chunk) in chars.chunks(w).enumerate() {
                visual_rows.push(chunk.iter().collect());
                // Only the first visual row of a wrapped line has the gutter
                visual_gutters.push(if j == 0 { gw } else { 0 });
            }
        }
    }

    (visual_rows, visual_gutters)
}

/// Extract the plain text content visible in the history area.
///
/// Returns one String per visual row, accounting for line wrapping.
/// The returned vec has exactly `viewport_height` entries (or fewer
/// if the buffer is shorter than the viewport).
#[cfg(test)]
fn extract_visible_text(
    lines: &[Line<'_>],
    scroll_from_top: u16,
    viewport_width: usize,
    viewport_height: usize,
) -> Vec<String> {
    let mut visual_rows: Vec<String> = Vec::new();

    // Build all visual rows from logical lines
    for line in lines {
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        if text.is_empty() {
            visual_rows.push(String::new());
        } else {
            // Wrap at viewport_width
            let chars: Vec<char> = text.chars().collect();
            for chunk in chars.chunks(viewport_width.max(1)) {
                visual_rows.push(chunk.iter().collect());
            }
        }
    }

    // Slice to the visible window
    let start = scroll_from_top as usize;
    let end = (start + viewport_height).min(visual_rows.len());
    if start < visual_rows.len() {
        visual_rows[start..end].to_vec()
    } else {
        Vec::new()
    }
}

/// Extract the selected text from visual rows, skipping gutter columns.
///
/// `rows` and `gutters` must be parallel (from `build_all_visual_rows`).
/// For each row with a non-zero gutter width, that many leading columns
/// are excluded from the extracted text (NoSelect behavior).
pub(crate) fn extract_selected_text(
    rows: &[String],
    gutters: &[u16],
    selection: &Selection,
) -> String {
    let (start, end) = selection.ordered();
    let mut result = String::new();

    for row in start.row..=end.row {
        let idx = row as usize;
        if idx >= rows.len() {
            break;
        }
        let line = &rows[idx];
        let gutter_w = gutters.get(idx).copied().unwrap_or(0) as usize;
        let chars: Vec<char> = line.chars().collect();

        let col_start = if row == start.row {
            start.col as usize
        } else {
            0
        };
        let col_end = if row == end.row {
            (end.col as usize + 1).min(chars.len())
        } else {
            chars.len()
        };

        // Skip gutter columns (NoSelect)
        let effective_start = col_start.max(gutter_w);

        if effective_start < chars.len() && effective_start < col_end {
            let selected: String = chars[effective_start..col_end.min(chars.len())]
                .iter()
                .collect();
            result.push_str(&selected);
        }
        if row < end.row {
            result.push('\n');
        }
    }

    result
}

/// Apply selection highlighting to lines being rendered.
///
/// Returns modified lines with inverted styles on selected regions.
/// Selection coordinates are in **buffer space** (absolute visual rows).
pub(crate) fn apply_selection_highlight<'a>(
    lines: Vec<Line<'a>>,
    selection: &Selection,
    _scroll_from_top: u16,
    viewport_width: usize,
    _history_y: u16,
) -> Vec<Line<'a>> {
    let (sel_start, sel_end) = selection.ordered();
    let highlight = Style::default()
        .bg(Color::Rgb(68, 68, 120))
        .fg(Color::White)
        .add_modifier(Modifier::BOLD);

    let mut visual_row: u16 = 0;
    let mut result = Vec::with_capacity(lines.len());

    for line in lines {
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        let rows_this_line = if text.is_empty() {
            1
        } else {
            text.chars().count().div_ceil(viewport_width.max(1))
        } as u16;

        // Selection is in buffer space — compare directly against visual_row
        let line_end = visual_row + rows_this_line - 1;
        let in_selection = line_end >= sel_start.row && visual_row <= sel_end.row;

        if in_selection {
            let highlighted_spans: Vec<Span<'a>> = line
                .spans
                .into_iter()
                .map(|s| Span::styled(s.content, highlight))
                .collect();
            result.push(Line::from(highlighted_spans));
        } else {
            result.push(line);
        }

        visual_row += rows_this_line;
    }

    result
}

/// Copy text to the system clipboard, returning a short status phrase.
///
/// Delegates to [`crate::clipboard`] which auto-selects arboard (local),
/// OSC 52 (SSH), or OSC 52 + tmux passthrough depending on the environment.
pub(crate) fn copy_to_clipboard(text: &str) -> Result<String, String> {
    crate::clipboard::copy_to_clipboard(text)
}

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

    fn make_line(text: &str) -> Line<'static> {
        Line::from(text.to_string())
    }

    #[test]
    fn test_selection_ordered() {
        let sel = Selection {
            anchor: VisualPos { row: 5, col: 10 },
            cursor: VisualPos { row: 2, col: 3 },
            scroll_from_top: 0,
        };
        let (start, end) = sel.ordered();
        assert_eq!(start.row, 2);
        assert_eq!(end.row, 5);
    }

    #[test]
    fn test_selection_contains_row() {
        let sel = Selection {
            anchor: VisualPos { row: 2, col: 0 },
            cursor: VisualPos { row: 5, col: 10 },
            scroll_from_top: 0,
        };
        assert!(!sel.contains_row(1));
        assert!(sel.contains_row(2));
        assert!(sel.contains_row(3));
        assert!(sel.contains_row(5));
        assert!(!sel.contains_row(6));
    }

    #[test]
    fn test_extract_visible_text_basic() {
        let lines = vec![
            make_line("line one"),
            make_line("line two"),
            make_line("line three"),
        ];
        let visible = extract_visible_text(&lines, 0, 80, 10);
        assert_eq!(visible.len(), 3);
        assert_eq!(visible[0], "line one");
        assert_eq!(visible[2], "line three");
    }

    #[test]
    fn test_extract_visible_text_with_scroll() {
        let lines = vec![
            make_line("line one"),
            make_line("line two"),
            make_line("line three"),
        ];
        let visible = extract_visible_text(&lines, 1, 80, 10);
        assert_eq!(visible.len(), 2);
        assert_eq!(visible[0], "line two");
    }

    #[test]
    fn test_extract_visible_text_with_wrapping() {
        // A line that wraps at width 10
        let lines = vec![make_line("abcdefghij12345")];
        let visible = extract_visible_text(&lines, 0, 10, 10);
        assert_eq!(visible.len(), 2);
        assert_eq!(visible[0], "abcdefghij");
        assert_eq!(visible[1], "12345");
    }

    fn no_gutters(n: usize) -> Vec<u16> {
        vec![0; n]
    }

    #[test]
    fn test_extract_selected_text_single_line() {
        let rows = vec!["hello world".to_string()];
        let sel = Selection {
            anchor: VisualPos { row: 0, col: 6 },
            cursor: VisualPos { row: 0, col: 10 },
            scroll_from_top: 0,
        };
        let text = extract_selected_text(&rows, &no_gutters(1), &sel);
        assert_eq!(text, "world");
    }

    #[test]
    fn test_extract_selected_text_multi_line() {
        let rows = vec![
            "first line".to_string(),
            "second line".to_string(),
            "third line".to_string(),
        ];
        let sel = Selection {
            anchor: VisualPos { row: 0, col: 6 },
            cursor: VisualPos { row: 2, col: 4 },
            scroll_from_top: 0,
        };
        let text = extract_selected_text(&rows, &no_gutters(3), &sel);
        assert_eq!(text, "line\nsecond line\nthird");
    }

    #[test]
    fn test_copy_to_clipboard_format() {
        let rows = vec!["hello".to_string(), "world".to_string()];
        let sel = Selection {
            anchor: VisualPos { row: 0, col: 0 },
            cursor: VisualPos { row: 1, col: 4 },
            scroll_from_top: 0,
        };
        let text = extract_selected_text(&rows, &no_gutters(2), &sel);
        assert_eq!(text, "hello\nworld");
    }

    #[test]
    fn test_build_all_visual_rows_basic() {
        let lines = vec![
            make_line("line one"),
            make_line("line two"),
            make_line("line three"),
        ];
        let (rows, _) = build_all_visual_rows(&lines, &no_gutters(3), 80);
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0], "line one");
        assert_eq!(rows[2], "line three");
    }

    #[test]
    fn test_build_all_visual_rows_with_wrapping() {
        let lines = vec![make_line("abcdefghij12345")];
        let (rows, _) = build_all_visual_rows(&lines, &no_gutters(1), 10);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0], "abcdefghij");
        assert_eq!(rows[1], "12345");
    }

    #[test]
    fn test_build_all_visual_rows_empty_lines() {
        let lines = vec![make_line("hello"), make_line(""), make_line("world")];
        let (rows, _) = build_all_visual_rows(&lines, &no_gutters(3), 80);
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0], "hello");
        assert_eq!(rows[1], "");
        assert_eq!(rows[2], "world");
    }

    /// Simulates a cross-page selection: anchor at buffer row 2, cursor at
    /// buffer row 8, with a viewport that only shows 5 rows at a time.
    /// The selection should capture all rows 2–8 regardless of viewport.
    #[test]
    fn test_cross_page_selection() {
        let lines: Vec<Line<'_>> = (0..20).map(|i| make_line(&format!("line {i}"))).collect();
        let (all_rows, all_gutters) = build_all_visual_rows(&lines, &no_gutters(20), 80);
        assert_eq!(all_rows.len(), 20);

        let sel = Selection {
            anchor: VisualPos { row: 2, col: 0 },
            cursor: VisualPos { row: 8, col: 5 },
            scroll_from_top: 0,
        };
        let text = extract_selected_text(&all_rows, &all_gutters, &sel);
        assert!(text.contains("line 2"));
        assert!(text.contains("line 5"));
        assert!(text.contains("line 8"));
        assert!(!text.contains("line 1\n"));
        assert!(!text.contains("line 9"));
        assert_eq!(text.lines().count(), 7);
    }

    /// Test NoSelect: gutter columns are skipped during text extraction.
    #[test]
    fn test_noselect_gutter_skipped() {
        // Simulate diff lines with 7-char gutter: "{:>4} {} "
        // e.g. "   1   fn main() {" (context, sigil=' ')
        let rows = vec![
            "   1   fn main() {".to_string(),
            "   2 - println!(\"hello\");".to_string(),
            "   2 + println!(\"world\");".to_string(),
            "   3   }".to_string(),
        ];
        let gutters = vec![7u16, 7, 7, 7];
        let sel = Selection {
            anchor: VisualPos { row: 0, col: 0 },
            cursor: VisualPos { row: 3, col: 30 },
            scroll_from_top: 0,
        };
        let text = extract_selected_text(&rows, &gutters, &sel);
        // Should NOT contain line numbers or +/- markers
        assert!(!text.contains("   1"), "should skip gutter: {text}");
        assert!(!text.contains(" - "), "should skip sigil: {text}");
        assert!(!text.contains(" + "), "should skip sigil: {text}");
        // Should contain the code content
        assert!(text.contains("fn main()"));
        assert!(text.contains("println"));
    }
}