fmglee

fmglee

A string formatting library for gleam.

Valid format specifiers are

Examples

import fmglee as fmt
import gleeunit/should

fmt.new("Number %d, float %f, string %s")
|> fmt.d(99)
|> fmt.f(12.9)
|> fmt.s("Hello!")
|> fmt.build
|> should.equal("Number 99, float 12.9, string Hello!")
fmt.fmt("Number %d, float %f, string %s", with: [fmt.D(99), fmt.F(12.9), fmt.S("Hello!")])
|> should.equal("Number 99, float 12.9, string Hello!")

Types

pub type Fmt {
  S(String)
  D(Int)
  F(Float)
}

Constructors

  • S(String)

    Wrap a String for use with fmt(_, using: [..])

  • D(Int)

    Wrap an Int for use with fmt(_, using: [..])

  • F(Float)

    Wrap a Float for use with fmt(_, using: [..])

Errors returned by fmtglee

pub type FmtError {
  IncorrectValueType
  TooManyValues
  NotEnoughValues
}

Constructors

  • IncorrectValueType

    The current value does not match the current placeholder. Returned when the order of Fmts is not the same as the order of placeholders.

  • TooManyValues

    Returned when there are more Fmts than placeholders in the string.

  • NotEnoughValues

    Returned when there are less Fmts than placeholders in the string.

pub type Formatter {
  Formatter(s: String, v: List(Fmt))
}

Constructors

  • Formatter(s: String, v: List(Fmt))

    Used to compile a format string using the pipeable methods.

Functions

pub fn build(formatter: Formatter) -> String

Compile a Formatter into a string. Panics where try_build(formatter) returns an FmtError.

pub fn d(formatter: Formatter, d: Int) -> Formatter

Add an Int value to a Formatter.

pub fn f(formatter: Formatter, f: Float) -> Formatter

Add a Float value to a Formatter.

pub fn fmt(s: String, with v: List(Fmt)) -> String

Format a string and a list of Fmt values. Will panic where try_fmt(s, v) returns an FmtError.

pub fn new(s: String) -> Formatter

Create a new Formatter.

Examples

new("Hello, %s. You are %d years old.")
|> s(name)
|> d(age)
|> build
|> io.println
pub fn print(s: String, with v: List(Fmt)) -> Nil

Compile the format string and print the output. Panics if fmt(s, v) would panic with the provided arguments.

pub fn printf(formatter: Formatter) -> Nil

Print the output of a compiled Formatter. Panics if the Formatter is invalid.

pub fn println(s: String, with v: List(Fmt)) -> Nil

Compile the format string and print the output with a newline. Panics if fmt(s, v) would panic with the provided arguments.

pub fn printlnf(formatter: Formatter) -> Nil

Print the output of a compiled Formatter with a newline. Panics if the Formatter is invalid.

pub fn s(formatter: Formatter, s: String) -> Formatter

Add a String value to a Formatter.

pub fn try_build(
  formatter: Formatter,
) -> Result(String, FmtError)

Compile a Formatter into a string. Errors when the number of provided values does not match the number of placeholders, or if an invalid type was provided for a placeholder.

pub fn try_fmt(
  s: String,
  with v: List(Fmt),
) -> Result(String, FmtError)

Format a string and a list of Fmt values. Substitutes placeholders from left to right with values in the list of Fmt. Errors if there is a type missmatch between the placeholder and the value, or if the number of placeholders does not match the number of values given.

pub fn write(
  s: String,
  with v: List(Fmt),
  using writer: fn(String) -> Nil,
) -> Nil

Write a string and list of Fmt to the provided Writer.

pub fn writef(
  formatter: Formatter,
  using writer: fn(String) -> Nil,
) -> Nil

Write a Formatter to the provided writer. The Formatter will be built using build which will panic if the formatter is invalid.

Search Document