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

hosho_text.h

Questions About the Text on the Screen: Ranges, Search, Links, Command Blocks.

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 host asks these of a screen to give the user more than a grid of cells: the text between two cells, the places a word occurs, the link or file name under the pointer, and the lines that are one command with its output.

Features:

  • The text of a range of cells, with lines that continue joined
  • Search forward and back through the screen and the scrollback, with or without case; the matches on one line, for a display to mark
  • The link at a cell: a hyperlink a program made (OSC 8), an address in the text (scheme://... or mailto:...), or a file name with an optional line and column (src/main.c:120:8)
  • The command block of a line, from the shell's marks: its prompt, where its output starts, where it ends, how it ended

Internal design notes:

  • A row is as for hosho_screen_line(): 0 and up for the screen, negative for the scrollback
  • Search and links read one logical line at a time: the lines that continue on the next are one line of text, so a match or an address can go over the end of a line
  • A file name is a guess from the text's form. The host decides whether the file exists
#include "hosho_text.h"

Types: HoshoPosition, HoshoTextRange, HoshoLinkKind, HoshoLink, HoshoBlock, HoshoTextColor

Functions: hosho_text_range, hosho_text_search, hosho_text_search_row, hosho_text_search_pattern, hosho_text_search_pattern_row, hosho_text_rectangle, hosho_text_range_html, hosho_text_link_at, hosho_address_at, hosho_text_block_at

Types

HoshoPosition

A cell: its row (as for hosho_screen_line()) and column.

Field Type Description
row int 0 and up for the screen, negative for the scrollback
column int from 0

HoshoTextRange

A run of cells in reading order; start and end are both in it.

Field Type Description
start HoshoPosition the first cell
end HoshoPosition the last cell

HoshoLinkKind

What a link is.

Name Value Description
HOSHO_LINK_NONE no link
HOSHO_LINK_ADDRESS an address: found in the text when it has a scheme and :// (https://..., file://...) or is mailto: and a mail address; or any address a program made a link (OSC 8)
HOSHO_LINK_FILE a file name, maybe with a line and a column

The link at a cell.

Field Type Description
kind HoshoLinkKind what it is
text[2048] char the address, or the file name without its line and column
line int for a file: the line after its name, or 0
column int for a file: the column after the line, or 0
explicit_link int 1 when a program made the link (OSC 8), 0 when it was found in the text
range HoshoTextRange the cells of the link

HoshoBlock

The lines of one command: its prompt, its output, its end.

Field Type Description
prompt_row int the line where the prompt starts
output_row int the line where the output starts, or prompt_row + 1 when the shell did not say; more than end_row when there is no output
end_row int the last line of the block
status int HOSHO_STATUS_* of the command.
complete int 1 when a later prompt follows: the command ended

HoshoTextColor

typedef uint32_t (*HoshoTextColor)(void *user, HoshoColor color, int is_background);

Gives the 0xRRGGBB of a cell's color, for hosho_text_range_html(); hosho_terminal_color() does this for a terminal.

Functions

hosho_text_range

char *hosho_text_range(const HoshoScreen *screen, HoshoTextRange range, size_t *length);

The text of a range of cells.

Lines that continue on the next line are joined; other lines end with a line feed. Empty cells at a line's end are left out.

Parameter Description
screen The screen
range The cells
length Gets the bytes of the text; can be NULL

Returns: The text (UTF-8, NUL after it), which the caller must free(); NULL when memory ran out

int hosho_text_search(
    const HoshoScreen *screen,
    const char *needle,
    int case_sensitive,
    HoshoPosition from,
    int direction,
    HoshoTextRange *found);

Find the next or the previous place a text occurs.

Parameter Description
screen The screen
needle The text to find (UTF-8)
case_sensitive 1 when a and A are different; with 0, only the letters A to Z match their small letters
from Where to start: the match found starts after this cell (forward) or before it (back)
direction 1 to search to the end of the screen, -1 to search to the start of the scrollback
found Gets the cells of the match

Returns: 1 when a match was found, 0 when there is none in that direction

hosho_text_search_row

int hosho_text_search_row(
    const HoshoScreen *screen,
    const char *needle,
    int case_sensitive,
    int row,
    HoshoTextRange *ranges,
    int limit);

The matches of a text that have a cell on one row, for a display to mark.

Parameter Description
screen The screen
needle The text to find (UTF-8)
case_sensitive 1 when a and A are different; with 0, only the letters A to Z match their small letters
row The row
ranges Room for limit ranges; each can start or end on another row
limit How many ranges fit

Returns: How many ranges were written

hosho_text_search_pattern

int hosho_text_search_pattern(
    const HoshoScreen *screen,
    const char *pattern,
    int case_sensitive,
    HoshoPosition from,
    int direction,
    HoshoTextRange *found);

Find the next or the previous place a pattern matches.

The pattern is an extended regular expression of POSIX (regcomp()). A match is in one line of text; a line that continues on the next row is one line. A match of no characters is not a match.

Parameter Description
screen The screen
pattern The pattern (UTF-8)
case_sensitive 1 when a and A are different
from Where to start: the match found starts after this cell (forward) or before it (back)
direction 1 to search to the end of the screen, -1 to search to the start of the scrollback
found Gets the cells of the match

Returns: 1 when a match was found, 0 when there is none in that direction or the pattern is not correct

hosho_text_search_pattern_row

int hosho_text_search_pattern_row(
    const HoshoScreen *screen,
    const char *pattern,
    int case_sensitive,
    int row,
    HoshoTextRange *ranges,
    int limit);

The matches of a pattern that have a cell on one row, for a display to mark.

Parameter Description
screen The screen
pattern The pattern (UTF-8), as for hosho_text_search_pattern()
case_sensitive 1 when a and A are different
row The row
ranges Room for limit ranges; each can start or end on another row
limit How many ranges fit

Returns: How many ranges were written

hosho_text_rectangle

char *hosho_text_rectangle(
    const HoshoScreen *screen,
    HoshoTextRange range,
    size_t *length);

Get the text of a rectangle of cells: of each row, the columns from the range's first column to its last.

Parameter Description
screen The screen
range Two corners of the rectangle
length Gets the bytes of the text; can be NULL

Returns: The text (UTF-8, a line feed between the rows, NUL after it), which the caller must free(); NULL when memory ran out

hosho_text_range_html

char *hosho_text_range_html(
    const HoshoScreen *screen,
    HoshoTextRange range,
    HoshoTextColor color,
    void *user,
    size_t *length);

Get the text of a range of cells as HTML, with its colors, bold, italic, underline and strike.

Parameter Description
screen The screen
range The cells
color The function that gives a cell color's 0xRRGGBB
user Given back to color
length Gets the bytes of the text; can be NULL

Returns: A "pre" element (UTF-8, NUL after it), which the caller must free(); NULL when memory ran out

int hosho_text_link_at(
    const HoshoScreen *screen,
    HoshoPosition position,
    HoshoLink *link);

The link at a cell.

Parameter Description
screen The screen
position The cell
link Gets the link

Returns: 1 when the cell is part of a link, 0 when it is not

hosho_address_at

int hosho_address_at(const uint32_t *characters, int count, int at, int *start, int *end);

The web or mail address around a character of a line, as hosho_text_link_at() finds one.

An address has a scheme and :// (https://..., file://...), or is mailto: and a mail address. Brackets may be part of it; a full stop, comma or closing bracket with no opening one at its end is the sentence's, not the address's. An editor can use it on a line of its own text too.

Parameter Description
characters The line, one code point each
count How many
at The character asked about
start Set to the address's first character
end Set to just past its last

Returns: 1 when at is in an address, else 0

hosho_text_block_at

int hosho_text_block_at(const HoshoScreen *screen, int row, HoshoBlock *block);

The command block a line is in.

Parameter Description
screen The screen
row The line
block Gets the block

Returns: 1 when the line is in a block, 0 when no prompt is marked at or above it, or on the alternate screen