nano-coder 0.3.2

A 6MB coding agent for the terminal and for agent fleets: multi-provider, ACP, resumable sessions, plans.
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
//! Minimal line editor for a terminal stdin. Reading key by key (instead of
//! the kernel's line mode) lets Ctrl-O and Ctrl-C act immediately, and lets
//! text typed during a turn show on the status line instead of mixing with
//! streamed output.

use std::sync::{Arc, Mutex, OnceLock};

use crate::status::StatusLine;

/// Where the line being typed is shown.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditMode {
    /// After the `> ` prompt, inline.
    Prompt,
    /// During a turn: on the status line (inline if there is none).
    Turn,
}

pub struct EditView {
    line: String,
    mode: EditMode,
    status: Option<Arc<StatusLine>>,
    /// Rows below the prompt used by the command menu.
    menu_rows: usize,
    /// Esc hid the menu; it comes back when the line changes.
    menu_hidden: bool,
    /// Draw the command menu (key-by-key terminal input only).
    menu_enabled: bool,
    /// Visible width of the prompt before the line (it starts with a
    /// timestamp when timestamps are on).
    prompt_width: usize,
}

pub type SharedView = Arc<Mutex<EditView>>;

impl EditView {
    pub fn shared(status: Option<Arc<StatusLine>>) -> SharedView {
        Arc::new(Mutex::new(Self {
            line: String::new(),
            mode: EditMode::Prompt,
            status,
            menu_rows: 0,
            menu_hidden: false,
            menu_enabled: false,
            prompt_width: 2,
        }))
    }

    pub fn set_mode(&mut self, mode: EditMode) {
        self.mode = mode;
        if let Some(status) = &self.status {
            match mode {
                EditMode::Turn if !self.line.is_empty() => status.set_input(Some(&self.line)),
                _ => status.set_input(None),
            }
        }
    }

    fn on_status(&self) -> Option<&StatusLine> {
        (self.mode == EditMode::Turn).then_some(self.status.as_deref()).flatten()
    }

    fn insert(&mut self, text: &str) {
        self.line.push_str(text);
        match self.on_status() {
            Some(status) => status.set_input(Some(&self.line)),
            None => write(text),
        }
        self.line_changed();
    }

    fn line_changed(&mut self) {
        self.menu_hidden = false;
        self.draw_menu();
    }

    fn menu_visible(&self) -> bool {
        self.menu_rows > 0
    }

    /// Redraw the command menu below the prompt for the current line.
    fn draw_menu(&mut self) {
        if !self.menu_enabled || self.mode != EditMode::Prompt {
            return;
        }
        let (rows, cols) = crate::status::terminal_size().unwrap_or((24, 80));
        // Reserve the prompt row, plus the status row only when a status line
        // is present (none under AGENTIC_NO_STATUS or a short terminal).
        let reserved = if self.status.is_some() { 2 } else { 1 };
        let max_rows = (rows as usize).saturating_sub(reserved).min(16);
        let lines = if self.menu_hidden { Vec::new() } else { crate::commands::menu(&self.line, cols as usize, max_rows) };
        let (seq, used) = menu_sequence(self.menu_rows, &lines);
        self.menu_rows = used;
        if !seq.is_empty() {
            write(&seq);
        }
    }

    /// Hide the menu (Esc) until the line changes.
    fn hide_menu(&mut self) {
        self.menu_hidden = true;
        self.draw_menu();
    }

    /// Tab: complete a `/command`; elsewhere a space.
    fn tab(&mut self) {
        let is_command = self.line.starts_with('/') && !self.line.contains(char::is_whitespace);
        match is_command.then(|| crate::commands::complete(&self.line)).flatten() {
            Some(done) => {
                let rest = done[self.line.len()..].to_string();
                if !rest.is_empty() {
                    self.insert(&rest);
                }
            }
            None if is_command => {}
            None => self.insert(" "),
        }
    }

    /// The prompt (`HH:MM:SS > ` or `> `) followed by the line so far.
    pub fn prompt(&mut self) -> String {
        let stamp = crate::ui::stamp();
        self.prompt_width = crate::ui::visible_width(&stamp) + 2;
        format!("{stamp}> {}", self.line)
    }

    /// On Enter: rewrite the prompt's timestamp with the time the line was
    /// sent. Same width, so nothing reflows.
    fn restamp_prompt(&self) {
        let stamp = crate::ui::stamp();
        if !self.menu_enabled || self.mode != EditMode::Prompt || stamp.is_empty() || self.prompt_width <= 2 {
            return;
        }
        let cols = crate::status::terminal_size().map(|(_, c)| c as usize).unwrap_or(80);
        let up = rows_above_cursor(self.prompt_width + self.line.chars().count(), cols);
        let up = if up > 0 { format!("\x1b[{up}A") } else { String::new() };
        write(&format!("\x1b7{up}\r{stamp}\x1b8"));
    }

    /// The prompt and line were printed again (after other output): the old
    /// menu rows scrolled away, so draw it afresh.
    pub fn prompt_redrawn(&mut self) {
        self.menu_rows = 0;
        self.draw_menu();
    }

    /// The terminal was resized: the old menu rows may no longer fit under the
    /// new scroll region, so blank the rows we reserved and redraw the menu
    /// sized to the new terminal. Call after the status line re-establishes the
    /// scroll region for the new size.
    pub fn resize(&mut self) {
        if self.menu_rows > 0 {
            let (seq, _) = menu_sequence(self.menu_rows, &[]);
            write(&seq);
            self.menu_rows = 0;
        }
        self.draw_menu();
    }

    /// Remove the last `n` characters.
    fn erase(&mut self, n: usize) {
        let mut erased = String::new();
        for _ in 0..n {
            match self.line.pop() {
                Some(c) => erased.push(c),
                None => break,
            }
        }
        match self.on_status() {
            Some(status) => status.set_input((!self.line.is_empty()).then_some(self.line.as_str())),
            None => write(&"\x08 \x08".repeat(erased.chars().count())),
        }
        if !erased.is_empty() {
            self.line_changed();
        }
    }

    fn erase_word(&mut self) {
        let trimmed = self.line.trim_end();
        let word_start = trimmed
            .char_indices()
            .rev()
            .find(|(_, c)| c.is_whitespace())
            .map(|(i, c)| i + c.len_utf8())
            .unwrap_or(0);
        let n = self.line[word_start..].chars().count();
        self.erase(n);
    }

    fn take(&mut self) -> String {
        if self.menu_visible() {
            let (seq, _) = menu_sequence(self.menu_rows, &[]);
            write(&seq);
            self.menu_rows = 0;
        }
        self.menu_hidden = false;
        self.restamp_prompt();
        let line = std::mem::take(&mut self.line);
        match self.on_status() {
            Some(status) => status.set_input(None),
            None => write("\r\n"),
        }
        line
    }
}

/// Terminal output that shows `lines` below the cursor's row (which holds
/// the prompt), given that `old_rows` rows are already in use there, and
/// the number of rows in use afterwards. The cursor ends where it started.
/// Rows are reserved with IND (ESC D), which scrolls at the bottom of the
/// scroll region and keeps the column; the status line below the region is
/// never touched.
fn menu_sequence(old_rows: usize, lines: &[String]) -> (String, usize) {
    if old_rows == 0 && lines.is_empty() {
        return (String::new(), 0);
    }
    let mut seq = String::new();
    if lines.len() > old_rows {
        seq.push_str(&"\x1bD".repeat(lines.len()));
        seq.push_str(&format!("\x1b[{}A", lines.len()));
    }
    let rows = old_rows.max(lines.len());
    seq.push_str("\x1b7");
    for i in 0..rows {
        seq.push_str("\x1b[1B\r\x1b[2K");
        if let Some(line) = lines.get(i) {
            seq.push_str(line);
        }
    }
    seq.push_str("\x1b8");
    (seq, if lines.is_empty() { 0 } else { rows })
}

/// How many rows above the cursor the prompt starts, when `chars`
/// characters have been printed from column 0 of a `cols`-wide terminal.
/// A row filled exactly leaves the cursor on it (pending wrap).
fn rows_above_cursor(chars: usize, cols: usize) -> usize {
    if chars == 0 || cols == 0 {
        return 0;
    }
    if chars.is_multiple_of(cols) { chars / cols - 1 } else { chars / cols }
}

fn write(text: &str) {
    use std::io::Write;
    let mut out = std::io::stdout().lock();
    let _ = out.write_all(text.as_bytes());
    let _ = out.flush();
}

/// What a key press produced.
pub enum Key {
    Line(String),
    Eof,
    Interrupt,
    ToggleThinking,
    /// A lone Esc press (not part of an escape sequence).
    Escape,
}

/// How long to wait after Esc for the rest of an escape sequence. Terminals
/// send sequences in one write, so a lone Esc is one with nothing following.
const ESCAPE_SEQUENCE_WAIT_MS: i32 = 30;

static ORIGINAL: OnceLock<libc::termios> = OnceLock::new();

fn get_termios() -> Option<libc::termios> {
    let mut termios: libc::termios = unsafe { std::mem::zeroed() };
    (unsafe { libc::tcgetattr(libc::STDIN_FILENO, &mut termios) } == 0).then_some(termios)
}

/// Put the terminal back the way it was (on exit or panic).
pub fn restore_terminal() {
    if let Some(original) = ORIGINAL.get() {
        unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, original) };
    }
}

/// Key-by-key input while alive: no echo, no line buffering, and control
/// keys (Ctrl-C, Ctrl-O, which macOS would use to discard output) delivered
/// as bytes. Output processing is left on.
struct KeyMode;

impl KeyMode {
    fn enter() -> Option<Self> {
        let original = get_termios()?;
        let _ = ORIGINAL.set(original);
        let mut raw = *ORIGINAL.get().unwrap();
        raw.c_lflag &= !(libc::ICANON | libc::ECHO | libc::ISIG | libc::IEXTEN);
        raw.c_cc[libc::VMIN] = 1;
        raw.c_cc[libc::VTIME] = 0;
        (unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &raw) } == 0).then_some(Self)
    }
}

impl Drop for KeyMode {
    fn drop(&mut self) {
        restore_terminal();
    }
}

/// Reads keys, carrying bytes that arrived past the end of a line (pastes)
/// into the next read.
#[derive(Default)]
pub struct LineReader {
    pending: std::collections::VecDeque<u8>,
    utf8: Vec<u8>,
}

impl LineReader {
    fn next_byte(&mut self) -> Option<u8> {
        if let Some(byte) = self.pending.pop_front() {
            return Some(byte);
        }
        let mut buf = [0u8; 1024];
        loop {
            let n = unsafe { libc::read(libc::STDIN_FILENO, buf.as_mut_ptr().cast(), buf.len()) };
            if n > 0 {
                self.pending.extend(&buf[..n as usize]);
                return self.pending.pop_front();
            }
            if n == 0 || std::io::Error::last_os_error().kind() != std::io::ErrorKind::Interrupted {
                return None;
            }
        }
    }

    /// The next byte if one arrives within `ms` milliseconds.
    fn byte_within(&mut self, ms: i32) -> Option<u8> {
        if self.pending.is_empty() {
            let mut fd = libc::pollfd { fd: libc::STDIN_FILENO, events: libc::POLLIN, revents: 0 };
            if unsafe { libc::poll(&mut fd, 1, ms) } <= 0 {
                return None;
            }
        }
        self.next_byte()
    }

    /// Read one line. Ctrl-C, Ctrl-O and Esc go to `send` straight away; the
    /// return value is a `Key::Line` or `Key::Eof`.
    pub fn read_line(&mut self, view: &SharedView, send: &dyn Fn(Key)) -> Key {
        let _mode = KeyMode::enter();
        view.lock().unwrap().menu_enabled = true;
        let shared = view;
        loop {
            let Some(byte) = self.next_byte() else { return Key::Eof };
            let mut view = view.lock().unwrap();
            match byte {
                b'\r' | b'\n' => {
                    // Treat CR LF as one line end.
                    if byte == b'\r' && self.pending.front() == Some(&b'\n') {
                        self.pending.pop_front();
                    }
                    return Key::Line(view.take() + "\n");
                }
                0x03 => {
                    let n = view.line.chars().count();
                    view.erase(n);
                    drop(view);
                    send(Key::Interrupt);
                }
                0x04 if view.line.is_empty() => return Key::Eof,
                0x0f => {
                    drop(view);
                    send(Key::ToggleThinking);
                }
                0x7f | 0x08 => view.erase(1),
                0x15 => {
                    let n = view.line.chars().count();
                    view.erase(n);
                }
                0x17 => view.erase_word(),
                0x1b => {
                    let menu = view.menu_visible();
                    drop(view);
                    match self.byte_within(ESCAPE_SEQUENCE_WAIT_MS) {
                        // Skip escape sequences (arrow keys and the like).
                        Some(b'[' | b'O') => {
                            while let Some(b) = self.next_byte() {
                                if (0x40..=0x7e).contains(&b) {
                                    break;
                                }
                            }
                        }
                        None if menu => shared.lock().unwrap().hide_menu(),
                        None => send(Key::Escape),
                        // Esc Esc typed faster than the wait: two presses.
                        Some(0x1b) => {
                            self.pending.push_front(0x1b);
                            send(Key::Escape);
                        }
                        // Alt+key: ignored.
                        Some(_) => {}
                    }
                }
                b'\t' => view.tab(),
                byte if byte < 0x20 => {}
                byte => {
                    self.utf8.push(byte);
                    match std::str::from_utf8(&self.utf8) {
                        Ok(text) => {
                            let text = text.to_string();
                            self.utf8.clear();
                            view.insert(&text);
                        }
                        Err(e) if e.error_len().is_some() => self.utf8.clear(),
                        Err(_) => {}
                    }
                }
            }
        }
    }
}

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

    #[test]
    fn erase_word_handles_multibyte_spaces() {
        let view = EditView::shared(None);
        let mut view = view.lock().unwrap();
        view.mode = EditMode::Turn; // no status line: echoes, but must not panic
        view.line = "run a\u{a0}bé  ".into();
        view.erase_word();
        assert_eq!(view.line, "run a\u{a0}");
        view.erase_word();
        assert_eq!(view.line, "run ");
    }

    #[test]
    fn menu_rows_are_reserved_drawn_and_cleared() {
        let lines = vec!["a".to_string(), "b".to_string()];
        let (seq, rows) = menu_sequence(0, &lines);
        assert_eq!(rows, 2);
        assert_eq!(seq, "\x1bD\x1bD\x1b[2A\x1b7\x1b[1B\r\x1b[2Ka\x1b[1B\r\x1b[2Kb\x1b8");
        // Narrowing reuses the rows and blanks the extra one.
        let (seq, rows) = menu_sequence(2, &lines[..1]);
        assert_eq!((seq.as_str(), rows), ("\x1b7\x1b[1B\r\x1b[2Ka\x1b[1B\r\x1b[2K\x1b8", 2));
        let (seq, rows) = menu_sequence(2, &[]);
        assert_eq!((seq.as_str(), rows), ("\x1b7\x1b[1B\r\x1b[2K\x1b[1B\r\x1b[2K\x1b8", 0));
        assert_eq!(menu_sequence(0, &[]), (String::new(), 0));
    }

    #[test]
    fn finds_the_prompt_row_of_a_wrapped_line() {
        assert_eq!(rows_above_cursor(11, 80), 0);
        assert_eq!(rows_above_cursor(80, 80), 0, "pending wrap stays on the row");
        assert_eq!(rows_above_cursor(81, 80), 1);
        assert_eq!(rows_above_cursor(200, 80), 2);
    }

    #[test]
    fn tab_completes_commands_and_is_a_space_elsewhere() {
        let view = EditView::shared(None);
        let mut view = view.lock().unwrap();
        view.mode = EditMode::Turn; // not drawn: no terminal in tests
        view.line = "/comp".into();
        view.tab();
        assert_eq!(view.line, "/compact ");
        view.line = "/s".into();
        view.tab();
        assert_eq!(view.line, "/s", "ambiguous: unchanged");
        view.line = "fix it".into();
        view.tab();
        assert_eq!(view.line, "fix it ");
    }
}