claude-code-rust 0.3.0

A native Rust terminal interface for Claude Code
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
// Claude Code Rust - A native Rust terminal interface for Claude Code
// Copyright (C) 2025  Simon Peter Rothgang
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use super::App;

pub(crate) fn normalize_selection(
    a: super::SelectionPoint,
    b: super::SelectionPoint,
) -> (super::SelectionPoint, super::SelectionPoint) {
    if (a.row, a.col) <= (b.row, b.col) { (a, b) } else { (b, a) }
}

pub(super) fn clear_selection(app: &mut App) {
    app.selection = None;
    app.rendered_chat_lines.clear();
    app.rendered_input_lines.clear();
}

#[cfg(test)]
fn slice_by_cols(text: &str, start_col: usize, end_col: usize) -> String {
    let mut out = String::new();
    for (i, ch) in text.chars().enumerate() {
        if i >= end_col {
            break;
        }
        if i >= start_col {
            out.push(ch);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    // =====
    // TESTS: 42
    // =====

    use super::*;
    use crate::app::SelectionPoint;
    use pretty_assertions::assert_eq;

    // normalize_selection

    #[test]
    fn normalize_already_ordered() {
        let a = SelectionPoint { row: 0, col: 2 };
        let b = SelectionPoint { row: 1, col: 5 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start, a);
        assert_eq!(end, b);
    }

    #[test]
    fn normalize_reversed() {
        let a = SelectionPoint { row: 2, col: 3 };
        let b = SelectionPoint { row: 0, col: 1 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start, b);
        assert_eq!(end, a);
    }

    #[test]
    fn normalize_same_point() {
        let p = SelectionPoint { row: 5, col: 10 };
        let (start, end) = normalize_selection(p, p);
        assert_eq!(start, p);
        assert_eq!(end, p);
    }

    // normalize_selection

    #[test]
    fn normalize_same_row_different_cols() {
        let a = SelectionPoint { row: 3, col: 10 };
        let b = SelectionPoint { row: 3, col: 2 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start.col, 2);
        assert_eq!(end.col, 10);
    }

    #[test]
    fn normalize_origin() {
        let a = SelectionPoint { row: 0, col: 0 };
        let b = SelectionPoint { row: 0, col: 0 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start, a);
        assert_eq!(end, b);
    }

    #[test]
    fn normalize_same_row_same_col_nonzero() {
        let p = SelectionPoint { row: 7, col: 7 };
        let (start, end) = normalize_selection(p, p);
        assert_eq!(start, p);
        assert_eq!(end, p);
    }

    // normalize_selection

    #[test]
    fn normalize_large_coordinates() {
        let a = SelectionPoint { row: usize::MAX, col: usize::MAX };
        let b = SelectionPoint { row: 0, col: 0 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start.row, 0);
        assert_eq!(end.row, usize::MAX);
    }

    /// Row takes priority in ordering: higher row always comes second
    /// even if its col is 0 and the other col is MAX.
    #[test]
    fn normalize_row_priority_over_col() {
        let a = SelectionPoint { row: 0, col: usize::MAX };
        let b = SelectionPoint { row: 1, col: 0 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start, a, "row 0 must come first regardless of col");
        assert_eq!(end, b);
    }

    /// Adjacent rows, both at col 0 - order by row.
    #[test]
    fn normalize_adjacent_rows_col_zero() {
        let a = SelectionPoint { row: 5, col: 0 };
        let b = SelectionPoint { row: 4, col: 0 };
        let (start, end) = normalize_selection(a, b);
        assert_eq!(start.row, 4);
        assert_eq!(end.row, 5);
    }

    /// Symmetry: normalize(a, b) and normalize(b, a) produce the same result.
    #[test]
    fn normalize_symmetry_many_pairs() {
        let pairs = [
            (SelectionPoint { row: 0, col: 0 }, SelectionPoint { row: 0, col: 1 }),
            (SelectionPoint { row: 3, col: 9 }, SelectionPoint { row: 1, col: 100 }),
            (SelectionPoint { row: 100, col: 0 }, SelectionPoint { row: 0, col: 100 }),
            (SelectionPoint { row: 42, col: 42 }, SelectionPoint { row: 42, col: 42 }),
        ];
        for (a, b) in pairs {
            let (s1, e1) = normalize_selection(a, b);
            let (s2, e2) = normalize_selection(b, a);
            assert_eq!((s1, e1), (s2, e2), "normalize must be symmetric for {a:?} / {b:?}");
        }
    }

    /// Idempotence: normalizing an already-normalized pair doesn't change it.
    #[test]
    fn normalize_idempotent() {
        let a = SelectionPoint { row: 10, col: 20 };
        let b = SelectionPoint { row: 3, col: 50 };
        let (s, e) = normalize_selection(a, b);
        let (s2, e2) = normalize_selection(s, e);
        assert_eq!((s, e), (s2, e2));
    }

    // slice_by_cols

    #[test]
    fn slice_ascii_mid() {
        assert_eq!(slice_by_cols("hello world", 2, 7), "llo w");
    }

    #[test]
    fn slice_full_string() {
        assert_eq!(slice_by_cols("abc", 0, 3), "abc");
    }

    #[test]
    fn slice_single_char() {
        assert_eq!(slice_by_cols("hello", 1, 2), "e");
    }

    #[test]
    fn slice_single_char_string_full() {
        assert_eq!(slice_by_cols("x", 0, 1), "x");
    }

    // slice_by_cols

    #[test]
    fn slice_empty_string() {
        assert_eq!(slice_by_cols("", 0, 5), "");
    }

    #[test]
    fn slice_empty_string_zero_range() {
        assert_eq!(slice_by_cols("", 0, 0), "");
    }

    #[test]
    fn slice_start_equals_end() {
        assert_eq!(slice_by_cols("hello", 3, 3), "");
    }

    #[test]
    fn slice_out_of_bounds() {
        assert_eq!(slice_by_cols("hi", 0, 100), "hi");
    }

    #[test]
    fn slice_start_beyond_string() {
        assert_eq!(slice_by_cols("hi", 50, 100), "");
    }

    /// `start_col` > `end_col` -- no guard in the function, loop condition
    /// `i >= end_col` triggers immediately, so result is empty.
    #[test]
    fn slice_start_greater_than_end() {
        assert_eq!(slice_by_cols("hello world", 7, 2), "");
    }

    /// Tab character is 1 char (col), not 4 or 8.
    #[test]
    fn slice_tab_chars() {
        let s = "a\tb\tc";
        // chars: a(0) \t(1) b(2) \t(3) c(4)
        assert_eq!(slice_by_cols(s, 0, 2), "a\t");
        assert_eq!(slice_by_cols(s, 1, 4), "\tb\t");
    }

    /// Newline embedded in a "line" - treated as one char.
    #[test]
    fn slice_with_embedded_newline() {
        let s = "ab\ncd";
        // a(0) b(1) \n(2) c(3) d(4)
        assert_eq!(slice_by_cols(s, 1, 4), "b\nc");
    }

    /// Carriage return embedded in a "line".
    #[test]
    fn slice_with_embedded_cr() {
        let s = "ab\rcd";
        assert_eq!(slice_by_cols(s, 0, 5), "ab\rcd");
    }

    /// Null byte is a valid char.
    #[test]
    fn slice_with_null_byte() {
        let s = "a\0b";
        assert_eq!(slice_by_cols(s, 0, 3), "a\0b");
        assert_eq!(slice_by_cols(s, 1, 2), "\0");
    }

    // slice_by_cols

    #[test]
    fn slice_unicode_emoji() {
        let s = "a\u{1F600}b\u{1F600}c";
        // chars: a(0), emoji(1), b(2), emoji(3), c(4)
        assert_eq!(slice_by_cols(s, 1, 4), "\u{1F600}b\u{1F600}");
    }

    #[test]
    fn slice_cjk_chars() {
        let s = "\u{4F60}\u{597D}\u{4E16}\u{754C}"; // ni hao shi jie
        assert_eq!(slice_by_cols(s, 1, 3), "\u{597D}\u{4E16}");
    }

    #[test]
    fn slice_mixed_unicode_and_ascii() {
        let s = "hi\u{1F600}world";
        // h(0), i(1), emoji(2), w(3), o(4), r(5), l(6), d(7)
        assert_eq!(slice_by_cols(s, 0, 3), "hi\u{1F600}");
        assert_eq!(slice_by_cols(s, 3, 8), "world");
    }

    /// Combining diacritical mark: e + combining acute = 2 chars, 1 glyph.
    /// Slicing between them splits the glyph - this is the char-based reality.
    #[test]
    fn slice_combining_diacritical_splits_glyph() {
        let s = "e\u{0301}x"; // e + combining acute + x
        // chars: e(0), \u{0301}(1), x(2)
        assert_eq!(slice_by_cols(s, 0, 1), "e"); // bare e, no accent
        assert_eq!(slice_by_cols(s, 1, 2), "\u{0301}"); // orphan combining mark
        assert_eq!(slice_by_cols(s, 0, 2), "e\u{0301}"); // full glyph
        assert_eq!(slice_by_cols(s, 0, 3), "e\u{0301}x"); // everything
    }

    /// Multiple combining marks stacked on one base: a + ring + macron.
    #[test]
    fn slice_stacked_combining_marks() {
        let s = "a\u{030A}\u{0304}z"; // a + combining ring above + combining macron + z
        // chars: a(0) ring(1) macron(2) z(3)
        assert_eq!(slice_by_cols(s, 0, 3), "a\u{030A}\u{0304}");
        assert_eq!(slice_by_cols(s, 1, 3), "\u{030A}\u{0304}"); // orphaned marks
        assert_eq!(slice_by_cols(s, 3, 4), "z");
    }

    /// ZWJ sequence (family emoji): multiple codepoints, one visual glyph.
    /// char-based slicing will split it.
    #[test]
    fn slice_zwj_family_emoji() {
        // man + ZWJ + woman + ZWJ + girl = 5 chars, 1 visible emoji
        let s = "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}";
        assert_eq!(s.chars().count(), 5);
        // Slicing first 2 chars gives man + ZWJ (broken glyph)
        assert_eq!(slice_by_cols(s, 0, 2), "\u{1F468}\u{200D}");
        // Full sequence
        assert_eq!(slice_by_cols(s, 0, 5), s);
    }

    /// Flag emoji: two regional indicator chars = 1 flag.
    #[test]
    fn slice_flag_emoji_splits() {
        let flag = "\u{1F1FA}\u{1F1F8}"; // US flag = 2 chars
        assert_eq!(flag.chars().count(), 2);
        assert_eq!(slice_by_cols(flag, 0, 1), "\u{1F1FA}"); // half a flag
        assert_eq!(slice_by_cols(flag, 0, 2), flag); // whole flag
        assert_eq!(slice_by_cols(flag, 1, 2), "\u{1F1F8}"); // other half
    }

    /// Arabic RTL text - chars are chars regardless of display direction.
    #[test]
    fn slice_arabic_rtl() {
        let s = "\u{0645}\u{0631}\u{062D}\u{0628}\u{0627}"; // mrhba
        assert_eq!(s.chars().count(), 5);
        assert_eq!(slice_by_cols(s, 1, 4), "\u{0631}\u{062D}\u{0628}");
    }

    /// All-emoji string.
    #[test]
    fn slice_all_emoji() {
        let s = "\u{1F600}\u{1F601}\u{1F602}\u{1F603}\u{1F604}";
        assert_eq!(slice_by_cols(s, 2, 4), "\u{1F602}\u{1F603}");
        assert_eq!(slice_by_cols(s, 0, 5), s);
    }

    /// Stress test: 10K character string, slice a window in the middle.
    #[test]
    fn slice_stress_10k_chars() {
        let s: String = (0..10_000).map(|i| if i % 2 == 0 { 'a' } else { 'b' }).collect();
        let sliced = slice_by_cols(&s, 4000, 4010);
        assert_eq!(sliced.len(), 10);
        assert_eq!(sliced, "ababababab");
    }

    /// Stress test: 10K emoji string.
    #[test]
    fn slice_stress_10k_emoji() {
        let s: String = "\u{1F600}".repeat(10_000);
        let sliced = slice_by_cols(&s, 9990, 10_000);
        assert_eq!(sliced.chars().count(), 10);
    }

    /// Slice exactly the last character.
    #[test]
    fn slice_last_char_only() {
        assert_eq!(slice_by_cols("abcdef", 5, 6), "f");
    }

    /// Slice exactly the first character.
    #[test]
    fn slice_first_char_only() {
        assert_eq!(slice_by_cols("abcdef", 0, 1), "a");
    }

    /// Variation selector: base + VS16 = 2 chars.
    #[test]
    fn slice_variation_selector() {
        let s = "\u{2764}\u{FE0F}x"; // red heart emoji (heart + VS16) + x
        assert_eq!(s.chars().count(), 3);
        assert_eq!(slice_by_cols(s, 0, 2), "\u{2764}\u{FE0F}");
        assert_eq!(slice_by_cols(s, 2, 3), "x");
    }

    /// Mixed script: Latin, CJK, emoji, Arabic all in one string.
    #[test]
    fn slice_mixed_scripts() {
        let s = "Hi\u{4F60}\u{1F600}\u{0645}!";
        // H(0) i(1) ni(2) emoji(3) meem(4) !(5)
        assert_eq!(s.chars().count(), 6);
        assert_eq!(slice_by_cols(s, 0, 6), s);
        assert_eq!(slice_by_cols(s, 2, 5), "\u{4F60}\u{1F600}\u{0645}");
    }

    /// Consecutive zero-range slices always return empty.
    #[test]
    fn slice_zero_width_at_every_position() {
        let s = "hello";
        for i in 0..=5 {
            assert_eq!(slice_by_cols(s, i, i), "", "zero-width at col {i}");
        }
    }

    /// Sliding window: every 3-char window of "abcde".
    #[test]
    fn slice_sliding_window() {
        let s = "abcde";
        let windows: Vec<String> = (0..3).map(|i| slice_by_cols(s, i, i + 3)).collect();
        assert_eq!(windows, vec!["abc", "bcd", "cde"]);
    }
}