linesmith 0.1.3

A Rust status line for Claude Code and other AI coding CLIs
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Raw value editor — single-line text input that replaces the
//! cursor segment with arbitrary content.
//!
//! Invoked from the items editor via the `r` verb. Useful for
//! segment IDs the picker doesn't expose (plugin IDs or future
//! built-ins). The TOML accepts whatever the user types and the
//! load layer warns on unknown IDs. The preview path doesn't
//! resolve plugin segments, so plugin IDs round-trip correctly
//! into the file but render as "unknown segment id" in the
//! preview. Document mutation lives in
//! `super::items_editor::apply_replace`; this module is UI-only.

use std::mem;

use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;
use toml_edit::DocumentMut;

use crate::config;

use super::app::{AppScreen, ScreenOutcome};
use super::items_editor::{self, ItemsEditorState};

/// What field of the segments-array entry the editor is mutating.
/// Drives `apply_replace` dispatch: a separator-character commit
/// updates the inline table's `character` field; a segment-id
/// commit replaces the array entry with a string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum RawTarget {
    /// Replace the array entry at `target_idx` with the buffer as a
    /// TOML string. The default for non-separator rows.
    SegmentId,
    /// Update the `character` field of the inline-table separator
    /// at `target_idx`. An empty buffer clears the field (boundary
    /// falls back to `[layout_options].separator`).
    SeparatorCharacter,
}

/// Editor state. `text` is the buffer; `cursor` is a char index
/// into `text` (0..=text.chars().count()). `target_idx` is the
/// segments-array position being mutated; `target_kind` selects
/// which field (segment id vs separator character); `prev` is the
/// editor we return to.
#[derive(Debug)]
pub(super) struct RawValueEditorState {
    text: String,
    cursor: usize,
    target_idx: usize,
    target_kind: RawTarget,
    prev: ItemsEditorState,
}

impl RawValueEditorState {
    pub(super) fn new(
        initial: String,
        target_idx: usize,
        target_kind: RawTarget,
        prev: ItemsEditorState,
    ) -> Self {
        let cursor = initial.chars().count();
        Self {
            text: initial,
            cursor,
            target_idx,
            target_kind,
            prev,
        }
    }
}

/// Drive the raw editor through one keypress. Esc cancels, Enter
/// commits, character keys insert at cursor, navigation keys move
/// the cursor. Modifier-bearing keys other than plain Shift are
/// inert so terminal chord shortcuts don't insert garbage.
pub(super) fn update(
    state: &mut RawValueEditorState,
    document: &mut DocumentMut,
    config: &mut config::Config,
    key: KeyEvent,
) -> ScreenOutcome {
    match (key.code, key.modifiers) {
        (KeyCode::Esc, KeyModifiers::NONE) => {
            let prev = mem::take(&mut state.prev);
            ScreenOutcome::NavigateTo(AppScreen::ItemsEditor(prev))
        }
        (KeyCode::Enter, KeyModifiers::NONE) => {
            let target_idx = state.target_idx;
            let target_kind = state.target_kind;
            let new_value = mem::take(&mut state.text);
            let prev = mem::take(&mut state.prev);
            items_editor::apply_replace(prev, document, config, target_idx, target_kind, &new_value)
        }
        (KeyCode::Backspace, KeyModifiers::NONE) => {
            delete_before_cursor(state);
            ScreenOutcome::Stay
        }
        (KeyCode::Delete, KeyModifiers::NONE) => {
            delete_at_cursor(state);
            ScreenOutcome::Stay
        }
        (KeyCode::Left, KeyModifiers::NONE) => {
            state.cursor = state.cursor.saturating_sub(1);
            ScreenOutcome::Stay
        }
        (KeyCode::Right, KeyModifiers::NONE) => {
            state.cursor = (state.cursor + 1).min(state.text.chars().count());
            ScreenOutcome::Stay
        }
        (KeyCode::Home, KeyModifiers::NONE) => {
            state.cursor = 0;
            ScreenOutcome::Stay
        }
        (KeyCode::End, KeyModifiers::NONE) => {
            state.cursor = state.text.chars().count();
            ScreenOutcome::Stay
        }
        (KeyCode::Char(c), mods) if is_text_input_modifier(mods) => {
            insert_char(state, c);
            ScreenOutcome::Stay
        }
        _ => ScreenOutcome::Stay,
    }
}

/// Predicate for `KeyCode::Char` modifier sets we treat as text
/// input. Accepts no-modifier and Shift (uppercase letters), plus
/// the `CONTROL | ALT` AltGr combination (with optional Shift)
/// that produces characters like `@`, `{`, `}`, `€` on non-US
/// keyboard layouts. Ctrl-alone or Alt-alone is treated as a
/// chord shortcut and rejected so terminal hotkeys don't leak
/// text into the buffer.
fn is_text_input_modifier(mods: KeyModifiers) -> bool {
    if mods.is_empty() || mods == KeyModifiers::SHIFT {
        return true;
    }
    let altgr = KeyModifiers::CONTROL | KeyModifiers::ALT;
    mods == altgr || mods == altgr | KeyModifiers::SHIFT
}

fn delete_before_cursor(state: &mut RawValueEditorState) {
    if state.cursor == 0 {
        return;
    }
    let new_text: String = state
        .text
        .chars()
        .enumerate()
        .filter_map(|(i, c)| (i + 1 != state.cursor).then_some(c))
        .collect();
    state.text = new_text;
    state.cursor -= 1;
}

fn delete_at_cursor(state: &mut RawValueEditorState) {
    if state.cursor >= state.text.chars().count() {
        return;
    }
    let new_text: String = state
        .text
        .chars()
        .enumerate()
        .filter_map(|(i, c)| (i != state.cursor).then_some(c))
        .collect();
    state.text = new_text;
}

fn insert_char(state: &mut RawValueEditorState, c: char) {
    let mut new_text = String::with_capacity(state.text.len() + c.len_utf8());
    let mut inserted = false;
    for (i, ch) in state.text.chars().enumerate() {
        if i == state.cursor {
            new_text.push(c);
            inserted = true;
        }
        new_text.push(ch);
    }
    if !inserted {
        new_text.push(c);
    }
    state.text = new_text;
    state.cursor += 1;
}

/// Render the editor. Layout: title row, help row, blank, the
/// text input row with cursor highlighted via reverse video on a
/// single character (or a trailing reverse-video space when the
/// cursor sits at end-of-text).
pub(super) fn view(state: &RawValueEditorState, frame: &mut Frame, area: Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // title
            Constraint::Length(1), // help
            Constraint::Length(1), // blank
            Constraint::Length(1), // text input
            Constraint::Min(1),    // remainder
        ])
        .split(area);

    let title = Paragraph::new(Line::from(Span::styled(
        " edit raw value ",
        Style::default().add_modifier(Modifier::BOLD),
    )))
    .alignment(Alignment::Center);
    frame.render_widget(title, chunks[0]);

    let help = Paragraph::new(Line::from(vec![
        Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" commit · "),
        Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" cancel · "),
        Span::styled("← →", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" cursor · "),
        Span::styled("Bksp", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" delete"),
    ]))
    .alignment(Alignment::Center);
    frame.render_widget(help, chunks[1]);

    let text_area = chunks[3];
    let mut spans: Vec<Span<'_>> = Vec::with_capacity(3);
    let chars: Vec<char> = state.text.chars().collect();
    let cursor = state.cursor.min(chars.len());
    let cursor_style = Style::default().add_modifier(Modifier::REVERSED);
    if cursor < chars.len() {
        if cursor > 0 {
            spans.push(Span::raw(chars[..cursor].iter().collect::<String>()));
        }
        spans.push(Span::styled(chars[cursor].to_string(), cursor_style));
        if cursor + 1 < chars.len() {
            spans.push(Span::raw(chars[cursor + 1..].iter().collect::<String>()));
        }
    } else {
        // Cursor sits past the last char — render a reverse-video
        // space so the user can see the insertion point.
        if !chars.is_empty() {
            spans.push(Span::raw(state.text.clone()));
        }
        spans.push(Span::styled(" ", cursor_style));
    }
    let body = Paragraph::new(Line::from(spans)).alignment(Alignment::Center);
    frame.render_widget(body, text_area);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::main_menu::MainMenuState;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn document(toml: &str) -> DocumentMut {
        toml.parse().expect("test toml must parse")
    }

    fn editor(initial: &str) -> RawValueEditorState {
        RawValueEditorState::new(
            initial.to_string(),
            0,
            RawTarget::SegmentId,
            ItemsEditorState::new(
                items_editor::LineKey::Single,
                items_editor::ItemsEditorPrev::MainMenu(MainMenuState::default()),
            ),
        )
    }

    #[test]
    fn enter_commits_text_and_returns_to_items_editor() {
        let mut s = editor("model");
        let mut doc = document("[line]\nsegments = [\"model\"]\n");
        let mut cfg = config::Config::default();
        for c in ['_', '2'] {
            update(&mut s, &mut doc, &mut cfg, key(KeyCode::Char(c)));
        }
        let outcome = update(&mut s, &mut doc, &mut cfg, key(KeyCode::Enter));
        assert!(matches!(
            outcome,
            ScreenOutcome::CommitAndNavigate(AppScreen::ItemsEditor(_))
        ));
        let line = cfg.line.expect("line reparsed");
        let ids: Vec<&str> = line
            .segments
            .iter()
            .filter_map(config::LineEntry::segment_id)
            .collect();
        assert_eq!(ids, vec!["model_2"]);
    }

    #[test]
    fn esc_cancels_and_leaves_document_untouched() {
        let raw = "[line]\nsegments = [\"model\"]\n";
        let mut s = editor("model");
        let mut doc = document(raw);
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Char('x')));
        let outcome = update(&mut s, &mut doc, &mut cfg, key(KeyCode::Esc));
        assert!(matches!(
            outcome,
            ScreenOutcome::NavigateTo(AppScreen::ItemsEditor(_))
        ));
        assert_eq!(doc.to_string(), raw);
    }

    #[test]
    fn backspace_deletes_char_before_cursor() {
        let mut s = editor("abc");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Backspace));
        assert_eq!(s.text, "ab");
        assert_eq!(s.cursor, 2);
    }

    #[test]
    fn backspace_in_middle_removes_char_left_of_cursor() {
        // Cursor=2 in "abc"; Backspace removes 'b', cursor lands
        // at 1. Pins the mid-string deletion path that the
        // end-of-string test doesn't cover.
        let mut s = editor("abc");
        s.cursor = 2;
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Backspace));
        assert_eq!(s.text, "ac");
        assert_eq!(s.cursor, 1);
    }

    #[test]
    fn backspace_on_multi_byte_char_does_not_panic() {
        // The chars()-based deletion path must handle multi-byte
        // characters without slicing through a UTF-8 boundary.
        // Insert 'é' then Backspace; observe empty buffer.
        let mut s = editor("");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Char('é')));
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Backspace));
        assert_eq!(s.text, "");
        assert_eq!(s.cursor, 0);
    }

    #[test]
    fn backspace_at_zero_is_inert() {
        let mut s = editor("abc");
        s.cursor = 0;
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Backspace));
        assert_eq!(s.text, "abc");
        assert_eq!(s.cursor, 0);
    }

    #[test]
    fn delete_removes_char_at_cursor() {
        let mut s = editor("abc");
        s.cursor = 1;
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Delete));
        assert_eq!(s.text, "ac");
        assert_eq!(s.cursor, 1);
    }

    #[test]
    fn arrows_move_cursor_within_bounds() {
        let mut s = editor("abc");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Right));
        assert_eq!(s.cursor, 3, "Right at end is a no-op");
        for _ in 0..3 {
            update(&mut s, &mut doc, &mut cfg, key(KeyCode::Left));
        }
        assert_eq!(s.cursor, 0);
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Left));
        assert_eq!(s.cursor, 0, "Left at 0 is a no-op");
    }

    #[test]
    fn home_and_end_jump_to_bounds() {
        let mut s = editor("abc");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Home));
        assert_eq!(s.cursor, 0);
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::End));
        assert_eq!(s.cursor, 3);
    }

    #[test]
    fn char_inserted_at_cursor_advances_cursor() {
        let mut s = editor("ac");
        s.cursor = 1;
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Char('b')));
        assert_eq!(s.text, "abc");
        assert_eq!(s.cursor, 2);
    }

    #[test]
    fn shift_char_is_treated_as_text_input() {
        // Shift+a → uppercase A; the editor accepts it as text,
        // not as a chord.
        let mut s = editor("");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        let chord = KeyEvent::new(KeyCode::Char('A'), KeyModifiers::SHIFT);
        update(&mut s, &mut doc, &mut cfg, chord);
        assert_eq!(s.text, "A");
    }

    #[test]
    fn altgr_modified_chars_insert_as_text() {
        // On non-US keyboard layouts, `@`, `{`, `}`, `€` etc.
        // arrive with CONTROL|ALT set (AltGr). These must insert
        // as text — rejecting them would make those characters
        // impossible to type, breaking edits for plugin IDs and
        // segment names that need them.
        let mut s = editor("");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        let altgr = KeyEvent::new(
            KeyCode::Char('@'),
            KeyModifiers::CONTROL | KeyModifiers::ALT,
        );
        update(&mut s, &mut doc, &mut cfg, altgr);
        assert_eq!(s.text, "@");
        // Shift+AltGr (some layouts) — also accepted.
        let shift_altgr = KeyEvent::new(
            KeyCode::Char('Œ'),
            KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SHIFT,
        );
        update(&mut s, &mut doc, &mut cfg, shift_altgr);
        assert_eq!(s.text, "");
    }

    #[test]
    fn ctrl_and_alt_modified_chars_are_inert() {
        // Ctrl+S / Ctrl+C are intercepted at the app level and
        // never reach this update. Ctrl+other-letter and Alt+letter
        // chords shouldn't insert anything — pin so a future relax
        // of the modifier match doesn't let chord keys leak text.
        let mut s = editor("");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        for mods in [KeyModifiers::CONTROL, KeyModifiers::ALT] {
            let chord = KeyEvent::new(KeyCode::Char('a'), mods);
            update(&mut s, &mut doc, &mut cfg, chord);
            assert_eq!(
                s.text, "",
                "chord chars (mods={mods:?}) must not insert text"
            );
        }
    }

    #[test]
    fn unicode_char_inserts_correctly() {
        // Multi-byte char insertion. The cursor is a char index
        // (not byte), so this exercises the chars()-based
        // accounting rather than naive byte indexing.
        let mut s = editor("");
        let mut doc = document("");
        let mut cfg = config::Config::default();
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Char('é')));
        update(&mut s, &mut doc, &mut cfg, key(KeyCode::Char('')));
        assert_eq!(s.text, "é日");
        assert_eq!(s.cursor, 2);
    }

    fn render_to_string(state: &RawValueEditorState, width: u16, height: u16) -> String {
        use ratatui::backend::TestBackend;
        use ratatui::Terminal;
        let backend = TestBackend::new(width, height);
        let mut terminal = Terminal::new(backend).expect("backend");
        terminal
            .draw(|frame| view(state, frame, frame.area()))
            .expect("draw");
        crate::tui::buffer_to_string(terminal.backend().buffer())
    }

    #[test]
    fn snapshot_raw_value_editor_with_seed_text() {
        // `frame.set_cursor(...)` doesn't materialize into the TestBackend
        // buffer, so cursor position isn't pinned here.
        let s = editor("model");
        insta::assert_snapshot!("raw_value_editor_seed", render_to_string(&s, 60, 12));
    }

    #[test]
    fn snapshot_raw_value_editor_empty_buffer() {
        let s = editor("");
        insta::assert_snapshot!("raw_value_editor_empty", render_to_string(&s, 60, 12));
    }
}