3.8. kink/FORMAT

Provides str formatting via FORMAT.format.

The template str for format fun can contain placeholders delimited by braces "{" and "}". FORMAT.format invokes show method fo the arg corresponding to the placeholder, and emplaces the result str in the placeholder.

There are following variants of placeholders.

"{}" -- takes a positional argument, with the spec ''.

"{%xxx}" -- takes a positional argument, with the spec 'xxx'.

"{Foo}" -- takes a named argument with the name 'Foo', with the spec ''.

"{Foo%xxx}" -- takes a named argument with the name 'Foo', with the spec 'xxx'.

The name str must not contain "{", "}" and "%".

The spec str is passed to spec method of the conf val of the show method. The spec str must not contain "{" or "}".

The braces "{" and "}" must be escaped as "{{" and ""}}" in the template str.

You can specify a locale for the formatting. If you do so, the locale str is passed to the locale method of the conf val of the show method, before spec method is called.

Example:

:FORMAT.require_from('kink/')
:Str <- FORMAT.format('Id: {%04d}, Name: {}, Elevation: {%,d} meters'){(:F)
  F.args(12 'Mount Fuji' 3776.24)
}
stdout.print_line(Str)
# Output:
# Id: 0012, Name: Mount Fuji, Elevation: 3,776.24 meters

With locale:

:FORMAT.require_from('kink/')
:Str <- FORMAT.format('Id: {%04d}, Name: {}, Elevation: {%,d} meters'){(:F)
  F.locale('da-DK')
  F.args(12 'Mount Fuji' 3776.24)
}
stdout.print_line(Str)
# Output:
# Id: 0012, Name: Mount Fuji, Elevation: 3.776,24 meters

Using named args:

:FORMAT.require_from('kink/')
:Str <- FORMAT.format('Id: {Id%04d}, Name: {Name}, Elevation: {Elevation%,d} meters'){(:F)
  F.locale('da-DK')
  F.named_args(
    'Id' 12
    'Name' 'Mount Fuji'
    'Elevation' 3776.24
  )
}
stdout.print_line(Str)
# Output:
# Id: 0012, Name: Mount Fuji, Elevation: 3.776,24 meters

3.8.1. FORMAT.format(Template $config)

Does the formatting with the Template str and the $config fun, then return the result str.

$config must take a format_conf val.

3.8.2. Type format_conf

Conf val for $config fun of FORMAT.format.

Format_conf.args(... Args)

Provies positional args.

The number of args must be equal to the unnamed placeholders.

Format_conf.named_args(Name1 Val1 Name2 Val2 ,,,)

Provides named args.

All the named args required by the template must be provided. Providing unused args causes no problem.

Format_conf.locale(Locale)

Provides a locale str.