Documentation
¶
Overview ¶
Package text is width-aware text handling: the measuring, padding and clipping a terminal table needs, with no colour and no layout in it.
Everything here counts display cells rather than bytes or runes. A box-drawing glyph is one cell, an emoji is two, and a rune count would quietly overflow the column that trusted it.
Index ¶
- func Ago(t time.Time) string
- func Clip(s string, n int) string
- func ClipBlock(block string, width, height int) string
- func Elapsed(d time.Duration) string
- func FirstLine(s string) string
- func FirstWords(s string, n int) string
- func Fit(s string, n int) string
- func LineCount(block string) int
- func Pad(s string, n int) string
- func Plain(s string) string
- func Quote(s string) string
- func Since(t time.Time) string
- func Truncate(s string, n int) string
- func Width(s string) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ago ¶
Ago renders a timestamp as a compact relative age, as k9s does. Anything under a minute is "now": below that the number changes faster than it is read.
func Clip ¶
Clip shortens a possibly-styled string to n cells without breaking its escape sequences — unlike Truncate it adds no ellipsis, because a table cell that spends a cell saying it was cut is a cell of content lost.
func ClipBlock ¶
ClipBlock trims a block to a height and a width, marking a cut with an ellipsis on its own line — so a long quote cannot push a dialog off screen.
func Elapsed ¶
Elapsed is Since for a duration already measured — by a caller holding a clock of its own, which is the only way one of these is testable without waiting.
func FirstLine ¶
FirstLine is the opening line of a body: a commit subject, a comment's first sentence — the part that fits on a table row.
func FirstWords ¶
FirstWords is the first n words of s — how a full sentence of help becomes a footer hint without a second, shorter copy of it being written by hand.
func Plain ¶
Plain strips escape sequences and stray control characters, leaving the text a reader would see.
It is used for two different reasons and has to be right for both. Recolouring wants the text without its colours — a selection band whose background a cell's own colour would otherwise end partway along the row. Safety wants the text without anything a terminal would obey: a description, a comment or a branch name arrives from somewhere else, and a remote string that can retitle a window, ring the bell or move the cursor is a remote string that can lie about what is on screen.
func Since ¶
Since is a compact age that keeps its seconds, which Ago deliberately rounds away: "failed · now" reads as if nothing were wrong, and the first minute is exactly when a stalled screen is worth noticing.
Types ¶
This section is empty.