bashkit 0.20.1

Awesomely fast virtual sandbox with bash and file system
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! `nano` builtin: a small modeless text editor for the in-process terminal.
//!
//! Only usable inside a [`Terminal`](crate::terminal::Terminal) session, like
//! `vi`: it reads keys from the terminal device in raw mode and draws on the
//! alternate screen.
//!
//! Important decisions:
//! - A useful subset of GNU nano: typing inserts, arrows/Home/End/PageUp/
//!   PageDown move, `^O` write out (asks for the name), `^X` exit (asks to
//!   save a modified buffer), `^K`/`^U` cut and paste lines, `^W` search,
//!   `^G` help, `^C` cursor position. No multiple buffers, syntax colors,
//!   undo, mouse, or rc files; unknown keys are ignored.
//! - Same bounds and VFS rules as `vi` (8 MiB buffer, writes through the
//!   session VFS). Tabs render to 8-column stops; one char is one cell.
//! - Exits 0 whether or not the buffer was saved, like GNU nano.

use std::path::PathBuf;

use async_trait::async_trait;

use super::{Builtin, Context, resolve_path};
use crate::error::{Error, Result};
use crate::interpreter::ExecResult;
use crate::terminal::{Key, ScreenGuard, Tty, read_key};

// THREAT[TM-DOS-119]: bounded editor memory, same cap as vi.
const MAX_BUFFER_BYTES: usize = 8 * 1024 * 1024;
/// Cap on the cut buffer (`^K` accumulates lines).
const MAX_CUT_BYTES: usize = 1024 * 1024;
const TAB_STOP: usize = 8;
const HELP_ROW_1: &str = "^G Help  ^O Write Out  ^W Where Is  ^K Cut  ^C Location";
const HELP_ROW_2: &str = "^X Exit  ^U Paste";

/// The nano builtin.
pub struct Nano;

#[async_trait]
impl Builtin for Nano {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        let mut file: Option<&String> = None;
        for arg in ctx.args {
            if arg == "--help" {
                return Ok(ExecResult::ok(
                    "usage: nano [FILE]\nModeless text editor; needs an interactive terminal.\n\
                     ^O write out, ^X exit, ^K/^U cut/paste line, ^W search, ^G help.\n",
                ));
            }
            if arg.starts_with('-') || arg.starts_with('+') {
                return Ok(ExecResult::err(
                    format!("nano: unsupported option: {arg}\n"),
                    2,
                ));
            }
            if file.is_some() {
                return Ok(ExecResult::err("nano: only one file is supported\n", 2));
            }
            file = Some(arg);
        }
        let Some(tty) = ctx.execution_extension::<Tty>() else {
            return Ok(ExecResult::err(
                "nano: not a terminal (run inside bashkit::terminal::Terminal)\n",
                1,
            ));
        };
        let tty = tty.try_with(Clone::clone).map_err(|_| Error::Cancelled)?;
        let path = file.map(|f| resolve_path(ctx.cwd, f));
        run_on(&tty, &ctx, path, file.cloned()).await
    }
}

/// Edit `path` (or an unnamed buffer) on `tty` until the user exits.
pub(crate) async fn run_on(
    tty: &Tty,
    ctx: &Context<'_>,
    path: Option<PathBuf>,
    display_name: Option<String>,
) -> Result<ExecResult> {
    let size = tty.size();
    let mut ed = Editor::new(size.rows, size.cols);
    ed.display_name = display_name;
    if let Some(path) = &path {
        match ctx.fs.stat(path).await {
            Ok(meta) if meta.file_type.is_dir() => {
                return Ok(ExecResult::err(
                    format!("nano: {}: is a directory\n", path.display()),
                    1,
                ));
            }
            Ok(meta) if meta.size as usize > MAX_BUFFER_BYTES => {
                return Ok(ExecResult::err(
                    format!("nano: {}: file too large\n", path.display()),
                    1,
                ));
            }
            Ok(_) => {
                let bytes = ctx.fs.read_file(path).await?;
                ed.load(&String::from_utf8_lossy(&bytes));
                ed.status = format!("[ Read {} lines ]", ed.lines.len());
            }
            Err(_) => ed.status = "[ New File ]".into(),
        }
    }
    ed.path = path;
    let _screen = ScreenGuard::enter(tty, true);
    ed.run(tty, ctx).await;
    Ok(ExecResult::ok(""))
}

/// What a bottom-line prompt is asking for.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Ask {
    /// File name for `^O` (or `^X` then Y); `exit` quits after writing.
    FileName {
        exit: bool,
    },
    /// "Save modified buffer?" from `^X`.
    SaveOnExit,
    Search,
}

struct Editor {
    lines: Vec<Vec<char>>,
    row: usize,
    col: usize,
    top: usize,
    left: usize,
    rows: u16,
    cols: u16,
    path: Option<PathBuf>,
    display_name: Option<String>,
    modified: bool,
    status: String,
    cut: Vec<Vec<char>>,
    /// Consecutive `^K` presses append to the cut buffer.
    last_was_cut: bool,
    ask: Option<(Ask, String)>,
    last_search: String,
    quit: bool,
}

impl Editor {
    fn new(rows: u16, cols: u16) -> Self {
        Self {
            lines: vec![Vec::new()],
            row: 0,
            col: 0,
            top: 0,
            left: 0,
            rows,
            cols,
            path: None,
            display_name: None,
            modified: false,
            status: String::new(),
            cut: Vec::new(),
            last_was_cut: false,
            ask: None,
            last_search: String::new(),
            quit: false,
        }
    }

    fn load(&mut self, text: &str) {
        let text = text.strip_suffix('\n').unwrap_or(text);
        self.lines = text.split('\n').map(|l| l.chars().collect()).collect();
        if self.lines.is_empty() {
            self.lines.push(Vec::new());
        }
    }

    fn text(&self) -> String {
        let mut out = String::new();
        for line in &self.lines {
            out.extend(line.iter());
            out.push('\n');
        }
        out
    }

    fn bytes(&self) -> usize {
        self.lines.iter().map(|l| l.len() + 1).sum()
    }

    fn name(&self) -> String {
        self.display_name
            .clone()
            .unwrap_or_else(|| "New Buffer".into())
    }

    async fn run(&mut self, tty: &Tty, ctx: &Context<'_>) {
        self.render(tty);
        while !self.quit {
            let key = match read_key(tty).await {
                Some(Ok(key)) => key,
                Some(Err(())) => {
                    let size = tty.size();
                    self.rows = size.rows;
                    self.cols = size.cols;
                    self.render(tty);
                    continue;
                }
                None => return,
            };
            if self.ask.is_some() {
                self.ask_key(key, ctx).await;
            } else {
                self.edit_key(key);
            }
            if !self.quit {
                self.render(tty);
            }
        }
    }

    fn edit_key(&mut self, key: Key) {
        let was_cut = std::mem::take(&mut self.last_was_cut);
        match key {
            Key::Char(c) => self.insert(c),
            Key::Ctrl(b'i') => self.insert('\t'),
            Key::Enter => self.newline(),
            Key::Backspace => self.backspace(),
            Key::Delete | Key::Ctrl(b'd') => self.delete(),
            Key::Left | Key::Ctrl(b'b') => self.left(),
            Key::Right | Key::Ctrl(b'f') => self.right(),
            Key::Up | Key::Ctrl(b'p') => self.vertical(-1),
            Key::Down | Key::Ctrl(b'n') => self.vertical(1),
            Key::Home | Key::Ctrl(b'a') => self.col = 0,
            Key::End | Key::Ctrl(b'e') => self.col = self.lines[self.row].len(),
            Key::PageUp | Key::Ctrl(b'y') => self.vertical(-(self.text_rows() as isize)),
            Key::PageDown | Key::Ctrl(b'v') => self.vertical(self.text_rows() as isize),
            Key::Ctrl(b'k') => self.cut_line(was_cut),
            Key::Ctrl(b'u') => self.paste(),
            Key::Ctrl(b'o') => {
                self.ask = Some((Ask::FileName { exit: false }, self.default_name()));
            }
            Key::Ctrl(b'x') => {
                if self.modified {
                    self.ask = Some((Ask::SaveOnExit, String::new()));
                } else {
                    self.quit = true;
                }
            }
            Key::Ctrl(b'w') => self.ask = Some((Ask::Search, String::new())),
            Key::Ctrl(b'g') => {
                self.status = "^O save  ^X exit  ^K cut line  ^U paste  ^W search  \
                               arrows move"
                    .into();
            }
            Key::Ctrl(b'c') => {
                self.status = format!(
                    "[ line {}/{}, col {}/{} ]",
                    self.row + 1,
                    self.lines.len(),
                    self.col + 1,
                    self.lines[self.row].len() + 1
                );
            }
            _ => {}
        }
    }

    fn default_name(&self) -> String {
        self.display_name.clone().unwrap_or_default()
    }

    async fn ask_key(&mut self, key: Key, ctx: &Context<'_>) {
        let Some((ask, mut input)) = self.ask.take() else {
            return;
        };
        if ask == Ask::SaveOnExit {
            match key {
                Key::Char('y' | 'Y') => {
                    self.ask = Some((Ask::FileName { exit: true }, self.default_name()));
                }
                Key::Char('n' | 'N') => self.quit = true,
                Key::Ctrl(b'c') => self.status = "[ Cancelled ]".into(),
                _ => self.ask = Some((ask, input)),
            }
            return;
        }
        match key {
            Key::Enter => match ask {
                Ask::FileName { exit } => {
                    if input.is_empty() {
                        self.status = "[ Cancelled ]".into();
                    } else if self.write(&input, ctx).await && exit {
                        self.quit = true;
                    }
                }
                Ask::Search => {
                    if !input.is_empty() {
                        self.last_search = input;
                    }
                    self.search();
                }
                Ask::SaveOnExit => {}
            },
            Key::Ctrl(b'c') => self.status = "[ Cancelled ]".into(),
            Key::Backspace => {
                input.pop();
                self.ask = Some((ask, input));
            }
            Key::Char(c) if input.len() < 4096 => {
                input.push(c);
                self.ask = Some((ask, input));
            }
            _ => self.ask = Some((ask, input)),
        }
    }

    async fn write(&mut self, name: &str, ctx: &Context<'_>) -> bool {
        let path = resolve_path(ctx.cwd, name);
        let text = self.text();
        match ctx.fs.write_file(&path, text.as_bytes()).await {
            Ok(()) => {
                self.path = Some(path);
                self.display_name = Some(name.to_string());
                self.modified = false;
                self.status = format!("[ Wrote {} lines ]", self.lines.len());
                true
            }
            Err(e) => {
                self.status = format!("[ Error writing {name}: {e} ]");
                false
            }
        }
    }

    fn search(&mut self) {
        let needle: Vec<char> = self.last_search.chars().collect();
        if needle.is_empty() {
            return;
        }
        let n = self.lines.len();
        for step in 0..=n {
            let r = (self.row + step) % n;
            let from = if step == 0 { self.col + 1 } else { 0 };
            let line = &self.lines[r];
            if let Some(pos) = (from..line.len().saturating_sub(needle.len() - 1))
                .find(|&i| line[i..].starts_with(&needle))
            {
                self.row = r;
                self.col = pos;
                if step == n {
                    self.status = "[ This is the only occurrence ]".into();
                }
                return;
            }
        }
        self.status = format!("[ \"{}\" not found ]", self.last_search);
    }

    // ---- editing ---------------------------------------------------------

    fn room(&mut self, extra: usize) -> bool {
        if self.bytes() + extra > MAX_BUFFER_BYTES {
            self.status = "[ Buffer is full ]".into();
            return false;
        }
        true
    }

    fn insert(&mut self, c: char) {
        if !self.room(c.len_utf8()) {
            return;
        }
        self.lines[self.row].insert(self.col, c);
        self.col += 1;
        self.modified = true;
    }

    fn newline(&mut self) {
        if !self.room(1) {
            return;
        }
        let rest = self.lines[self.row].split_off(self.col);
        self.lines.insert(self.row + 1, rest);
        self.row += 1;
        self.col = 0;
        self.modified = true;
    }

    fn backspace(&mut self) {
        if self.col > 0 {
            self.col -= 1;
            self.lines[self.row].remove(self.col);
            self.modified = true;
        } else if self.row > 0 {
            let line = self.lines.remove(self.row);
            self.row -= 1;
            self.col = self.lines[self.row].len();
            self.lines[self.row].extend(line);
            self.modified = true;
        }
    }

    fn delete(&mut self) {
        if self.col < self.lines[self.row].len() {
            self.lines[self.row].remove(self.col);
            self.modified = true;
        } else if self.row + 1 < self.lines.len() {
            let next = self.lines.remove(self.row + 1);
            self.lines[self.row].extend(next);
            self.modified = true;
        }
    }

    fn left(&mut self) {
        if self.col > 0 {
            self.col -= 1;
        } else if self.row > 0 {
            self.row -= 1;
            self.col = self.lines[self.row].len();
        }
    }

    fn right(&mut self) {
        if self.col < self.lines[self.row].len() {
            self.col += 1;
        } else if self.row + 1 < self.lines.len() {
            self.row += 1;
            self.col = 0;
        }
    }

    fn vertical(&mut self, delta: isize) {
        let max = self.lines.len() as isize - 1;
        self.row = (self.row as isize + delta).clamp(0, max) as usize;
        self.col = self.col.min(self.lines[self.row].len());
    }

    fn cut_line(&mut self, append: bool) {
        if !append {
            self.cut.clear();
        }
        let cut_bytes: usize = self.cut.iter().map(|l| l.len() + 1).sum();
        if cut_bytes + self.lines[self.row].len() < MAX_CUT_BYTES {
            let line = if self.lines.len() == 1 {
                std::mem::take(&mut self.lines[0])
            } else {
                self.lines.remove(self.row)
            };
            self.cut.push(line);
            self.row = self.row.min(self.lines.len() - 1);
            self.col = 0;
            self.modified = true;
        }
        self.last_was_cut = true;
    }

    fn paste(&mut self) {
        let extra: usize = self.cut.iter().map(|l| l.len() + 1).sum();
        if self.cut.is_empty() || !self.room(extra) {
            return;
        }
        for (i, line) in self.cut.clone().into_iter().enumerate() {
            self.lines.insert(self.row + i, line);
        }
        self.row += self.cut.len();
        self.col = 0;
        self.modified = true;
    }

    // ---- rendering -------------------------------------------------------

    /// Rows for text: everything but the title, status and two help rows.
    fn text_rows(&self) -> usize {
        usize::from(self.rows).saturating_sub(4).max(1)
    }

    fn display_col(line: &[char], col: usize) -> usize {
        let mut w = 0;
        for &c in line.iter().take(col) {
            w = if c == '\t' {
                (w / TAB_STOP + 1) * TAB_STOP
            } else {
                w + 1
            };
        }
        w
    }

    fn render(&mut self, tty: &Tty) {
        let cols = usize::from(self.cols);
        let text_rows = self.text_rows();
        if self.row < self.top {
            self.top = self.row;
        }
        if self.row >= self.top + text_rows {
            self.top = self.row + 1 - text_rows;
        }
        let cursor_x = Self::display_col(&self.lines[self.row], self.col);
        if cursor_x < self.left {
            self.left = cursor_x;
        }
        if cursor_x >= self.left + cols {
            self.left = cursor_x + 1 - cols;
        }

        let mut out = String::from("\x1b[?25l\x1b[H");
        // Title bar, reverse video.
        let title = format!(
            "  bashkit nano  {}{}",
            self.name(),
            if self.modified { "  Modified" } else { "" }
        );
        out.push_str("\x1b[7m");
        out.extend(format!("{title:<cols$}").chars().take(cols));
        out.push_str("\x1b[0m\r\n");
        for i in 0..text_rows {
            out.push_str("\x1b[2K");
            if let Some(line) = self.lines.get(self.top + i) {
                out.push_str(&visible(line, self.left, cols));
            }
            out.push_str("\r\n");
        }
        out.push_str("\x1b[2K");
        match &self.ask {
            Some((ask, input)) => {
                let label = match ask {
                    Ask::FileName { .. } => "File Name to Write: ",
                    Ask::SaveOnExit => "Save modified buffer?  Y Yes  N No  ^C Cancel",
                    Ask::Search => "Search: ",
                };
                out.push_str("\x1b[7m");
                out.extend(format!("{label}{input}").chars().take(cols));
                out.push_str("\x1b[0m");
            }
            None => out.extend(self.status.chars().take(cols)),
        }
        out.push_str("\r\n\x1b[2K");
        out.extend(HELP_ROW_1.chars().take(cols));
        out.push_str("\r\n\x1b[2K");
        out.extend(HELP_ROW_2.chars().take(cols));
        let (cy, cx) = match &self.ask {
            Some((Ask::SaveOnExit, _)) | None => {
                (self.row - self.top + 2, cursor_x - self.left + 1)
            }
            Some((ask, input)) => {
                let label = if *ask == Ask::Search {
                    "Search: "
                } else {
                    "File Name to Write: "
                };
                (
                    text_rows + 2,
                    (label.chars().count() + input.chars().count() + 1).min(cols),
                )
            }
        };
        out.push_str(&format!("\x1b[{cy};{cx}H\x1b[?25h"));
        tty.write(out.as_bytes());
        if self.ask.is_none() {
            self.status.clear();
        }
    }
}

/// The part of `line` from display column `left`, `cols` wide, with tabs
/// expanded and control chars in caret notation.
fn visible(line: &[char], left: usize, cols: usize) -> String {
    let mut cells = String::new();
    let mut width = 0;
    for &c in line {
        let mut cell = String::new();
        if c == '\t' {
            let next = (width / TAB_STOP + 1) * TAB_STOP;
            cell.extend(std::iter::repeat_n(' ', next - width));
        } else if (c as u32) < 0x20 || c == '\x7f' {
            cell.push('^');
            cell.push(((c as u8) ^ 0x40) as char);
        } else {
            cell.push(c);
        }
        for ch in cell.chars() {
            if width >= left && width < left + cols {
                cells.push(ch);
            }
            width += 1;
        }
        if width >= left + cols {
            break;
        }
    }
    cells
}

#[cfg(test)]
mod tests {
    use crate::Bash;
    use crate::terminal::Terminal;

    async fn run(term: &mut Terminal, input: &str) {
        term.send(input);
        term.run_until_idle().await;
    }

    async fn read(term: &Terminal, path: &str) -> String {
        let bytes = term.fs().read_file(path.as_ref()).await.unwrap();
        String::from_utf8(bytes).unwrap()
    }

    #[tokio::test]
    async fn edits_saves_and_exits() {
        let mut term = Terminal::new(Bash::builder());
        run(&mut term, "printf 'one\\ntwo\\n' > /tmp/f; nano /tmp/f\r").await;
        assert!(term.is_alternate_screen());
        let text = term.screen_text();
        assert!(text.contains("bashkit nano  /tmp/f"), "{text}");
        assert!(text.contains("^X Exit"), "{text}");
        // Type at the start of line 1, go to line 2's end, add a line.
        run(&mut term, "> \x1b[B\x1b[F\rthree").await;
        assert!(term.screen_text().contains("Modified"));
        // ^O asks for the name (pre-filled), Enter writes.
        run(&mut term, "\x0f\r").await;
        assert_eq!(read(&term, "/tmp/f").await, "> one\ntwo\nthree\n");
        run(&mut term, "\x18").await;
        assert!(!term.is_alternate_screen());
    }

    #[tokio::test]
    async fn exit_asks_to_save_and_cut_paste_moves_lines() {
        let mut term = Terminal::new(Bash::builder());
        run(
            &mut term,
            "printf 'a\\nb\\nc\\nd\\n' > /tmp/g; nano /tmp/g; echo rc=$?\r",
        )
        .await;
        // ^K cuts "a" and "b" (consecutive cuts append), move down, ^U pastes.
        run(&mut term, "\x0b\x0b\x1b[B\x15").await;
        run(&mut term, "\x18").await;
        assert!(term.screen_text().contains("Save modified buffer?"));
        run(&mut term, "y\r").await;
        assert!(!term.is_alternate_screen());
        assert!(
            term.screen_text().ends_with("rc=0\n$"),
            "{}",
            term.screen_text()
        );
        assert_eq!(read(&term, "/tmp/g").await, "c\na\nb\nd\n");
    }

    #[tokio::test]
    async fn exit_without_saving_and_new_file() {
        let mut term = Terminal::new(Bash::builder());
        run(&mut term, "nano\r").await;
        run(&mut term, "draft\x18n").await;
        assert!(!term.is_alternate_screen());
        // New named file: ^X, Y, name prompt is pre-filled.
        run(&mut term, "nano /tmp/new.txt\r").await;
        assert!(term.screen_text().contains("[ New File ]"));
        run(&mut term, "hi\x18y\r").await;
        assert_eq!(read(&term, "/tmp/new.txt").await, "hi\n");
    }

    #[tokio::test]
    async fn search_and_position() {
        let mut term = Terminal::new(Bash::builder());
        run(&mut term, "printf 'x\\nfind me\\n' > /tmp/s; nano /tmp/s\r").await;
        run(&mut term, "\x17me\r\x03").await;
        assert!(
            term.screen_text().contains("[ line 2/2, col 6/8 ]"),
            "{}",
            term.screen_text()
        );
    }

    #[tokio::test]
    async fn needs_a_terminal() {
        let mut bash = Bash::new();
        let r = bash.exec("nano /tmp/x").await.unwrap();
        assert_eq!(r.exit_code, 1);
        assert!(r.stderr.contains("not a terminal"));
    }
}