etui/span

Types

A single terminal row composed of styled spans.

pub type Line {
  Line(spans: List(Span), alignment: text.Alignment)
}

Constructors

A styled text fragment: string content + display style + optional hyperlink.

pub type Span {
  Span(content: String, style: style.Style, link: String)
}

Constructors

  • Span(content: String, style: style.Style, link: String)

    Arguments

    link

    OSC 8 hyperlink URI. Empty string = no link.

Several lines of styled text.

Line is one row and never wraps; Text is a block that does. Until this existed, wrapping only worked on a plain String, so any text that needed both mixed styles and reflowing, a log viewer or rendered markdown, could not have both.

pub type Text {
  Text(lines: List(Line))
}

Constructors

  • Text(lines: List(Line))

Values

pub fn line_aligned(
  spans: List(Span),
  alignment: text.Alignment,
) -> Line

Line from spans with explicit alignment.

pub fn line_new(spans: List(Span)) -> Line

Line from a list of spans, left-aligned.

pub fn line_plain(content: String) -> Line

Line with a single unstyled string, left-aligned.

pub fn line_width(l: Line) -> Int

Total cell width of a line (sum of span widths).

pub fn render_line(
  buf: buffer.Buffer,
  pos: geometry.Position,
  l: Line,
  max_width: Int,
) -> buffer.Buffer

Render a line into the buffer at pos, clipped to max_width cells. Each span is drawn with its own fg/bg/modifier. Spans beyond max_width are silently dropped; a span that straddles the boundary is truncated. The line’s alignment field shifts the start position within the available width. ASCII controls, including newlines and tabs, are ignored by the buffer. Use text_plain to split a string into multiple lines before rendering.

pub fn span_bg(sp: Span, color: style.Color) -> Span

Set background color on a span.

pub fn span_bold(content: String) -> Span

Bold span (default colors + bold modifier).

pub fn span_dim(content: String) -> Span

Dim span (default colors + dim modifier).

pub fn span_fg(sp: Span, color: style.Color) -> Span

Set foreground color on a span.

pub fn span_italic(content: String) -> Span

Italic span (default colors + italic modifier).

pub fn span_link(content: String, uri: String) -> Span

Span with an OSC 8 clickable hyperlink. Terminals that support OSC 8 (iTerm2, Kitty, VTE, Windows Terminal) will render the text as a clickable link. Others display it as plain text.

span.span_link("docs.gleam.run", "https://docs.gleam.run")
pub fn span_modifier(sp: Span, modifier: style.Modifier) -> Span

Add a modifier to a span.

pub fn span_plain(content: String) -> Span

Span with default terminal colors and no modifier.

pub fn span_styled(content: String, s: style.Style) -> Span

Span with explicit style applied.

pub fn span_underline(content: String) -> Span

Underline span (default colors + underline modifier).

pub fn span_underline_color(sp: Span, color: style.Color) -> Span

Colour the underline of a span independently of its text.

pub fn span_width(sp: Span) -> Int

Total cell width of a span.

pub fn text_height(t: Text) -> Int

Total rows.

pub fn text_new(lines: List(Line)) -> Text

Text from lines.

pub fn text_plain(content: String) -> Text

Text from a single unstyled string, split on newlines.

pub fn with_link(sp: Span, uri: String) -> Span

Add an OSC 8 hyperlink URI to an existing span.

pub fn wrap(t: Text, width: Int) -> Text

Wrap every line to width cells, keeping each span’s style.

span.line_new([span.span_bold("ERROR"), span.span_plain(" disk full")])
|> span.text_new([_])
|> span.wrap(12)
// ERROR disk   <- still bold
// full
pub fn wrap_line(l: Line, width: Int) -> List(Line)

Wrap one line into as many as it takes.

Words are the unit, as in text.wrap, and a word wider than the line is broken across rows rather than left to overflow. A word never loses its style by being moved to another row, which is the whole point: the styles travel with the words rather than with the columns they happened to be in.

✨ Search Document