Hosho Documentation
The manual of Hosho, a C library that is a terminal without a display. It is written for a programmer who puts a terminal into a program. This page says what Hosho is, how its parts fit together, how to build and install it, and which page of the manual covers what.
1. What Hosho is
A terminal emulator does two jobs. It understands what a program writes (text, colors, cursor movements, hundreds of control sequences) and keeps the screen the program expects. And it shows that screen in a window and sends the user's keys back. Hosho does the first job completely and leaves the second to you. It runs a program on a pseudoterminal, reads what the program writes, and keeps the screen, the scrollback, the cursor and the modes. It needs only libc: no display, no toolkit, no threads. The program that draws the screen and sends the keys is called the host.
So a terminal can be a widget in any program, in any toolkit, or no window at all: a test that checks what a program draws, or a recorder. Hosho comes with one host ready to use: a terminal widget for X11 programs built on Xtk (demo/hosho_widget.c), which Unryu and Hanshi use.
- A real terminal. Hosho answers as a VT420 with color and does what xterm does: 256 and direct colors, every underline style, the alternate screen, scroll regions and left and right margins, rectangle functions, character sets, mouse reports in each form, bracketed paste, focus reports, the title stack, and the reports programs ask for. The value of
TERMa program gets isxterm-256color. - More than xterm. Text is wrapped again when the width changes. Hyperlinks (OSC 8), addresses and file names such as
src/main.c:120:8are links. The scrollback can be searched, with text or a pattern. Shell integration marks each prompt with its command's result. Pictures come as sixel, as iTerm2's inline images and by the Kitty graphics protocol. The Kitty keyboard protocol, notifications, a job's progress, a status line, printing and the DEC locator are there too. - Themes. A theme file gives each color and each setting of the display. A theme of three lines works: every key it leaves out takes a sensible fallback. An editor theme of Unryu is a terminal theme too.
- Safe with what programs send. A picture is the most complicated thing a program can send. A host can take every picture apart in a process of its own, in a jail, and the widget does.
Hosho is written from public standards and manuals (ECMA-48, the DEC VT100 to VT510 manuals, xterm's control sequences, the Unicode data files, the Kitty and iTerm2 protocol documents, RFC 1950 and 1951), not from the source of another terminal. HOSHO_VERSION in hosho.h is the library's version: this manual describes version 1.3.
2. The layers
Each layer is one header, and each header can be included by itself. hosho.h includes them all.
What a program writes goes through the pseudoterminal to the parser, which splits it into characters and control functions. The terminal does each one to the screen. The host draws the screen and does what the terminal asks of it (the host functions). What the user does goes through the input encoder to the program.
| Header | What it gives |
|---|---|
hosho_pseudoterminal.h |
A program, or the user's shell, started on a pseudoterminal: read and write that do not block, the size, the exit status. The arguments and variables that start a shell with Hosho's shell integration. |
hosho_parser.h |
The bytes a program writes, split into characters and control functions: C0 controls, escape sequences, control sequences with parameters and sub-parameters, OSC, DCS and APC strings. The input is UTF-8 and can come in pieces of any size. |
hosho_terminal.h |
What each control function does: the cursor, the modes, the colors, the reports, the title, and the functions a host gives the terminal (HoshoTerminalHost). See Control Functions. |
hosho_screen.h |
The grid of cells, the alternate buffer and the scrollback. Wide characters, combining marks, hyperlinks, pictures, the shell's marks, and a "changed" flag on each line for the display. |
hosho_width.h |
How many cells a character fills: 0, 1 or 2, from tables of the Unicode data files, so the answer is the same on every system. |
hosho_bidi.h |
The order in which a line with Hebrew or Arabic is shown, by the Unicode Bidirectional Algorithm. The terminal keeps the characters in reading order; a display uses this to show them. |
hosho_sixel.h, hosho_inflate.h |
A sixel picture as pixels, and data compressed with zlib made whole. The terminal uses them when the host does not take pictures apart itself. See Pictures. |
hosho_input.h |
A key, a mouse event, a focus change or a paste as the bytes the program reads, by the modes the program set. See Keyboard, Mouse and Paste. |
hosho_text.h |
Questions about the text: the text of a range, search with text or a pattern, the link at a cell, the lines of one command and its output, a range as HTML. |
hosho_theme.h |
Terminal themes: read a theme file, fill in what it leaves out, write it with every key, give its colors to a terminal. See Themes. |
demo/hosho_widget.h |
The terminal widget for X11 programs built on Xtk. It is not part of the library: a program compiles demo/hosho_widget.c with its own sources. See Embedding the Widget. |
The API pages of the wiki have one page for each of these headers, made from the comments in them: every type, constant and function, with what each parameter and return value means.
3. Building and installing
Hosho is built with make. The library needs a C compiler that understands -std=gnu17 (GCC or Clang) and a POSIX system with pseudoterminals (Linux, the BSDs). The build treats every warning as an error (-Wall -Wextra -Wpedantic -Wconversion -Wshadow -Werror).
| Command | What it does |
|---|---|
make |
Builds the static library libhosho.a and removes the object files. make KEEP_OBJS=1 keeps them. |
make demo |
Builds demo/hosho_demo, a terminal window made of the widget (see below). It is the only part that needs X11 and Xtk: Xtk's source folder, with its libxtk.a built, is looked for in ../xtk; make demo XTK_DIR=/path/to/xtk names another place. It also needs the development files of X11, Xrender, Xft, fontconfig, freetype, Xcursor, Xext and Xpm. |
make install |
Installs the headers to /usr/include/hosho/, the library to /usr/lib/, the four built-in themes to /usr/share/hosho/themes/ and the shell integration to /usr/share/hosho/shell/. PREFIX=/usr/local installs elsewhere, and DESTDIR=... stages the install for a package. |
make clean |
Removes the object files, the library and the demo. |
A program that uses the installed library compiles with -I/usr/include/hosho, includes "hosho.h" (or the headers it needs), and links with -lhosho. A program can also compile the files of src/ it needs straight into itself, as Unryu and Hanshi do. The widget is not installed: a program that uses it compiles demo/hosho_widget.c and links Xtk.
The Debian package (hosho_<version>_amd64.deb, on the project's release page) installs the same files with PREFIX=/usr.
4. The demo
demo/hosho_demo is a terminal window: one X11 window that holds the widget. It runs your shell, or the program named after its options; the window closes when the program ends. Its source, demo/hosho_demo.c, is a complete host and a good model for your own.
demo/hosho_demo # your shell
demo/hosho_demo vim src/hosho.h # one program
demo/hosho_demo -t Washi -r 40 -c 120 -f "DejaVu Sans Mono" -s 16
demo/hosho_demo -t my_theme.conf # a theme file, read again each time it changes
demo/hosho_demo -t Sumi -W # print the theme with every key, and end
demo/hosho_demo -d ls --color # print the last screen as text at the end
| Option | What it does |
|---|---|
-r rows, -c columns |
The window's size in characters, 24 by 80 when not given. |
-t theme |
The terminal theme: Hosho Dark (the default), Hosho Light, Sumi or Washi, a theme file, or xtk to take the colors of the Xtk theme. A theme file is read again when it changes, so a saved change shows at once. |
-f font, -s size |
The font family and its size in pixels, in place of the theme's. |
-T theme |
One of Xtk's built-in themes, for the scrollbar and the menu (Dark when not given). |
-W |
Writes the terminal theme with every key, and a line of words above each one, to standard output, and ends. |
-d |
When the program ends, prints the last screen as plain text. |
The demo's title follows the program, its window grows and shrinks by whole characters, it opens links with xdg-open, shows notifications with notify-send, marks its window for the window manager when the bell rings while it does not have the keyboard, and does the window functions a program asks for when the theme has window-ops = 1. It starts the program alone, with no shell integration; to try the integration with bash: demo/hosho_demo bash --rcfile shell/hosho_rcfile.bash.
5. The pages of the manual
-
A complete example program, the event loop, every function and callback of the widget, the menu items a host adds, and the jailed worker for pictures.
-
What the user can do in the widget: keys, mouse, selection, the menu, search, links, the keyboard selection, font size, and what the widget shows.
-
The library without the widget: a complete example, the event loop, reading and drawing the screen, the cursor, sizes, and the questions about the text.
-
Every function a host gives the terminal in
HoshoTerminalHost, and the settings and permissions a host gives the terminal. -
The theme file, each key with its fallback, the rules, the built-in themes, editor themes, and the functions that read and write themes.
-
What each key sends, the keyboards, modifyOtherKeys, the Kitty keyboard protocol, mouse and focus reports, paste, and the DEC locator.
-
The marks a shell sends, the file for each shell and how it is loaded, the shell's state, and what a host does with them.
-
Sixel, iTerm2's inline images and the Kitty graphics protocol; how pictures sit on the screen; taking them apart in a jail.
-
Each control function the terminal does: C0 controls, escape sequences, control sequences, modes, OSC, DCS and APC strings, and the reports.
Hosho API
The whole library
From bytes to a screen
For the host that shows it
The program
