text

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package text provides pure text/string utility functions. All functions are ANSI-aware where relevant (counting visible width, truncation, padding). This is a leaf package with zero internal imports.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountVisibleWidth

func CountVisibleWidth(s string) int

CountVisibleWidth returns the visible width of a string, excluding ANSI codes.

func FirstLine

func FirstLine(s string) string

FirstLine returns the first line of a multi-line string.

func Indent

func Indent(s string, spaces int) string

Indent prefixes each non-empty line with the given number of spaces.

func JoinNonEmpty

func JoinNonEmpty(sep string, parts ...string) string

JoinNonEmpty joins non-empty strings with the given separator.

func LineCount

func LineCount(s string) int

LineCount returns the number of lines in a string.

func PadCenter

func PadCenter(s string, width int) string

PadCenter centers a string within the specified width. ANSI-aware: counts visible characters only.

func PadLeft

func PadLeft(s string, width int) string

PadLeft pads a string on the left to the specified width. ANSI-aware: counts visible characters only.

func PadRight

func PadRight(s string, width int) string

PadRight pads a string on the right to the specified width. ANSI-aware: counts visible characters only.

func Repeat

func Repeat(s string, n int) string

Repeat returns s repeated n times. Returns empty string if n <= 0.

func Slugify added in v0.2.0

func Slugify(s string) string

Slugify normalizes input text into a lowercase dash-separated slug. The result contains only [a-z0-9-], collapses repeated dashes, trims leading/trailing dashes, and is capped at 64 chars. Returns "project" when normalization produces an empty value.

func StripANSI

func StripANSI(s string) string

StripANSI removes all ANSI escape sequences from a string.

func Truncate

func Truncate(s string, width int) string

Truncate shortens a string to width visible characters, adding "..." if truncated. ANSI-aware: counts visible characters only. When truncation occurs, ANSI codes are stripped from the result (reinserting codes at truncation boundaries is not supported).

func TruncateMiddle

func TruncateMiddle(s string, width int) string

TruncateMiddle shortens a string by removing characters from the middle. Useful for paths: "/Users/foo/very/long/path" -> "/Us.../path" ANSI-aware: when truncation occurs, ANSI codes are stripped from the result.

func WordWrap

func WordWrap(s string, width int) string

WordWrap wraps text to the specified width, breaking on word boundaries.

func WrapLines

func WrapLines(s string, width int) []string

WrapLines wraps text to the specified width and returns individual lines.

Types

This section is empty.

Jump to

Keyboard shortcuts

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