text

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package text provides small formatting helpers shared by command output: rune-safe truncation, compact relative timestamps, and byte sizes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisplayWidth added in v0.2.1

func DisplayWidth(s string) int

DisplayWidth reports how many terminal columns s occupies: East Asian wide and fullwidth runes take two, combining marks and format characters take none, everything else takes one.

Column alignment must be computed with this rather than a rune count. Melange output carries server-supplied text — device marketing names above all — and a single CJK character in one cell shifts every column to its right when padding is counted in runes.

func EscapeTSVCell

func EscapeTSVCell(s string) string

EscapeTSVCell makes one value safe for the CLI's line-oriented TSV contract. Printable content is unchanged; backslashes and record/column control characters use reversible backslash escapes.

func FormatBytes

func FormatBytes(n int64) string

FormatBytes renders n in binary units (KiB/MiB/GiB) with one decimal. Values below 1 KiB are plain bytes; values beyond 1 GiB stay in GiB.

func Pluralize added in v0.2.1

func Pluralize(n int, singular, plural string) string

Pluralize renders a count with its noun: "1 repository", "3 repositories". Both forms are spelled out by the caller because English plurals the CLI needs are not all formed by appending "s".

func RelativeTime

func RelativeTime(t, now time.Time) string

RelativeTime renders t relative to now in a compact form: "just now", "5m ago", "3h ago", "2d ago", and an absolute "Jan 2, 2006" date beyond 30 days. Future timestamps render as "just now".

func SanitizeTerminal

func SanitizeTerminal(s string) string

SanitizeTerminal removes terminal escape/control sequences from untrusted human-readable text. Newlines and tabs are retained so block layout remains readable; all other C0/C1 controls are removed. OSC, CSI, and related string controls are consumed through their terminator so their payload cannot be reinterpreted or copied into a terminal.

func SanitizeTerminalInline

func SanitizeTerminalInline(s string) string

SanitizeTerminalInline is SanitizeTerminal for table cells and other single-line fields. Layout controls become spaces rather than new rows.

func Truncate

func Truncate(max int, s string) string

Truncate shortens s to at most max terminal columns (see DisplayWidth), appending "..." when content is cut. It never splits a multibyte rune. A max at or below the width of the ellipsis truncates without the suffix; max <= 0 yields "".

Types

This section is empty.

Jump to

Keyboard shortcuts

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