text

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ago

func Ago(t time.Time) string

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

func Clip(s string, n int) string

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

func ClipBlock(block string, width, height int) string

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

func Elapsed(d time.Duration) string

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

func FirstLine(s string) string

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

func FirstWords(s string, n int) string

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 Fit

func Fit(s string, n int) string

Fit pads or clips a possibly-styled line to exactly n cells.

func LineCount

func LineCount(block string) int

LineCount is how many lines a rendered block occupies.

func Pad

func Pad(s string, n int) string

Pad right-pads s to n display cells, accounting for wide runes.

func Plain

func Plain(s string) string

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 Quote

func Quote(s string) string

Quote wraps s in double quotes, for naming a value back to the reader.

func Since

func Since(t time.Time) string

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.

func Truncate

func Truncate(s string, n int) string

Truncate shortens s to n display cells, marking the cut with an ellipsis.

func Width

func Width(s string) int

Width is the number of display cells a possibly-styled string occupies.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL