Documentation
ยถ
Index ยถ
Constants ยถ
This section is empty.
Variables ยถ
This section is empty.
Functions ยถ
This section is empty.
Types ยถ
type LineOffset ยถ
LineOffset represents a half-open interval [Start, End) that describes either the byte offset or rune offset range of a wrapped segment in the original unwrapped string.
type WrappedString ยถ
type WrappedString struct { // The current wrapped line number (after wrapping). CurLineNum int // The original unwrapped line number this segment came // from. OrigLineNum int // The byte start and end offsets of this segment in the // original unwrapped string. OrigByteOffset LineOffset // The rune start and end offsets of this segment in the // original unwrapped string. OrigRuneOffset LineOffset // Which segment number this is within the original line // (first, second, etc.). SegmentInOrig int // Whether this segment is the last from the original // ilne within the unwrapped string. LastSegmentInOrig bool // Whether the segment fits entirely within the wrapping // limit. NotWithinLimit bool // Whether the wrap was due to a hard break (newline) // instead of word wrapping. IsHardBreak bool // The viewable width of the wrapped string. Width int // Whether this wrapped segment ends with a split word due // to reaching the wrapping limit // (e.g., a hyphen may be added). EndsWithSplitWord bool }
WrappedString represents a single wrapped segment of the original unwrapped string, along with metadata about the wrapping process.
The WrappedString struct is used to store the metadata for each wrapped segment of the original unwrapped string.
type WrappedStringSeq ยถ
type WrappedStringSeq struct { // WrappedLines is the list of individual wrapped segments with // metadata. WrappedLines []WrappedString // WordSplitAllowed indicates whether splitting words across // lines is permitted. WordSplitAllowed bool // TabSize defines how many spaces a tab character expands to. TabSize int // Limit is the maximum viewable width allowed per line. Limit int }
WrappedStringSeq holds the sequence of wrapped lines produced by the string wrapping process, along with the configuration used.
func StringWrap ยถ
func StringWrap(str string, limit int, tabSize int, trimWhitespace bool) ( string, *WrappedStringSeq, error, )
StringWrap wraps the input string to the specified viewable-width limit, expanding tabs using the given tab size. It preserves *word boundaries* and never splits words across lines.
If trimWhitespace is true, leading and trailing whitespace on each wrapped line is stripped before the newline is appended.
ANSI escape sequences are preserved without contributing to visual width.
NOTE: Even though this variant does **not** split words, it still walks the text by Unicode *grapheme clusters* (using uniseg) and measures each cluster with go-runewidth. That is required for perfect width accounting with sequences such as ZWJ emojis (e.g. "๐ฉโ๐ป"), base-plus-combining marks, and full-width spaces. A plain rune scan would over-count their columns and wrap too early.
Returns the wrapped string and a metadata slice (WrappedStringSeq) that maps every wrapped segment back to its byte/rune span in the original input.
func StringWrapSplit ยถ
func StringWrapSplit(str string, limit int, tabSize int, trimWhitespace bool) ( string, *WrappedStringSeq, error, )
StringWrapSplit wraps the input string to the specified viewable-width limit, expanding tabs using the given tab size. Unlike StringWrap, this variant *may* split a word across lines if it exceeds the limit.
If trimWhitespace is true, leading and trailing whitespace on each wrapped line is stripped before the newline is appended.
ANSI escape sequences are preserved without contributing to visual width.
Because word splitting must occur only at safe grapheme boundaries, this function uses exactly the same grapheme-aware width logic described above; the only behavioural difference is that it inserts split points (and an optional hyphen) when necessary.
Returns the wrapped string and a metadata sequence describing each wrapped line.