Skip to content

SinkConsole

A Console over an ordinary sink.

It supplies the rest of the console interface.

capture wraps a plain sink in one of these automatically.

It implements the Console interface with write and flush; flush emits any partial final line, which is what makes an unterminated print visible when a capture scope ends. line_ending is the interpreter host's, chosen arbitrarily -- a sink has no platform of its own. is_tty is always false and geometry() is nil, since a sink is never a terminal.

Output mode

The mode: given at construction fixes how written data is divided into values for the console's lifetime. In either mode, the values concatenate back to exactly the bytes that were written.

  • :LINE: -- one Str per complete line, terminator included. The default.
  • :CHUNK: -- arbitrary Bin chunks. Chunk boundaries are unspecified.
let lines = []
term.capture $lines do echo hello
assert_eq $lines ["hello\n"]

let chomped = []
term.capture (chomped.prechomp()) do echo hello
assert_eq $chomped ["hello"]

let chunks = []
term.capture $chunks mode: :CHUNK: do echo hello
# [b"hello\n"]

Nothing is translated on the way through: a line ending that arrives is the line ending that is stored.

Constructor

SinkConsole sink …

Wraps a sink in a console.

Parameters

NameTypeDescription
sink Sink[Value] Where output values are written.
:can_style? Bool Emit ANSI styling. Off by default.
:mode? (:LINE: | :CHUNK:) Output mode. Defaults to :LINE:.

Example

# Style is off by default.
let plain = []
term.capture $plain do echo $warning

# Opt in to SGR styling.
let styled = []
term.capture (term.SinkConsole styled can_style: true) do echo $warning