JFLib

23. Consoles: ui.terminal#

lib/ui/term.rs is a console for command lines (an IOS-like CLI, a Linux-like shell, a log): a scrollback ring with an ANSI subset, the input line on textbox_surface (IME, clipboard, selection), history, completion, a --More-- pager, busy mode with type-ahead and raw keys. The app keeps a TermState per console and answers its events; apps/termtest is the example (an IOS-like router console and a fake shell).

static MONO: &[u8] = include_bytes!("../../lib/doc_view/assets/DejaVuSansMono.ttf");

fn init(&mut self, ui: &mut Ui) -> bool {
    let f = ui.text.load_mem(MONO, 0);   // embed a monospace font (licence next to it)
    if f >= 0 { ui.mono_font_set(f as u32); }
    self.con.set_prompt("Router#");
    true
}

// in frame: the console fills its place; ids ID_CON .. ID_CON + 7
ui.next_size(-1, -1);
for ev in ui.terminal(ID_CON, &mut self.con, TF_QHELP | TF_CTRL_SHIFT6) {
    match ev {
        TermEv::Submit(line) => self.run(ui, &line),        // echoed and in the history
        TermEv::Help(line) => self.con.write_str(&help(&line)),
        TermEv::Complete { line, caret } => self.con.complete_reply(&complete(&line, caret), &[] as &[&str]),
        TermEv::Interrupt => self.stop_job(),
        _ => {}
    }
}
  • Output: write(&[u8]) / write_str take UTF-8 with SGR colours and attributes, \r, \t, backspace, ESC[K, ESC[2J (the screen scrolls clear), ESC[nC/D/G, BEL (a flash). The 16 colours come from the theme tokens, so they suit light, dark and high contrast.
  • Modes (set_mode): TM_LINE the input line; TM_BUSY while a command runs (no input line; what the user types waits and comes back as one Submit per line afterwards; Ctrl+C still interrupts); TM_RAW every key as TermEv::Raw; TM_SECRET a password without echo.
  • Long output: pager(true), write it all, pager(false) -- the console shows a screen at a time with pager_text (" --More-- "): Space, Enter, q / Esc (TermEv::PagerQuit). page_len fixes the lines of a page instead (IOS' terminal length: JFNetworker uses 23).
  • While busy or paging (no input line) a focused console still holds the keyboard like a text box: plain keys are no accelerators, and Ctrl+Z / Ctrl+Y stay with it, so the app's undo (ui.undo_keys, an accelerator) does not run.
  • Flags: TF_QHELP (a typed ? gives TermEv::Help instead of a character), TF_CTRL_SHIFT6 (Ctrl+Shift+6 interrupts), TF_NO_INPUT (output only), TF_NO_FRAME.
  • The user selects with the mouse (copy: Ctrl+C with a selection, Ctrl+Shift+C), searches with Ctrl+F, scrolls with the wheel / PgUp / PgDn; new output while scrolled up shows a "Neue Ausgabe" button. Pasting several lines submits each.
  • Tests: every visible line is a K_TEXT record (expect text:"Router#" exists), the input line the text box ID_CON + 1 (termtest: expect #1001 text "show "). Pasted lines and type-ahead follow one per 16 ms: wait 50 before checking the later ones.

Source: docs/GUIDE.md in the JFLib repository.