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_strtake 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_LINEthe input line;TM_BUSYwhile a command runs (no input line; what the user types waits and comes back as oneSubmitper line afterwards; Ctrl+C still interrupts);TM_RAWevery key asTermEv::Raw;TM_SECRETa password without echo. - Long output:
pager(true), write it all,pager(false)-- the console shows a screen at a time withpager_text(" --More-- "): Space, Enter, q / Esc (TermEv::PagerQuit).page_lenfixes 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?givesTermEv::Helpinstead 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_TEXTrecord (expect text:"Router#" exists), the input line the text boxID_CON + 1(termtest:expect #1001 text "show "). Pasted lines and type-ahead follow one per 16 ms:wait 50before checking the later ones.