1 hosho_theme
liquibyte edited this page 2026-11-19 16:23:30 -06:00

hosho_theme.h

Terminal Themes: Every Color and Setting of the Display.

Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.

A terminal theme gives a color to each thing a host draws in a terminal, and holds the settings of the display. Each one is an item with a key in a section of the theme file. A theme can leave keys out: each item has a fallback, so a theme of three lines works, and hosho_theme_write() writes every key, so a theme file shows all that a user can change.

Features:

  • [theme]: name, description
  • [terminal]: colors of the view: background, text, selection, cursor, links, the margin of the shell's marks, a prompt's line, a failed command's output, the matches of a search
  • [palette]: the 16 colors programs name: black to bright-white
  • [attributes]: a style for text a program marks bold, faint, italic, underlined or blinking: "foreground background [bold] [italic]"
  • [rules]: a style for text in a program's output that a pattern finds: "foreground background [bold] [italic] /pattern/", with an i after the last / when case does not matter
  • [terminal-widget]: font, cursor shape and blink, padding, the margin's width, space between lines, lines of scrollback, bold as bright, whether a program can read or set the clipboard, the keyboard, the answerback message, the question before a paste, ligatures, the bell, a least contrast, a notice when a long command ends, a log file, a background picture, the window functions and a print command
  • An editor theme of Unryu is a terminal theme too: each view color its [terminal] section leaves out comes from its [editor] section when that has the color, so the terminal looks as the editor does
  • Built-in themes, each a few lines the fallbacks complete

Internal design notes:

  • No display here: colors are 0xRRGGBB
  • One table (hosho_theme.c) gives each item its section, key, kind and the words written above it; the enum below is in the table's order
  • Each color key that is a background has a key for the text on it ("selection-text"). "-" for such a text key keeps each cell's own color
  • hosho_theme_resolve() fills each item a theme left out, in an order where each fallback comes after what it is made from. The palette's fallback is one of two sets, by whether the background is dark or light
#include "hosho_theme.h"

Types: HoshoThemeKind, HoshoThemeItem, HoshoThemeStyle, HoshoThemeSettings, HoshoThemeRule, HoshoThemeEditorColors, HoshoTheme

Constants: HOSHO_THEME_RULE_LIMIT, HOSHO_THEME_BUILTIN_COUNT, hosho_theme_builtin_names

Functions: hosho_theme_init, hosho_theme_parse, hosho_theme_read, hosho_theme_builtin, hosho_theme_resolve, hosho_theme_write, hosho_theme_apply, hosho_theme_item_section, hosho_theme_item_key, hosho_theme_item_kind

Types

HoshoThemeKind

What an item holds: one color, or a style (two colors, bold, italic).

Name Value Description
HOSHO_THEME_KIND_COLOR one color, in foreground
HOSHO_THEME_KIND_STYLE foreground, background, bold, italic

HoshoThemeItem

Each color and style of a theme, in the order of the file.

Name Value Description
HOSHO_THEME_BACKGROUND the view's background
HOSHO_THEME_TEXT text with no other color
HOSHO_THEME_SELECTION the selection's background
HOSHO_THEME_SELECTION_TEXT text in the selection; "-": each cell's own color
HOSHO_THEME_CURSOR the cursor
HOSHO_THEME_CURSOR_TEXT the character under a block cursor; "-": the cell's background
HOSHO_THEME_LINK a hyperlink
HOSHO_THEME_GUTTER the margin where the shell's marks are drawn
HOSHO_THEME_PROMPT_MARK the mark of a prompt whose command still runs or gave no result
HOSHO_THEME_PROMPT_SUCCESS the mark of a prompt whose command ended with status 0
HOSHO_THEME_PROMPT_FAILURE the mark of a prompt whose command ended with another status
HOSHO_THEME_PROMPT_LINE the background of a prompt's line; none: not drawn
HOSHO_THEME_PROMPT_LINE_TEXT the text on it; "-": each cell's own color
HOSHO_THEME_OUTPUT_FAILURE the background of the output of a command that failed; none: not drawn
HOSHO_THEME_OUTPUT_FAILURE_TEXT the text on it; "-": each cell's own color
HOSHO_THEME_FOLD the words in place of a command's hidden output
HOSHO_THEME_SEARCH_MATCH under each match of a search
HOSHO_THEME_SEARCH_MATCH_TEXT the text of each match; "-": each cell's own color
HOSHO_THEME_SEARCH_CURRENT under the match the search is at
HOSHO_THEME_SEARCH_CURRENT_TEXT its text; "-": each cell's own color
HOSHO_THEME_BLACK color 0
HOSHO_THEME_RED color 1
HOSHO_THEME_GREEN color 2
HOSHO_THEME_YELLOW color 3
HOSHO_THEME_BLUE color 4
HOSHO_THEME_MAGENTA color 5
HOSHO_THEME_CYAN color 6
HOSHO_THEME_WHITE color 7
HOSHO_THEME_BRIGHT_BLACK color 8
HOSHO_THEME_BRIGHT_RED color 9
HOSHO_THEME_BRIGHT_GREEN color 10
HOSHO_THEME_BRIGHT_YELLOW color 11
HOSHO_THEME_BRIGHT_BLUE color 12
HOSHO_THEME_BRIGHT_MAGENTA color 13
HOSHO_THEME_BRIGHT_CYAN color 14
HOSHO_THEME_BRIGHT_WHITE color 15
HOSHO_THEME_BOLD text a program marks bold
HOSHO_THEME_FAINT text a program marks faint
HOSHO_THEME_ITALIC text a program marks italic
HOSHO_THEME_UNDERLINE text a program underlines
HOSHO_THEME_BLINK text a program marks as blinking
HOSHO_THEME_ITEM_COUNT how many items there are

HoshoThemeStyle

One item. A color item uses foreground only.

Field Type Description
foreground uint32_t 0xRRGGBB
background uint32_t 0xRRGGBB
has_foreground uint8_t 0: no color of its own ("-" or none): the text's own color, or not drawn
has_background uint8_t 0: the cell's own background ("-")
bold uint8_t a style: drawn in the bold face
italic uint8_t a style: drawn in the italic face
present uint8_t the theme has the key, or hosho_theme_resolve() filled it

HoshoThemeSettings

The settings of the display, from [terminal-widget]. A value of -1 (or "" for the font) means the theme does not say.

Field Type Description
font_family[128] char font-family: the font, as fontconfig names it
font_size int font-size: in pixels
cursor_shape int cursor-shape: HoshoCursorShape (block, underline, bar)
cursor_blink int cursor-blink: 1 or 0
padding int padding: pixels between the window's edge and the text
gutter_width int gutter-width: pixels of the margin for the shell's marks; 0: no margin
line_spacing int line-spacing: more pixels between lines
scrollback_lines int scrollback-lines: the lines of scrollback to keep
bold_bright int bold-bright: 1 when bold text in colors 0 to 7 is drawn in colors 8 to 15
clipboard_read int clipboard-read: 1 when a program can read the clipboard (OSC 52)
clipboard_write int clipboard-write: 1 when a program can set the clipboard (OSC 52)
keyboard_vt220 int keyboard: 1 for "vt220" (Home and End are Find and Select, the keypad sends its own codes in application mode), 0 for "pc"
key_set int keyboard: a HoshoKeySet for "sun", "hp", "sco", "rxvt" or "linux": the codes of the function keys; 0 for "pc" and "vt220"
answerback[64] char answerback: what the terminal sends when a program sends ENQ; "" for nothing
ligatures int ligatures: 1 when the font's ligatures and contextual forms are drawn (the arrows of a programming font)
bell int bell: 0 "sound", 1 "visual" (the view's edge shows a frame for a moment), 2 "both", 3 "none"
minimum_contrast double minimum-contrast: the least contrast ratio of text and its background, 1 to 21; text with less gets a lighter or darker color; 1 changes nothing
notify_command_seconds int notify-command-seconds: a command of the shell that ran this long or longer gives a notification when it ends and the terminal does not have the keyboard; 0 for never
log_file[256] char log-file: the file that gets the text programs show, added at its end; "" for no log
background_image[256] char background-image: a picture file drawn behind the text (the widget reads PNG, JPEG, GIF, BMP and XPM); "" for none
background_opacity int background-opacity: how much of the background color covers the picture, 0 to 100
window_ops int window-ops: 1 when a program can show, iconify, move, size, raise, lower, maximize and make full screen the window, ask whether it is an icon, its position and the screen's size, and read its title (XTWINOPS)
print_command[256] char print-command: the shell command that gets what a program prints (MC), on its standard input; "" for no printer
paste_confirm int paste-confirm: 1 when a paste that can run a command (a line end with text after it, to a program with no bracketed paste) asks first

HoshoThemeRule

One rule of [rules]: text that a pattern finds in a program's output gets a style.

Field Type Description
name[32] char the rule's key in the theme file; a rule whose key is longer than 31 bytes is not read
pattern[160] char a POSIX extended regular expression
ignore_case uint8_t 1 when a and A are the same for the pattern
style HoshoThemeStyle the style; a color is for text with no color of its own

HoshoThemeEditorColors

The colors an editor theme of Unryu has that a terminal can take. Read from a theme file, not written.

Field Type Description
background HoshoThemeStyle [editor] background
text HoshoThemeStyle [editor] text
selection HoshoThemeStyle [editor] selection
selection_text HoshoThemeStyle [editor] selection-text
caret HoshoThemeStyle [editor] caret
line_numbers HoshoThemeStyle [editor] line-numbers
compiler_error HoshoThemeStyle [editor] compiler-error
compiler_success HoshoThemeStyle [editor] compiler-success
link HoshoThemeStyle [syntax] link

HoshoTheme

A theme.

Field Type Description
name[64] char [theme] name
description[256] char [theme] description
items[HOSHO_THEME_ITEM_COUNT] HoshoThemeStyle each color and style
settings HoshoThemeSettings the display's settings
editor HoshoThemeEditorColors what an editor theme in the same file gives
rules[HOSHO_THEME_RULE_LIMIT] HoshoThemeRule the rules that give output a style
rule_count int how many rules there are

Constants

HOSHO_THEME_RULE_LIMIT

#define HOSHO_THEME_RULE_LIMIT 32

The most rules a theme has; more are not read.

HOSHO_THEME_BUILTIN_COUNT

#define HOSHO_THEME_BUILTIN_COUNT 4

How many built-in themes there are.

hosho_theme_builtin_names

const char* const hosho_theme_builtin_names[HOSHO_THEME_BUILTIN_COUNT][HOSHO_THEME_BUILTIN_COUNT];

The names of the built-in themes.

Functions

hosho_theme_init

void hosho_theme_init(HoshoTheme *theme);

Make a theme empty: no key present, each setting "not said".

Parameter Description
theme The theme

hosho_theme_parse

int hosho_theme_parse(HoshoTheme *theme, const char *text, size_t length);

Read a theme from text; keys the text does not have stay as they are.

Parameter Description
theme The theme; call hosho_theme_init() first for a theme of only this text
text The text of a theme file
length Its bytes

Returns: The number of keys that were read

hosho_theme_read

int hosho_theme_read(HoshoTheme *theme, const char *path);

Read a theme file: an empty theme, the file's keys, then the fallbacks.

The file is read into one buffer of the library's own, so this is not for two threads at once.

Parameter Description
theme Gets the theme, complete
path The file

Returns: 1 when the file was read, 0 when it could not be opened; the theme is then the default

hosho_theme_builtin

int hosho_theme_builtin(const char *name, HoshoTheme *theme);

Get a built-in theme, complete.

Parameter Description
name One of hosho_theme_builtin_names
theme Gets the theme

Returns: 1 when found, 0 when no built-in theme has that name; the theme is then the default

hosho_theme_resolve

void hosho_theme_resolve(HoshoTheme *theme);

Fill each item and setting the theme left out.

Parameter Description
theme The theme

hosho_theme_write

int hosho_theme_write(const HoshoTheme *theme, FILE *file);

Write a theme file with every key, each with a line that says what it is.

Parameter Description
theme The theme
file Where to write

Returns: 1, or 0 when a write failed

hosho_theme_apply

void hosho_theme_apply(const HoshoTheme *theme, HoshoTerminal *terminal);

Give a terminal what the theme says of it.

The default colors and the cursor's color, the 16 palette colors, the cursor's shape and blink, the answerback message, the keyboard (pc or vt220) and the function keys' set, and whether a program can use the window functions and the clipboard. The other settings are for the display: the host reads them from HoshoTheme.settings.

Parameter Description
theme A complete theme
terminal The terminal

hosho_theme_item_section

const char *hosho_theme_item_section(HoshoThemeItem item);

The section an item is in.

Parameter Description
item The item

Returns: The section's name, or NULL

hosho_theme_item_key

const char *hosho_theme_item_key(HoshoThemeItem item);

The key of an item.

Parameter Description
item The item

Returns: The key, or NULL

hosho_theme_item_kind

HoshoThemeKind hosho_theme_item_kind(HoshoThemeItem item);

Whether an item is one color or a style.

Parameter Description
item The item

Returns: The kind