fmt
lib/fmt.vklib/fmt.vk Pure-Veko string formatting, radix conversion, human-readable units, hex dumps, and ASCII tables. Zero capabilities required.
int-radix = fn [n i64, base i64] str
Formats a signed integer n in the given base (2..36).
use [fmt]
r1 = fmt/int-radix 255 16
r2 = fmt/int-radix -42 2
if r1 != 'ff' or r2 != '-101010' [ err 'fmt/int-radix mismatch' ] else [ .ok ]
pad-left = fn [s str, width i64, pad_ch str] str
Pads s on the left with pad_ch (1-byte string) to reach width bytes.
use [fmt]
if (fmt/pad-left '42' 5 '0') != '00042' [ err 'pad-left mismatch' ] else [ .ok ]
pad-right = fn [s str, width i64, pad_ch str] str
Pads s on the right with pad_ch (1-byte string) to reach width bytes.
use [fmt]
if (fmt/pad-right 'hi' 5 '.') != 'hi...' [ err 'pad-right mismatch' ] else [ .ok ]
hex = fn [n i64, width i64] str
Formats a non-negative integer n as zero-padded lowercase hexadecimal of at least width chars.
use [fmt]
if (fmt/hex 0x2a 4) != '002a' [ err 'fmt/hex mismatch' ] else [ .ok ]
hex-bytes = fn [data bytes] str
Formats a bytes buffer as a lowercase hexadecimal string.
use [fmt]
b = bytes/from-str 'AB'
if (fmt/hex-bytes b) != '4142' [ err 'fmt/hex-bytes mismatch' ] else [ .ok ]
size = fn [n i64] str
Formats a byte count into a human-readable IEC size string (B, KiB, MiB, GiB, TiB).
use [fmt]
if (fmt/size 1536) != '1.50 KiB' [ err 'fmt/size mismatch' ] else [ .ok ]
dur = fn [ns i64] str
Formats a duration in nanoseconds into a readable unit string (ns, us, ms, s, m/s).
use [fmt]
if (fmt/dur 2500000) != '2.50 ms' [ err 'fmt/dur mismatch' ] else [ .ok ]
format = fn [tpl str, args block] str
Formats tpl using positional placeholders ({0}, {1}, or auto-numbered {}).
Double braces {{ and }} emit literal braces.
use [fmt]
s = fmt/format 'host={0} port={1} ({{ok}})' ['api.local' 8443]
if s != 'host=api.local port=8443 ({ok})' [ err 'fmt/format mismatch' ] else [ .ok ]
template = fn [tpl str, vars map] str
Formats tpl using named {key} placeholders looked up in vars.
Double braces {{ and }} emit literal braces.
use [fmt]
s = fmt/template 'GET {path} HTTP/{ver}' { path = '/health', ver = '1.1' }
if s != 'GET /health HTTP/1.1' [ err 'fmt/template mismatch' ] else [ .ok ]
hexdump = fn [data bytes, cols i64] str
Renders a canonical offset + hex + ASCII hexdump of data with cols bytes per row.
use [fmt]
dump = fmt/hexdump (bytes/from-str 'Veko') 8
if not (str/contains? dump '56 65 6b 6f') [ err 'hexdump mismatch' ] else [ .ok ]
table = fn [headers block[str], rows block] str
Renders a clean padded ASCII table from headers (block[str]) and rows (block[block[str]]).
use [fmt]
tbl = fmt/table ['Name' 'Port'] [['api' '8443'] ['db' '5432']]
if not (str/contains? tbl 'api') [ err 'table mismatch' ] else [ .ok ]