JFLib

ui/term.rs#

Source: lib/ui/term.rs

term -- a terminal / console widget: a scrollback ring with an ANSI subset, the input line on
textbox_surface (IME, clipboard, selection), history, completion, IOS-style `?` help, a
--More-- pager, busy mode with type-ahead, raw keys, mouse selection and search

  TermState::new(scrollback) -> st     a console (the app keeps one per console); scrollback
                                       = lines kept (at least 100)
  st.write(&[u8]) / st.write_str(s)    output: UTF-8 with an ANSI subset -- SGR 0 1 2 4 7 22
                                       24 27, colours 30-37 / 90-97 / 39, backgrounds 40-47 /
                                       100-107 / 49 (38;5 / 48;5 / 38;2 mapped to the 16);
                                       \r \n \t BS, BEL (the console flashes), ESC[K (0 1 2),
                                       ESC[2J (the screen scrolls clear), ESC[3J (the
                                       scrollback too), ESC[H, ESC[nC / nD / nG; OSC skipped
  st.clear()                           drop the scrollback and the current line
  st.set_prompt(s)                     the prompt before the input (SGR allowed)
  st.set_mode(TM_*)                    TM_LINE the input line; TM_BUSY a command runs: no
                                       input line, typed lines wait (type-ahead) and come
                                       back one Submit at a time in TM_LINE; TM_RAW every key
                                       goes to the app (TermEv::Raw); TM_SECRET a line typed
                                       without echo or history (passwords)
  st.input_set(s, caret)               replace the input line (caret: a byte index)
  st.complete_reply(line, list)        the answer to TermEv::Complete: the new line, and
                                       candidates listed in columns below the old one
  st.pager(on)                         on: the following output pages -- a screen minus one
                                       line (st.page_len lines when set, as "terminal
                                       length"), then pager_text (" --More-- "): Space a page,
                                       Enter a line, q / Esc / Ctrl+C TermEv::PagerQuit (later
                                       output is dropped until pager(false)); off: the app
                                       wrote everything (a held rest still pages)
  st.grid() -> (cols, rows)            the character grid of the last layout
  st.text() -> String                  scrollback and the current line as plain text
  ui.terminal(id, &mut st, TF_*) -> Vec<TermEv>   the console, filling the place it gets
                                       (natural 480 x 240 DIP; next_size(-1, -1) fills);
                                       ids id .. id + 7 (id + 1: the input line)
  ui.mono_font_set(font)               the monospace font of every console (an id from
                                       ui.text.load_mem; apps embed DejaVu Sans Mono)
  ui.mono_font(size_pt) -> (font, cell_w)   that font, else font_load("monospace"), else
                                       FONT_REG drawn cell by cell; the size is adjusted so
                                       that a cell is a whole number of pixels

Keys while focused: Enter submits (the line is echoed after the prompt and kept in the
history), Tab TermEv::Complete, Up / Down the history, Home / End; TF_QHELP: a typed `?` is
not inserted but gives TermEv::Help (the line up to the caret); Ctrl+C copies a selection,
else TermEv::Interrupt (echoes ^C); TF_CTRL_SHIFT6: Ctrl+Shift+6 interrupts too; Ctrl+D on an
empty line TermEv::Eof, Ctrl+Z on an empty line and Ctrl+L TermEv::Chord; Ctrl+U clears the
line before the caret; Ctrl+Shift+C copies; Ctrl+V / Ctrl+Shift+V paste (every pasted line
is one Submit); PgUp / PgDn (also with Shift), Ctrl+Up / Down, Ctrl+Home / End scroll;
Ctrl+F the search bar (F3 / Enter: the next older match, Shift+F3 / Shift+Enter: newer, Esc
closes it).  While busy or paging the console holds the keyboard as a text box would: plain
keys are no accelerators, the undo keys stay with it (the app's undo does not run).  Mouse:
a drag selects in the scrollback, a double click a word, the wheel scrolls; scrolled up,
new output shows a "Neue Ausgabe" button that jumps down.  Every
visible line is a K_TEXT record (`expect text:"..." exists`), the input line the K_TEXTBOX
id + 1.  Colours: a 16-colour palette from the theme tokens (light / dark).  Every character
takes one cell (wide CJK characters overlap the next); right-to-left the rows stay LTR.

The visible rows are drawn by two cached Draw objects (scrollback, current line + input),
replaced only when what they show changes, so typing redraws only the input rows.
/// TermState::mode: the input line
TM_LINE: u8 = 0
/// TermState::mode: a command runs -- no input line, typed text waits (type-ahead)
TM_BUSY: u8 = 1
/// TermState::mode: every key and character goes to the app (TermEv::Raw)
TM_RAW: u8 = 2
/// TermState::mode: a line typed without echo or history (passwords)
TM_SECRET: u8 = 3
TF_QHELP: u32 = 1  // a typed `?` asks for help (TermEv::Help, IOS)
TF_CTRL_SHIFT6: u32 = 2  // Ctrl+Shift+6 interrupts too (IOS)
TF_NO_INPUT: u32 = 4  // output only: no input line
TF_NO_FRAME: u32 = 8  // no border, a small padding
TK_KEY: u32 = 0x8000_0000  // TermEv::Raw: a key (VK | MOD_* << 16), not a character
  • enum TermEv what ui.terminal reports

    • Submit(String) -- Enter: the line (already echoed after the prompt and kept in the history)
    • Complete { line: String, caret: usize } -- Tab: the input line and the caret (byte index); answer with complete_reply
    • Help(String) -- TF_QHELP: ? was typed; the line up to the caret (echoed with the ?)
    • Interrupt -- Ctrl+C without a selection (TF_CTRL_SHIFT6: also Ctrl+Shift+6)
    • Eof -- Ctrl+D on an empty line
    • Chord(u32) -- Ctrl+Z on an empty line, Ctrl+L: VK | MOD_* << 16
    • PagerQuit -- the pager was quit (q, Esc, Ctrl+C)
    • Resize(u16, u16) -- the character grid changed (also at the first layout): columns, rows
    • Raw(Vec<u32>) -- TM_RAW: characters, and keys as TK_KEY | VK | MOD_* << 16; Enter 13, Tab 9, Backspace 127, Esc 27, Ctrl+letter 1..26
  • struct TermState the state of one console (see the module header); the app keeps it between frames

    • prompt: String -- the text before the input (SGR allowed)
    • mode: u8 -- TM_LINE, TM_BUSY, TM_RAW, TM_SECRET
    • flags: u32 -- TF_* added to the flags of ui.terminal
    • font: Option<u32> -- a font id for this console (None: ui.mono_font)
    • size_pt: f32 -- the text size in points (default 10)
    • pager_text: String -- the pager's line (default " --More-- ")
    • page_len: u16 -- lines of output per page of the pager (0, the default: the screen minus one line)

impl TermState

  • fn new(scrollback: usize) -> TermState a console keeping scrollback lines (at least 100)
  • fn write(&mut self, s: &[u8]) output: UTF-8 with the ANSI subset of the module header (paged while the pager is on)
  • fn write_str(&mut self, s: &str) write(s.as_bytes())
  • fn clear(&mut self) drop the scrollback and the current line (the history stays)
  • fn set_prompt(&mut self, s: &str) the prompt before the input (SGR allowed)
  • fn set_mode(&mut self, mode: u8) TM_LINE, TM_BUSY, TM_RAW or TM_SECRET; into or out of TM_SECRET the input typed so far is dropped (overwritten): a hidden line showed in the next mode's input line, a visible one became part of the secret
  • fn input_set(&mut self, s: &str, caret: usize) replace the input line; caret: a byte index (clamped to a character start)
  • fn input(&self) -> &str the input line as typed so far
  • fn history(&self) -> &[String] the lines submitted so far (oldest first)
  • fn complete_reply<S: AsRef<str>>(&mut self, line: &str, list: &[S]) the answer to TermEv::Complete: the new input line (caret at its end); a non-empty list is shown in columns below the line as typed
  • fn pager(&mut self, on: bool) on: the following output pages (see the module header); off: the app wrote all of it
  • fn pager_waiting(&self) -> bool the pager holds output and waits for a key
  • fn grid(&self) -> (u16, u16) the character grid of the last layout (columns, rows; 0 before the first)
  • fn text(&self) -> String the scrollback and the current line as plain text (lines joined with '\n')
  • fn line_count(&self) -> usize lines kept in the scrollback

impl Ui

  • fn mono_font_set(&mut self, font: u32) the monospace font of every console (a font id, e.g. from ui.text.load_mem)
  • fn mono_font(&mut self, size_pt: f32) -> (u32, i32) the consoles' monospace font at size_pt points -> (font id, cell width px): the font of mono_font_set, else the system's "monospace", else FONT_REG (drawn cell by cell)
  • fn terminal(&mut self, id: u32, st: &mut TermState, flags: u32) -> Vec<TermEv> ui_terminal(id, st, flags) -> events: the console (see the module header)

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