Skip to content

FmtSpec

Stores reusable formatting options.

FmtSpec values are immutable. Calling one returns a new specification with the supplied options overridden, or a bound FmtValue when given a positional value.

Constructor

FmtSpec …

Parameters

NameTypeDescription
:fill? FmtFill Padding fill
:align? FmtAlign Alignment
:sign? FmtSign Sign display
:width? Int Minimum width in grapheme clusters
:precision? Int Numeric precision or maximum graphemes
:alt? Bool Enables alternate formatting
:kind? FmtKind Representation kind

Example

let money = FmtSpec precision: 2 kind: :FIXED:
assert_eq "$(money 1.5)/$(money 2.25)" "1.50/2.25"

Methods

(call) …

Returns a new specification, or a bound FmtValue when value is provided. Omitted options retain their current values; nil resets an option.

Binding a FmtValue nests it: the inner specification renders the value and this one lays that rendering out. See Sequencing.

Parameters

NameTypeDescription
value? value to bind
:fill? (FmtFill | nil) Padding fill
:align? (FmtAlign | nil) Alignment
:sign? (FmtSign | nil) Sign display
:width? (Int | nil) Minimum width in grapheme clusters
:precision? (Int | nil) Numeric precision or maximum graphemes
:alt? (Bool | nil) Enables alternate formatting
:kind? (FmtKind | nil) Representation kind

align() -> (FmtAlign | nil)

:LEFT:, :RIGHT:, :CENTER:, or nil for the formatted value's default.

alt() -> Bool

Whether alternate formatting is enabled.

fill() -> (FmtFill | nil)

The fill character, :ZERO: for numeric zero padding, or nil for spaces.

kind() -> (FmtKind | nil)

The requested representation, or nil to inherit the surrounding conversion.

Kind Representation Conversion
:STR: string s
:DBG: debug ?
:VERBATIM: verbatim !
:HEX: integer (hex) x
:OCT: integer (octal) o
:BIN: integer (binary) b
:DEC: integer (decimal) d
:EXP: float (scientific) e
:FIXED: float (fixed) f

The Conversion column gives the equivalent character in a formatted interpolation.

pad value -> Str

Applies fill, alignment, width, and truncation to a string.

Parameters

NameTypeDescription
value Str String to lay out

Example

let column = FmtSpec fill: . align: :CENTER: width: 8
echo $column.pad("name")

precision() -> (Int | nil)

The numeric precision or maximum number of extended grapheme clusters, depending on the formatted value, or nil when unset.

sign() -> (FmtSign | nil)

:PLUS:, :SPACE:, or nil to show only negative signs.

width() -> (Int | nil)

The minimum width in extended grapheme clusters, or nil when unset. A value may measure itself differently: term.Text measures terminal cells.