spruce/output

Pipeable output accumulation.

An Output threads a Spruce context and a buffer of rendered blocks through a pipeline, so several renderers compose with |> and emit together. It stays pure: nothing is printed until print, and the context is threaded for you so each renderer sees the right color level and indent depth.

import spruce
import spruce/message
import spruce/output

pub fn main() {
  let sp = spruce.detect()

  output.new(sp)
  |> output.append(message.start(_, "compiling"))
  |> output.group("Tests", fn(o) {
    o |> output.append(message.info(_, "running"))
  })
  |> output.print
}

append accepts any Spruce -> String renderer via a _ capture, so it works with every spruce module without per-type variants. For eager, streaming grouping that prints as work happens and can return a value, use spruce/group.group instead.

Types

An accumulator of rendered blocks plus the context they render with. Build one with new and the combinators in this module, then finish with to_string or print.

pub opaque type Output

Values

pub fn append(
  output: Output,
  render: fn(spruce.Spruce) -> String,
) -> Output

Append a rendered block produced by render, which receives the output’s context. Works with any Spruce -> String renderer via a _ capture, e.g. output.append(message.success(_, "done")).

pub fn blank(output: Output) -> Output

Append a blank line.

pub fn context(output: Output) -> spruce.Spruce

The context the output renders with. Reflects the current group depth inside a group body.

pub fn group(
  output: Output,
  title: String,
  body: fn(Output) -> Output,
) -> Output

Append a styled group title, then run body with the output’s context indented one level deeper. Blocks appended inside body nest under the title. Unlike spruce/group.group, this buffers output rather than printing.

pub fn new(sp: spruce.Spruce) -> Output

Start an empty output that renders with sp.

pub fn print(output: Output) -> Nil

Print the accumulated output to stdout.

pub fn text(output: Output, text: String) -> Output

Append a raw string as-is, without rendering.

pub fn to_string(output: Output) -> String

Render the accumulated output to a single string, blocks joined by newlines.

Search Document