std/text
Building and laying out text: a builder that adds text in parts, the
columns text takes in a terminal, padding, centering, cutting, and wrapping
to a width, and fuzzy matching with scores, as command palettes and
quick-open lists rank what was typed. Numbers are laid out with the
built-in format(value, spec), as
format(1234567, ",") gives "1,234,567".
import "std/text"
TextBuilder b = text_builder()
b.add("total: ")
b.add(format(1234.5, ",.2f"))
print(b) // total: 1,234.50
print(pad_left("7", 3), center("ok", 6)) // " 7" and " ok "
for m in fuzzy_filter("main", ["src/main.cz", "maintain.txt", "docs/notes.md"]) do
print(m.score, m.text, m.positions)
end| Function | Result |
|---|---|
text_builder() |
a TextBuilder, with add(text), add_line(text), add_char(c), length(), to_text(), and clear(); it is a ToText, so print and f-strings show its text |
text_width(text) |
the columns text takes: terminal codes count 0, wide characters such as CJK and emoji 2, marks 0 |
char_width(code) |
the columns of one character |
strip_styles(text) |
text without terminal codes |
pad_right, pad_left, center (text, width) |
text padded with spaces to width columns |
truncate(text, width) |
text cut to width columns, ending in … when cut |
take_columns(text, width) |
the longest start of text that fits, whole grapheme clusters at a time |
wrap(text, width) |
text broken into lines at spaces |
indent(text, spaces) |
every line of text indented |
repeat(text, count) |
text repeated |
fuzzy_match(query, text) |
an Option[FuzzyMatch]: whether the characters of query are in text in order, whatever their case, with a score and the byte positions of the matched characters |
fuzzy_filter(query, texts) |
the texts query matches, as FuzzyMatch items, the best first |
A fuzzy match scores more for characters at the start, at the start of a
word or of a part of a name (after _, -, ., /, a space, or where
lowercase turns uppercase), and next to each other, and less for those
skipped between; among matches that score the same, the shorter text
comes first.