Skip to content
pi-c Artifact [d43ee741ef]
Login

Artifact [d43ee741ef]

Artifact d43ee741ef8d1c34063ea038c6920e31ef22399683348c920ac5106f07688671:


/* Markdown as Pi's terminal UI draws it (markdown.c): headings, paragraphs
 * with `code`, bold, italic, strikethrough and links, bulleted, numbered and
 * nested lists, block quotes, fenced code with its language label, rules and
 * tables. Pure functions: text in, styled and wrapped lines out. */
#ifndef PIC_TUI_MARKDOWN_H
#define PIC_TUI_MARKDOWN_H

#include <stddef.h>

#include "width.h"

typedef struct {
    const char *base; /* SGR set again after every style change; NULL or "": none */
    int links;        /* link text is an OSC 8 hyperlink (and the url is not printed) */
    int preserve;     /* a user message: list markers and backslashes stay as typed */
} tui_md_opts;

/* text[0..len) as lines of at most cols columns, appended to out. Blocks are
 * separated by one blank line; no trailing blank line. 0 or -1 (out of memory). */
int tui_markdown(const char *text, size_t len, size_t cols, const tui_md_opts *o, tui_lines *out);

/* A streamed reply is flushed block by block: the bytes of the first block of
 * s that is finished (it ends in a blank line outside a code fence), blank
 * line included; 0 when the first block is still open. */
size_t tui_markdown_block_len(const char *s);

/* One line of a block too long to wait for: inside a code fence (*fence) it is
 * code; a fence line opens or closes it; otherwise it is rendered as text. */
int tui_markdown_line(const char *line, size_t len, int *fence, size_t cols, const tui_md_opts *o, tui_lines *out);

/* Pi's user message: the text rendered as markdown on the user-message
 * background, one column of padding on each side and a blank row above and
 * below, every row padded to cols. */
int tui_user_message_lines(const char *text, size_t cols, int links, tui_lines *out);

/* Whether to draw OSC 8 hyperlinks. MODE is the hyperlinks setting: "on",
 * "off", or "auto" (NULL too): on unless the terminal is known to show the
 * sequence or to drop it (TERM dumb or linux, Apple Terminal, GNU screen);
 * inside tmux only from tmux 3.4, which passes links on. */
int tui_hyperlinks_supported(const char *mode);

/* Turns links in tool output and bare URLs and paths on or off, and sets the
 * directory relative paths resolve against (the workspace; NULL: the current
 * directory). Markdown links follow tui_md_opts.links. */
void tui_links_set(int on, const char *base);
int tui_links_on(void);

/* The URL a link to dest[0..n) opens: a URL with a scheme (https://, mailto:)
 * as it is; a path as file:///ABSOLUTE/PATH, resolved against the base,
 * percent-encoded, with a :LINE[:COL] or :A-B suffix and a #fragment left
 * out. NULL for an anchor alone or nothing. malloc'd. */
char *tui_link_url(const char *dest, size_t n);

/* s[0..n) with each bare http(s) URL and each path that names an existing
 * file or directory made an OSC 8 link (with an id, so a wrapped one stays
 * one link); the visible text is unchanged and text already in a link is
 * left alone. NULL when nothing was linked (or links are off); else malloc'd,
 * its length in *outn. */
char *tui_linkify(const char *s, size_t n, size_t *outn);

/* Whether a click may hand url to the system opener: an http(s) or mailto
 * URL, or a file:/// URL naming an existing directory or a regular file that
 * is not executable and whose extension is not one the opener would run
 * (.app, .command, .sh, .py, ...; Ghostty refuses the same). */
int tui_link_safe(const char *url);

/* The sequence that opens an OSC 8 link to url[0..n): ESC ] 8 ; id=ID ; URL
 * ESC \, the id a hash of the URL so every piece of a wrapped link shares it.
 * malloc'd. */
char *tui_link_open(const char *url, size_t n);

#endif