package matrix

All checked builds pass
  1. Overview
  2. Docs
Fast, modern terminal toolkit for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

mosaic-0.3.0.tbz
sha256=a92cbbcbe016fb19fb0a749222da0e459a9567aef3f4c7340d9bd6b71c53ed6f
sha512=6a7ab054605538bb31a2b73a1a6d093981a2704c9f0af5852221c05735290d74aca7c7282298c4d0f91e86a800e2ccd94b4918af883cd963449c758eb6b272b3

doc/matrix/Matrix/index.html

Module MatrixSource

The runtime of a terminal application.

Matrix holds a terminal while an application runs. It sets the terminal up, decodes its input, keeps time, and presents the frames the application draws on a Grid.t, writing only the cells that changed since the frame before.

A run (run) holds one terminal for the scope of a function and gives it back however that function ends. Inside it, the application asks for the next thing to do and does it. Each answer is a step (step): an input event, a timer that came due, a message another thread posted, or a frame to draw. The runtime decides the order and the timing of the steps, and the application decides what each one means. next waits for the next step. A program that waits with an event loop of its own takes the steps with step, which never waits, and waits for what fds and deadline say (The loop).

A run draws on the alternate screen, which it owns whole, or in `Primary mode on the normal screen, beside the shell (mode). There the screen's rows are a transcript, the shell's output and then the static rows the application writes (Static output), above a live region that the application redraws in each frame. The live region is drawn below a blank row of its own, its origin, which is a Screen.Region's, so that what the shell or another program writes, and the terminal's rewraps, never make a frame erase a row above the origin. A transcript row leaves the screen only by scrolling into the terminal's scrollback.

The runtime asks the terminal what it can do, and reads the answers among the input. The time the terminal took to answer the first batch of queries, the startup probe, is its round trip. The runtime waits for an answer at most max(500 ms, four round trips), the answer wait, and then goes on without it.

The life cycle of a run:

  1. run takes the terminal and calls the application's function with the run's app.
  2. The application calls next and acts on each step. It handles an event, a timer or a message, and asks for a frame (request_redraw) when what it shows has changed.
  3. At a Frame step it draws the frame (draw).
  4. It returns when it is done, or at the Stopped step, and run gives the terminal back.

Unless a function's doc says otherwise, a function that takes an app must be called on the thread that called run, and raises Invalid_argument on another (app). Times are integer nanoseconds (Time), and a grid's rows and columns count from 0.

This program counts the keys pressed and shows the count. It asks for a frame after each key, and ends at Escape or Ctrl+C, which raw mode makes a key (config's raw_mode):

let () =
  Matrix.run @@ fun app ->
  let rec loop count =
    match Matrix.next app with
    | Stopped | Event (Key { key = Escape; _ }) -> ()
    | Event (Key k) when Matrix.Input.Keystroke.is_interrupt k -> ()
    | Event (Key _) ->
        Matrix.request_redraw app;
        loop (count + 1)
    | Frame ->
        Matrix.draw app (fun grid ->
            Matrix.Grid.draw_text grid ~x:0 ~y:0
              ~text:(Printf.sprintf "%d keys" count));
        loop count
    | Event _ | Timer _ | Posted _ -> loop count
  in
  loop 0

Sub-libraries

Sourcemodule Ansi = Ansi

ANSI escape sequence generation.

Sourcemodule Grid = Matrix_grid

Cell-based drawing grid.

Sourcemodule Input = Matrix_input

Terminal input decoding.

Sourcemodule Screen = Matrix_screen

Double-buffered frame rendering.

Sourcemodule Terminal = Matrix_terminal

Terminal protocol state machine.

Sourcemodule Terminfo = Matrix_terminfo

Terminal descriptions from the terminfo database.

Sourcemodule Text = Matrix_text

Terminal text measurement and segmentation.

Sourcemodule Tty = Tty

The terminal device and the signals it sends.

Types

Sourcetype kitty_keyboard = [
  1. | `Auto
  2. | `Off
]

The type for kitty keyboard protocol configurations.

  • `Auto uses the protocol once the terminal answers the protocol's query, or from the start when config's caps says the terminal has it. The run pushes the flags that disambiguate keys and report alternate keys, or every flag with key_releases, then asks for the flags again so that capabilities says which hold. Without the protocol the run sets xterm's modifyOtherKeys, so that chords such as Ctrl+Alt with a letter stay distinct.
  • `Off never uses the protocol, and sets modifyOtherKeys.
Sourcetype mode = [
  1. | `Alt
  2. | `Primary
]

The type for presentation modes.

  • `Alt draws on the alternate screen, which the run owns whole. The shell's screen comes back when the run gives the terminal back.
  • `Primary draws the live region on the normal screen, below the transcript. Its origin is the cursor's row as the run begins, or the row below it when the cursor is past the first column, and each frame draws the live region from the origin, wherever the terminal has moved its row, without asking where that is. The live region has the rows the application states (config's min_tui_height, draw's primary_required_rows), as many as fit below the origin.

An application that states neither has the rows from the cursor's row at the start down to the terminal's last row. The first frame has one row, and the runtime then asks for a cursor position report, which says where the origin is. When the answer comes the live region takes those rows, scrolling one row above it into the scrollback for its origin. No frame waits for the answer. The runtime asks again after a resize or a SIGCONT, and the answer gives the live region the rows below its origin down to the last row, and after a release, the rows from the row below what the program wrote, as at the start. A report asked before a resize and answered after it gives no row, and a late one is never typed (Queries). An input that cannot answer, one that is not a terminal or not in raw mode, leaves the live region one row.

Events

Sourcetype event =
  1. | Key of Input.Keystroke.t
    (*

    A key pressed or auto-repeated.

    *)
  2. | Key_release of Input.Keystroke.release
    (*

    A key released, reported only when config's key_releases asks.

    *)
  3. | Text of string
    (*

    Text the terminal reports without a key, such as text an input method composed.

    *)
  4. | Mouse of Input.Mouse_report.t
    (*

    A mouse report. In `Primary mode its row counts from the live region's first row, and is -1 above the live region, and while the runtime does not know which row of the screen the live region's origin is on. It learns it from the cursor position report it asks for after each frame that may have moved the origin, as one does that writes static rows, and from static_clear, which puts the origin on the first row. Until the report comes, it counts the static rows the frame wrote, which a row the terminal wrapped makes wrong.

    *)
  5. | Paste_start
    (*

    A bracketed paste begins.

    *)
  6. | Paste_data of string
    (*

    Content of the paste, exactly as sent, in pieces as it arrives. The runtime keeps none of it and sets no limit on its length.

    *)
  7. | Paste_end of [ `Complete | `Cut ]
    (*

    The paste ends at its end marker (`Complete), or is cut by the end of the input, abort_paste or release.

    *)
  8. | Focus_in
    (*

    The terminal window gained focus.

    *)
  9. | Focus_out
    (*

    The terminal window lost focus.

    *)
  10. | Resize of int * int
    (*

    The usable size (size) changed to (cols, rows), the terminal's in `Alt mode and the live region's in `Primary mode. It comes just before the Frame drawn at that size, so a storm of resizes is one Resize. The terminal reports its size by the window-size signal of the process's controlling terminal or by its own report (mode 2048), and the runtime applies the latest report when it next draws. In `Primary mode the live region also changes as a frame requires rows (draw). That change comes as a Resize before the frame after the one that made it.

    *)
  11. | Color_scheme of [ `Dark | `Light ]
    (*

    The terminal's color scheme, in reply to a query or on a change. A terminal also reports its scheme when its user changes its palette, so a program that follows the palette asks for it again (query_palette).

    *)
  12. | Palette of Ansi.Palette.t option
    (*

    The answer to query_palette, the colors the terminal reported, or None if it reported no default foreground or no default background.

    *)
  13. | Invalid of string
    (*

    Bytes that are not UTF-8, one ill-formed subsequence.

    *)
  14. | Reply of Input.Reply.t
    (*

    A reply to the application's own queries, sent with query.

    *)

The type for what the terminal says to the application, decoded by Input.Decoder. A reply to one of the runtime's own queries is the runtime's. It reaches the application only as the fact it carries (Color_scheme, Palette), or when it comes late, among the replies to the application's own query (query).

Sourceval pp_event : Format.formatter -> event -> unit

pp_event formats an event for debugging.

Stop requests

Sourcemodule Stop : sig ... end

Ending a run from another thread, domain or fiber.

Running

A run takes the terminal whose input and output descriptors are given to run, by default the process's. A process may hold its controlling terminal (Tty.is_controlling) in one run at a time, and runs on other terminals, each in its domain, run side by side. A served session is a terminal the process runs for a client elsewhere, such as a remote shell's or a web page's. It runs on the slave side of a pseudo-terminal the program opened, with the client's environment as env, while the program copies bytes between the master side and the client. The runtime queries only a terminal, so a socket in its place would leave it guessing. A terminal that is not the process's controlling one sends the process no window signal, so the program writes the client's size to the master in band, CSI 48 ; rows ; cols ; height ; width t as mode 2048 reports it, before the run and on every change. An output that is not a terminal, such as a pipe, has no window either. The run then keeps the size last reported in band on its input, 80 by 24 before any.

Sourcetype config

The type for what a run sets up.

Sourceval config : ?mode:mode -> ?raw_mode:bool -> ?mouse:Terminal.mouse_mode -> ?bracketed_paste:bool -> ?kitty_keyboard:kitty_keyboard -> ?key_releases:bool -> ?fps:float -> ?explicit_width:bool -> ?min_tui_height:int -> ?caps:Terminal.capabilities -> ?color:Ansi.Styling.choice -> unit -> config

config () is the configuration of a run.

Display:

  • mode presentation mode. Defaults to `Alt.
  • raw_mode whether to switch the input into raw mode. Defaults to true. In raw mode the terminal sends its interrupt and suspend characters as keys, so Ctrl+C is a Key, and the application decides what it means. Without raw mode the terminal's settings stay as they are, so in the usual cooked mode input comes a line at a time and Ctrl+C sends SIGINT (Signals). The runtime then sends no query, and uses the kitty keyboard protocol only when caps says the terminal has it. With the protocol the terminal sends Ctrl+C as a key sequence even then, which cooked mode holds until a line ends, as it holds all input.
  • explicit_width whether to measure text with the built-in wcwidth tables instead of asking the terminal how it measures text. Defaults to false.
  • min_tui_height the fewest rows the live region has in `Primary mode, as far as the rows below its origin have them, which static rows never take (static_write). It holds from the start: size, effective_size and each Resize count it. A frame's primary_required_rows (draw) also gives the live region rows, but only from that frame on. By default the application states no height, the fewest rows are 1, and the live region takes the rows down to the terminal's last row (mode).
  • color the program's choice of styling, such as from a --color option. Defaults to `Auto. The run writes its frames at the styling Ansi.Styling.decide gives for color, the environment of run's env (NO_COLOR, FORCE_COLOR, CLICOLOR_FORCE and CLICOLOR), whether the output is a terminal, and the terminal's colors and dumb (Terminal.capabilities). So with NO_COLOR set a run writes its attributes, such as bold and reverse, without a color, a terminal of 16 colors gets 16, and `Never writes no style and no hyperlink.

Frames:

  • fps the frames per second of the frame grid (request_redraw). Defaults to 60.. The frame interval is 1 / fps seconds. 60 is the refresh rate of most displays. Frames for animations, timers, posted messages and streams of input come on the grid, one per frame interval at most. Frames asked for by discrete input come in the turn that read it, one a turn at most, so the turns bound them and fps does not.

Input:

  • mouse mouse tracking. Defaults to `Sgr_any. `Off leaves the mouse to the terminal.
  • bracketed_paste whether to enable bracketed paste. Defaults to true.
  • kitty_keyboard kitty keyboard protocol configuration. Defaults to `Auto.
  • key_releases whether to ask the kitty keyboard protocol for key releases (Key_release), which also has every key reported as a sequence. Defaults to false.
  • caps the capabilities to assume where the terminal does not answer, in place of what the environment says (Terminal.Caps.assume). By default run's env is read when the run begins. The styling is decided from env either way.

Focus reports (mode 1004), color scheme reports (2031) and resize reports (2048) are always set, without asking whether the terminal has them, since the terminals checked ignore a mode they do not know. The runtime resets them when it gives the terminal back.

The application draws everything its frames show, except the panel MATRIX_DEBUG has the run draw (Diagnostics), and sets the cursor's visibility (set_cursor).

Raises Invalid_argument if fps is not positive and finite.

Sourcetype 'msg app

The type for running applications whose other threads send them messages of type 'msg (post). An app exists only inside run, and belongs to the thread that called run. Every function of this module that takes one raises Invalid_argument on another thread, except post.

Sourceval run : ?input:Unix.file_descr -> ?output:Unix.file_descr -> ?env:Terminal.Caps.env -> ?stop:Stop.t -> ?config:config -> ('msg app -> 'a) -> 'a

run f takes the terminal whose input is input and whose output is output, calls f app, and gives the terminal back when f returns or raises. input defaults to Unix.stdin and output to Unix.stdout. env is the environment of the session the terminal runs, from which the runtime guesses what the terminal can do until it answers. It defaults to the process's, with the description the terminfo database has for its TERM (Tty.terminfo). stop ends the run from elsewhere (Stop). It runs on Device.unix of input, output and env, or on the device config names (Devices).

The run takes the terminal, and gives it back, as Taking and giving back the terminal says. It draws and dumps frames itself as the environment variable MATRIX_DEBUG says (Diagnostics).

Raises what f raises, and otherwise the first exception raised while giving the terminal back. Raises Invalid_argument if MATRIX_DEBUG names something other than a tool, or if the terminal is the process's controlling terminal and another run holds it, unless that run gave it away for release. Raises Unix.Unix_error if the run's wake cannot be made, and Failure on a Windows console that takes no virtual terminal input or on a Windows older than Windows 10 1803 (Tty.Signal.hold). On Windows the input is a console or a file: a run raises Invalid_argument at its first wait for a pipe's input, which Windows cannot wait for.

The loop

The application takes the run's work one step at a time. step gives the next step at the time the caller gives it, and never waits for one. When the run has nothing to do it returns None, and the caller waits until one of the descriptors fds is readable or the time deadline comes, then asks again. next is that loop, waiting with select(2). A program that waits with an event loop of its own, such as Eio's, calls step and waits with that loop instead. With nothing to wait for, deadline is None, so an idle application costs no wakeups.

Steps come from turns. A turn begins at the first step after None, and at the step after the work of the previous turn is done. A turn:

  1. takes the time of the step that began it as its own (now);
  2. acts on the signals received;
  3. reads the input once, without waiting, and decodes it, unless an event decoded earlier is still waiting, which leaves the rest in the operating system's buffer;
  4. takes the messages posted since the last turn;
  5. returns work, one step at a time. First comes one step of each kind that has some, in this order: an input Event, a Timer that came due, a Posted message. Then come the events, then the timers, then the posted messages, until none is left or one frame interval has passed between the turn's time and the latest step's. So a flood of one kind delays the others by at most a frame interval, and within a kind everything comes in order;
  6. returns Stopped once the input has ended and every event decoded before its end has been returned;
  7. returns a Frame if one was asked for and is due by the latest step's time (request_redraw; the first frame waits for no interval). Work that took the turn's budget may have taken the interval with it. Before the frame, if the usable size changed, comes a Resize.

Each step is done before the next is asked for, so the application handles one event at a time, and an event sees what the previous one did. A turn orders its steps by kind first. A key read in a turn comes before a timer that came due in the same turn, even if the timer's deadline was earlier. Neither the input nor the frames are held for an answer from the terminal.

Sourcetype timer = private int

The type for timers, set with at. Two timers set in one run differ.

Sourcetype 'msg step =
  1. | Event of event
    (*

    An input event, in the order the terminal sent them.

    *)
  2. | Timer of timer
    (*

    A timer whose deadline the turn's time reached.

    *)
  3. | Posted of 'msg
    (*

    A message another thread sent with post.

    *)
  4. | Frame
    (*

    Draw a frame, with draw. A frame not drawn by the next step is dropped.

    *)
  5. | Stopped
    (*

    The input ended, and every event decoded before its end has been returned; or the run's stop was requested (Stop). Every later step is Stopped.

    *)

The type for the steps of a run.

Sourceval step : 'msg app -> now:int -> 'msg step option

step app ~now is the next step of the run at the time now, or None when the run has nothing to do until one of fds is readable or the time deadline comes. It looks at the input without waiting, and reads and decodes what is there. It never waits for input, a post or a time. It blocks while the terminal takes no output, and while the process is stopped by SIGTSTP (Signals). now must be on the run's clock, and never earlier than the time of the step before (Time).

Raises Invalid_argument if now is earlier than the time of the step before.

Sourceval fds : 'msg app -> Unix.file_descr list

fds app is the descriptors the run waits for. Once one of them is readable, step has something to do. They are the input while it has not ended, and the run's wake, which post and the signal handlers write, and they may differ from one wait to the next. A caller must wait for them to be readable and must never read them, since what it reads the run never sees.

Raises Invalid_argument on Windows, where no descriptor stands for the console: drive the run with next, or with run on a domain of its own.

Sourceval deadline : 'msg app -> int option

deadline app is the time by which step must be called again after it returned None, if no descriptor of fds has become readable first, or None when only those descriptors can give the run something to do. Right after step app ~now returned None, it is None or later than now. It is computed when called, so a timer set (at) or a message posted (post) since moves it, and a message posted makes it the turn's time (now).

Sourceval clock : unit -> int

clock () is the run's own clock, Tty.now, in nanoseconds.

Sourceval next : 'msg app -> 'msg step

next app is the next step of the run, waiting for one: step at clock's time, waiting with select(2) whenever it returns None. It behaves as this loop does, but allocates nothing while the run is idle:

let rec next app =
  match Matrix.step app ~now:(Matrix.clock ()) with
  | Some step -> step
  | None ->
      let timeout =
        match Matrix.deadline app with
        | None -> -1.
        | Some d -> Float.max 0. (Float.of_int (d - Matrix.clock ()) *. 1e-9)
      in
      (try ignore (Unix.select (Matrix.fds app) [] [] timeout)
       with Unix.Unix_error (EINTR, _, _) -> ());
      next app

Time

Times are integer nanoseconds from an arbitrary origin, on a clock that never goes back, so a deadline computed as origin + n * interval is exact and two computations of it are equal. A run's time is the time of its steps. Timers compare with the time of the turn (now), and a turn's budget and the frame interval are measured to the time of the latest step. A step is a function of the run's state, its input and that time alone.

The times given to step must never decrease, and must be on the run's clock, clock, which next reads, and which the run reads itself as it takes the terminal, releases it and gives it back. A time from another clock, such as Unix.gettimeofday, makes the run's deadlines meaningless. A test that scripts time runs on the emulated terminal of Matrix_test (library matrix.test), whose clock the run takes as its own.

Sourceval now : 'msg app -> int

now app is the time of the current turn, in nanoseconds.

Sourceval at : 'msg app -> int -> timer

at app time is a timer that the run returns as Timer in the first turn whose time is at least time, the current turn included. A deadline is a lower bound, so the timer may come late and never comes early. Timers due in one turn come in deadline order, then in the order they were set.

Sourceval cancel : 'msg app -> timer -> unit

cancel app timer keeps timer from being returned. Cancelling a timer that was returned or cancelled does nothing.

Frames

Sourceval request_redraw : 'msg app -> unit

request_redraw app asks for a frame. Requests until that frame is drawn are one request. Frames come on a grid of points a frame interval apart (config's fps). A frame asked for once the grid's next point has passed comes in the turn that asked, and one asked for sooner comes at that point. A frame drawn less than an interval after its point leaves the next point an interval after this one, so an animation keeps fps however late each frame is drawn. A frame drawn an interval or more after its point, as after a pause, starts the grid again from the time it is drawn. A request made from a Frame step until the next step asks for the next frame on the grid, so an animation asks again from each frame, and stops by not asking.

A frame asked for while the run handles discrete input, a key pressed, repeated or released, text, a mouse button pressed or released, the end of a paste or a change of focus, comes in the turn that asked, after the turn's other work. A turn draws one frame at most and reads the input once, so input that arrives while a frame is drawn is read by the next turn and shown by its frame, however fast it comes. A frame that comes before the grid's next point is early (early), and leaves the grid where it was. The mouse's motion and wheel, a paste's data and a resize come in streams, and their frames come on the grid. So fps bounds the frames on the grid, and the turns that read discrete input bound the frames it asks for.

The runtime asks for frames itself: as the run begins, after a resize, a release or a SIGCONT, when the terminal's capabilities change, and when static rows are queued (static_write).

Sourceval frame_dt : 'msg app -> float

frame_dt app is the time in seconds from the frame before the latest Frame to that frame, measured between the times of the steps that returned them. It is 0. in the run's first frame. An animation moves by it in each frame. One that stopped asking for frames sees the whole pause in the first frame it asks for again.

Sourceval early : 'msg app -> bool

early app is true if the latest Frame is early, drawn before the grid's next frame, as a frame asked for by discrete input is (request_redraw). An animation that moves a step per frame, rather than by frame_dt, must not move in an early frame, or it runs faster while the user types.

Sourceval draw : 'msg app -> ?primary_required_rows:int -> (Grid.t -> unit) -> unit

draw app f draws the frame the last step returned. It lays the frame out at effective_size, calls f with the frame's grid, cleared, and presents the frame. Presenting writes the cells that differ from the frame presented before, in one write, and writes nothing when nothing changed.

In `Primary mode the static rows queued go to the transcript first, in the same write, and the live region grows to primary_required_rows rows when it has fewer, as many as fit below its origin, every row of the terminal but one, by scrolling the rows above it into the terminal's scrollback. It shrinks with the terminal, and as static rows take its rows (static_write). The frame is as tall as the live region, so it shows the rows it asked for. The first frame, and one that writes static rows, grows the live region, or follows a resize, a release, a SIGCONT or a change of the terminal's capabilities, draws the live region afresh. The origin and the rows below it are erased first, and the frame's rows are written down to the last that shows anything. Another frame writes the cells that changed. primary_required_rows is ignored in `Alt mode.

A frame drawn across a release is not presented, and the next one is drawn whole.

Raises Invalid_argument if no Frame is being drawn, and what dump_frame raises when MATRIX_DEBUG names frames (Diagnostics).

Sourceval current_grid : 'msg app -> Grid.t

current_grid app is the grid of the frame presented last, blank before the first. The application must not change it, since the next frame is diffed against it.

Sourceval hits : 'msg app -> Screen.Hit_grid.t

hits app is the hit grid of the frame being drawn (Screen.Hit_grid).

Sourceval screen : 'msg app -> Screen.t

screen app is the runtime's screen. The runtime sizes and presents it: draw clears and resizes it before calling its function, and diffs and swaps it after. An embedding renderer builds its frames into it (Screen.build) from the function given to draw, so that the runtime presents its output with one diff. The application must not call Screen.render on it.

Threads

Sourceval post : 'msg app -> 'msg -> unit

post app msg has the run return Posted msg in a later turn. It is the one function of this module that any thread, domain or fiber may call, and it never fails. Posts come in the order they were made, those of one thread in program order. After the run ends, post does nothing.

A post is pending from when it is made until the run returns it. On the loop's thread post never blocks, nor in a run on a terminal that Matrix_test emulates in the process, whose steps the test decides. On another thread, while 65,536 posts are pending, it blocks until the loop has taken half of them or the run has ended, so a producer faster than the loop keeps pace with it and the posts pending stay bounded. It blocks the whole thread, with every fiber or promise that thread runs, and lets the other threads of its domain run meanwhile. The loop's thread must not wait for a thread that posts, as by joining it, while it takes no steps, since the two could wait for each other forever.

Taking and giving back the terminal

Taking the terminal is a sequence of steps, each of which pushes its undo: the signals (Signals), raw mode, the startup probe, the screen (the alternate screen, or in `Primary mode the live region), the measure of how the terminal sizes text (Terminal.Caps.batch.Widths), and the modes. The probe takes at most an answer wait, and input that comes meanwhile is kept and returned first by next. Only a terminal that answered the probe is measured, on cells the run owns and then erases: the alternate screen's second row, or in `Primary mode the first of two rows drawn below the origin, on a terminal of at least three rows. Two rows are the height of text drawn at scale 2 by kitty's text-sizing protocol. If a step raises, the undos made so far run in reverse and the exception propagates with its backtrace.

Giving the terminal back runs every undo, each even if another raises. When a reply is owed, it first waits at most an answer wait for the terminal to answer, so that no reply reaches the shell as typing. Then the modes are reset (mouse tracking, bracketed paste, the keyboard protocols, and focus, color scheme and resize reports), the cursor shown, the screen left, cooked mode restored, and the input not read discarded. In `Primary mode leaving the screen writes the static rows queued, then, at the run's end, leaves its last frame, unless the program said otherwise (keep_frame), and, for a release, erases the live region, with the cursor at the start of its origin, below the transcript.

A run gives its terminal back on every way out: when the function given to run returns or raises, for a program run in its place (release), on a signal that stops or ends the process (Signals), and on the process's exit path. If the process exits while a run holds its terminal, as when that function calls exit, or when the program's main domain returns while a run on another domain is still taking its terminal or giving it back, the exit path restores the terminal as an abort does. It writes the bytes that reset the modes the run set and puts back the terminal's saved settings. A terminal given back already is left alone.

Sourceval release : 'msg app -> (unit -> 'a) -> 'a

release app f gives the terminal back for f (), as at the end of the run, and takes it again when f returns or raises, as at its start. f can run another program in the terminal, as POSIX's system() does for its caller. The loop does not run during f, so nothing reads the terminal, and timers and posts wait for f to return.

Before f, the static rows queued are written first. Input not yet read stays for f, and in `Primary mode the live region is erased and the cursor waits at the start of its origin, below the transcript, where f's output goes on.

The events decoded and not yet returned, and those decoded while waiting for the replies owed, are dropped, but for Reply and Palette, and a paste in progress is cut. An ask of the palette still owed is answered None. A turn decodes all it reads before it returns a step, so this drops the keys read in the turn of the step that called release, whatever that step was. Those keys were typed after the event that called release, or at a time that cannot be ordered against the timer or the post that did, so they count as typed for f.

During f, SIGTSTP, SIGTERM and SIGHUP have their default actions, so the terminal's suspend character stops f's program and the runtime with it. SIGINT and SIGQUIT, which the terminal sends to f's program as well, are caught, and discarded when f returns.

After f, the terminal is taken again. Input left unread is discarded, the terminal's settings are kept as the ones to restore, the size is read, the screen and modes are set again, and the next frame is drawn whole, in `Primary mode from a row of its own below what f wrote, a line f left without its end included. A frame being drawn when release is called is not presented.

Raises what f raises, and Invalid_argument if called from the f of another release of the same run.

Sourceval set_mouse : 'msg app -> Terminal.mouse_mode -> unit

set_mouse app mode sets the mouse tracking app keeps to mode, as config's mouse sets it at the start, and applies it now if app holds the terminal. It holds when app gives the terminal back and takes it again, as around release and stop_job. With `Off, the terminal keeps the mouse, and selects its own cells.

Sourceval keep_frame : 'msg app -> bool -> unit

keep_frame app keep says what a run in `Primary mode leaves of its live region when it gives the terminal back at its end. With keep true, as by default, the rows of the frame it presented last that show anything stay as the terminal shows them, pictures included, moved up a row onto the origin's, with the cursor at the start of the row below them. With false, the live region is erased, with the cursor at the start of its origin. The run reads it as it gives the terminal back, so the program says it before it ends, and it holds however the run ends. It changes nothing in `Alt mode, nor in a release, which erases the live region.

Sourceval stop_job : 'msg app -> unit

stop_job app stops the job as the terminal's suspend character does in cooked mode. It releases the terminal around sending SIGTSTP to the process's group, and returns once the job is continued. Raw mode turns the suspend character into a key, so an application that wants Ctrl+Z to suspend calls stop_job on it. Nothing happens if SIGTSTP was ignored when the run began, as nothing could continue the job, or if the run's terminal is not the process's controlling terminal (Tty.is_controlling), since the job is not that terminal's and stopping it would stop every other session. Nothing happens on Windows, which has no job control.

Signals

A run holds its terminal's signals as Tty.Signal says every holder of a terminal in the process does. A run on any terminal takes the signals that end the process, SIGINT, SIGTERM, SIGHUP, SIGQUIT and SIGABRT, and a run on the process's controlling terminal (Tty.is_controlling) also takes SIGTSTP, SIGWINCH and SIGCONT. All but SIGWINCH and SIGCONT are taken only if their action is the default when the run begins, as X/Open Curses allows, so an application that installed its own action keeps it. The handlers record the signal for each run it concerns and wake it (its wake is among fds), and the loop acts on it in its next turn. Runs side by side take a signal together, and the action it had comes back when the last of them ends.

  • SIGINT, SIGTERM, SIGHUP and SIGQUIT end the process, and every run with it. The run whose loop acts on the signal first gives its terminal back, as when it ends. Every other run's terminal is restored as an abort restores it (below), its reset bytes written and its saved settings put back. Then the signal is raised again with its default action, so the process ends with that signal's status and run does not return. An application that wants run to return installs its own handler first. A write that the terminal is not taking stops when one of them comes, with the rest unwritten.
  • SIGTSTP releases the terminal and stops the process, as stop_job does for the whole job. On continuing, the terminal is taken again.
  • A SIGCONT the run did not cause, as when the shell continued the job after a stop from elsewhere, sets the terminal again, since the shell has put back its own settings. The size is read and the next frame drawn whole, in `Primary mode from a row of its own below what the shell wrote.
  • SIGWINCH has the size read when the next frame is drawn.
  • SIGABRT, from abort(), returns to no point where OCaml can run, so its handler restores every run's terminal itself. It writes the bytes that reset the modes the run set to the run's terminal, puts back its saved settings, and the process aborts.

Pastes

Sourceval abort_paste : 'msg app -> unit

abort_paste app ends the bracketed paste in progress, if any, with Paste_end `Cut. The bytes after it decode as input again. A paste goes on until its end marker, so after a terminal that stopped sending mid-paste, every later byte would be the paste's content. An application that will wait no longer for the marker calls abort_paste.

Queries

A terminal answers a query with a reply among its input. The runtime sends its queries, and the application's, in batches. A batch is owed from when it is sent until the terminal has answered it or, from a terminal that never does, for max(5 s, four round trips). While a batch is owed the decoder reads its replies as replies, and a reply that comes after that decodes as input. A batch stays owed after the runtime has stopped waiting for its answer.

An application's query sent other than by query, such as with Terminal.send, is not owed. Its reply is never a Reply, and one that is a control string, or a cursor report from the first row, reaches the application as input. The replies query delivers are those the terminal sends after it has answered the queries sent before, so they may include the late answer to a query the runtime itself sent. A report the terminal sends on its own that is a fact for the application comes as its event (Color_scheme).

Sourceval query : 'msg app -> owed:Input.Decoder.owed -> string -> unit

query app ~owed q sends the queries q to the terminal, followed by a primary device attributes query (DA1), and delivers their replies as Reply events, ending with the DA1 reply. owed says which of the replies q asks for are cursor position reports or control strings (DCS, OSC and APC replies), as Input.Decoder.expect counts them. The decoder must know that they are coming, or a string reply such as XTVERSION's DCS > | text ST decodes as Alt+P and typed text, and a cursor report CSI 1 ; m R as F3. A terminal answers in order and answers DA1, so its DA1 reply says that q has been answered, or never will be, as for the runtime's own queries. Every DA1 reply is alike, so one batch that ends with DA1 is in flight at a time, and q is sent once the terminal has answered the batches sent before it, or they have been given up.

Nothing is sent, and no Reply comes, when the input is not a terminal in raw mode, or it has ended, since no reply could come.

Sourceval query_palette : 'msg app -> unit

query_palette app asks the terminal for its palette (Ansi.Palette): its default foreground and background, its cursor color and entries 0 to 15 (OSC 10, 11, 12 and 4), in one batch that ends with DA1. Entries 16 to 255 are not asked for, and stay unknown, since the colors a program derives from a palette, such as the legible entries of Ansi.Palette.legible, are entries 0 to 15. A Palette event answers it once the terminal has answered the batch. It holds the colors the terminal reported, and a color it did not report is unknown. It is None when the terminal reported no foreground or no background, when it never answers, and, at the next step, when the input is not a terminal in raw mode or has ended. An ask made while another waits to be sent shares that one's answer. The replies are the runtime's, and never come as Reply events.

The terminal

Sourceval mode : 'msg app -> mode

mode app is the presentation mode of the run.

Sourceval size : 'msg app -> int * int

size app is the usable size (cols, rows), the terminal's in `Alt mode and the live region's in `Primary mode.

Sourceval full_size : 'msg app -> int * int

full_size app is the terminal's size (cols, rows), which is size in `Alt mode. In `Primary mode the live region has at most rows - 1 rows, the other being its origin's, and on a terminal of one row it shows none.

Sourceval effective_size : 'msg app -> int * int

effective_size app is the usable size (cols, rows) that draw will lay the next frame out at, once the static rows queued have taken theirs from the live region (static_write), and before it grows the live region to the rows the frame requires. It is size in `Alt mode, when no static row is queued, and so in the function given to draw.

Sourceval pixel_resolution : 'msg app -> (int * int) option

pixel_resolution app is the size in pixels (width, height) of the terminal's text area, as the terminal last reported it, or None before any report.

Sourceval cell_size : 'msg app -> (int * int) option

cell_size app is the size in pixels (width, height) of one of the terminal's cells, as it answered CSI 16 t, or else its text area's size (pixel_resolution) or its window's size as the system reports it, divided by its columns and rows. It is None when none is known.

Sourceval terminal : 'msg app -> Terminal.t

terminal app is the terminal handle the run writes through.

Sourceval capabilities : 'msg app -> Terminal.capabilities

capabilities app is what the run knows the terminal can do, which changes as the terminal answers.

Sourceval styling : 'msg app -> Ansi.Styling.t

styling app is the styling the run writes its next frame at, decided as config's color says from the run's environment and capabilities: `Plain writes no style, `Mono the attributes without color, and a color depth each color reduced to it. A program that chooses its colors by what the output shows, such as a theme with fewer colors for a terminal of 16, reads it here rather than deciding again from the environment, where it could disagree with the run. It changes as capabilities does, such as when a terminal's answer says it shows 24-bit color, and the frame after the change is written at the new one.

Static output

In `Primary mode the application adds rows to the transcript, above the live region, with these functions. The rows go out with the next frame draw lays out, which they ask for, in the same write. Each is a line the terminal prints as any output. It wraps where it is wider than the terminal it reaches, and a terminal that rewraps its lines on a resize rewraps it. The rows take theirs from the live region, which moves down until it has its fewest rows (config's min_tui_height). After that the rows above the live region scroll into the terminal's scrollback, and the live region keeps its height. Only static_replace and static_clear erase transcript rows. In `Alt mode these functions do nothing and raise nothing.

Sourceval static_write : ?preserve_live_region:bool -> ?rows:int -> 'msg app -> Grid.t -> unit

static_write ~preserve_live_region ~rows app grid queues the first rows rows of grid for the transcript, every row by default, blank rows included. It copies them immediately, so later changes to grid do not change them. Each row is written as a line, up to its last cell that shows something or whole if it holds a picture cell, on a row the frame erased, in the colors the terminal shows. A row wraps on a terminal that narrowed before it was written. Picture cells are written in the terminal's form of pictures (Pictures), under sixel with a sixel over the rows still on the screen. A row queued from the function given to draw goes out with the frame after that one. rows = 0 queues nothing.

With preserve_live_region true, the live region keeps its height, and the transcript, these rows included, scrolls up into the scrollback to make room. Otherwise, by default, the rows take theirs from the live region, down to min_tui_height.

Raises Invalid_argument, in `Primary mode, if grid is wider than the terminal, or rows is negative or greater than the height of grid.

Sourceval static_replace : ?preserve_live_region:bool -> ?rows:int -> 'msg app -> Grid.t -> unit

static_replace ~preserve_live_region ~rows app grid queues the replacement of the whole transcript with the first rows rows of grid, taken as static_write takes them, and drops the rows queued before. rows = 0 empties the transcript. The erase, the replacement and the next frame go out in one write, which draws the live region below the replacement from the screen's first row. With no height stated, the live region then has the screen's rows below its origin, but those the replacement takes.

Warning. The replacement erases the terminal's scrollback (CSI 3 J), the shell's output from before the run included. A terminal without CSI 3 J keeps its scrollback.

Raises as static_write does.

Sourceval static_clear : 'msg app -> unit

static_clear app erases the screen and drops the static rows queued. It writes the erase immediately, and asks for the next frame, which draws the live region below an origin on the screen's first row, and, with no height stated, gives it the screen's other rows. The scrollback stays.

Scroll hints

Sourceval set_scroll_hint : 'msg app -> Screen.scroll_hint -> unit

set_scroll_hint app hint tells the frame being drawn, or the next, that a region of it scrolled (Screen.scroll_hint). Presenting the frame then scrolls the terminal's rows (DECSTBM) and diffs only the rows the scroll revealed. Without a hint, a scrolled region rewrites every row it shows. Hints are used in `Alt mode and dropped in `Primary mode.

Cursor

The cursor's setters take effect with the next frame draw presents, and hold until set again. A run begins with the cursor hidden.

Sourceval set_cursor : ?visible:bool -> ?style:Ansi.cursor_style -> 'msg app -> unit

set_cursor ~visible ~style app sets the cursor's visibility and style, each when given. An application that wants the cursor shown from the first frame sets it before that frame:

Matrix.run @@ fun app ->
Matrix.set_cursor app ~visible:true;
loop app
Sourceval set_cursor_style : 'msg app -> style:Ansi.cursor_style -> blinking:bool -> unit

set_cursor_style app ~style ~blinking sets the cursor's shape and whether it blinks.

Sourceval set_cursor_position : 'msg app -> row:int -> col:int -> unit

set_cursor_position app ~row ~col places the cursor at row row and column col of the usable area (size), counted from 1, unlike a grid's rows and columns. row is clamped to the usable rows and col to at least 1. In `Primary mode a cursor shown there rests there between frames, so a line another program writes meanwhile lands on the live region's rows, which the next frame drawn afresh erases. A hidden cursor rests on the live region's origin.

Sourceval set_cursor_color : 'msg app -> r:float -> g:float -> b:float -> unit

set_cursor_color app ~r ~g ~b sets the cursor's color. Each component is clamped to [0.;1.].

Diagnostics

Debug_overlay draws the frames' timing and the collector's statistics in a corner of a frame, and dump_frame writes a frame to files. An application uses them in the frames it draws. A run on descriptors also uses them itself when the environment variable MATRIX_DEBUG, read as the run begins, names them in a list separated by commas:

  • overlay draws a Debug_overlay over each frame, after the function given to draw. It times the frame from the start of draw to the end of its write. Its app row is the time since the frame before that the application spent handling steps, outside step, next and draw. The waits for a step are left out of it.
  • frames or frames=DIR dumps each frame drawn into the directory DIR, "frames" by default, with its hit grid (dump_frame). The dump is taken before the overlay is drawn, so it holds the application's frame alone.

An empty MATRIX_DEBUG names no tool. The overlay's cells are part of the frames the run presents and of their metrics. A run on a test's terminal, such as a Matrix_test session's, reads no MATRIX_DEBUG, so that its frames depend only on the test. Any program shows the panel and keeps every frame it draws, its code unchanged:

MATRIX_DEBUG=overlay,frames=/tmp/frames ./app.exe

An application that dumps every thirtieth frame counts them:

let frames = ref 0 in
let on_frame app view =
  incr frames;
  Matrix.draw app (fun grid ->
      view grid;
      if !frames mod 30 = 0 then Matrix.dump_frame ~dir:"dumps" app)
in
on_frame

While a run holds the terminal, the program must write to it only through the run. Bytes written to the terminal by other means, such as by Printf.printf or by a library's message on standard error, land in the frame where the run does not know they are, and stay on the screen until the cells under them change. A program that prints diagnostics while it runs must write them to standard error, and be run with standard error redirected to a file, which another terminal follows:

./app.exe 2>app.log
tail -f app.log

In `Primary mode those bytes land where the cursor rests, at the start of the live region's origin unless the application shows the cursor elsewhere. The next frame drawn afresh erases what they leave there without a line end, and until then a frame writes its changed cells a row lower for each line they ended. The program can also write rows above the live region with static_write.

Sourcemodule Debug_overlay : sig ... end

A panel of a run's frame timing and memory, drawn in a corner of a frame.

Sourceval dump_frame : ?hits:bool -> ?dir:string -> ?pattern:string -> 'msg app -> unit

dump_frame app writes the frame being drawn, as the function given to draw has drawn it so far, to a new file of dir (default "frames", made if missing), named pattern (default "frame_") followed by a number that grows with each dump of the process and .ansi. The file holds the frame's cells as ANSI text, a file named with .txt in place of .ansi holds them as plain text (Grid.to_text) and, if hits (default false), a file named with .hits.txt holds its hit grid.

Raises Invalid_argument if no Frame is being drawn, Failure if dir cannot be made, and Sys_error if a file cannot be written.

Devices

A device (Device.t) is the terminal a run drives as the runtime sees it: bytes in and out, a clock, a way to wait for input or a time, the window's size, the terminal's settings, and the signals of its session. A run decodes, negotiates, schedules and draws; a device moves bytes and waits. run drives a device over its descriptors, Device.unix, so a program needs a device of its own only where its terminal is not a pair of descriptors of the process:

  • a server that runs a program for each remote session, as wish serves Bubble Tea programs over SSH, writes the session's channel, reads its input, and reports its window changes as Winch and its end as Terminate;
  • an embedding that shows a program in a terminal of its own, emulated in the process, such as a pane of a multiplexer or Matrix_test's sessions;
  • a test of the runtime that scripts the clock, the input, the answers and the signals.

A program runs on a device with on_device: run ~config:(on_device d c) runs as run ~config:c does, on d. A device wraps another by record update, such as Device.unix with the size a transport reports.

Sourcemodule Device : sig ... end
Sourceval on_device : Device.t -> config -> config

on_device d c is c for a run on d. run given it takes no descriptors and no environment, which are d's, and reads no MATRIX_DEBUG (Diagnostics).

run raises Invalid_argument if it is given descriptors or an environment with such a configuration.