Documentation
¶
Overview ¶
Package ansi provides utilities for working with ANSI escape sequences in strings. It offers functions to strip ANSI codes, calculate visible string length, and perform ANSI-aware trimming operations.
Index ¶
- func Length(s string) int
- func Strip(s string) string
- func TrimLeftSpaces(s string) string
- func TrimLinesRight(s string) string
- func TrimRight(s string) string
- func TrimRightSpaces(s string) string
- func TrimTrailingWhitespace(rendered string, paragraphIndent string, paragraphIndentWidth int) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Strip ¶
Strip removes all ANSI escape codes from a string, returning only visible content. This uses a state machine approach for robust handling of all ANSI sequences.
func TrimLeftSpaces ¶
TrimLeftSpaces removes only leading spaces from an ANSI-coded string while preserving all ANSI escape sequences on the remaining content. This is useful for removing Glamour's paragraph indent while preserving styled content.
func TrimLinesRight ¶
TrimLinesRight trims trailing whitespace from each line in a multi-line string. This is useful after lipgloss.Render() which pads all lines to the same width. Uses ANSI-aware TrimRight to handle whitespace wrapped in ANSI codes.
Background: Glamour pads content to ensure background colors render properly (see https://github.com/charmbracelet/glamour/issues/235). This padding is necessary for styled blocks but creates excessive trailing whitespace. We trim this padding while preserving intentional suffix spaces that are part of the styled content (e.g., H1 header badges like " About Atmos ").
func TrimRight ¶
TrimRight removes trailing whitespace from an ANSI-coded string while preserving trailing spaces that are part of styled content (with background color). This distinguishes between:
- Styled content spaces: have background color (48;2;...) - PRESERVED
- Glamour padding spaces: only foreground color (38;2;...) - TRIMMED
This preserves intentional suffix spaces (e.g., H1 header badges " About ") while removing Glamour's line-padding spaces that have no background styling.
func TrimRightSpaces ¶
TrimRightSpaces removes only trailing spaces (not tabs) from an ANSI-coded string while preserving all ANSI escape sequences on the actual content. This is useful for removing Glamour's padding spaces while preserving intentional tabs.
func TrimTrailingWhitespace ¶
func TrimTrailingWhitespace(rendered string, paragraphIndent string, paragraphIndentWidth int) []string
TrimTrailingWhitespace splits rendered markdown by newlines and trims trailing spaces that Glamour adds for padding (including ANSI-wrapped spaces). For empty lines (all whitespace), it preserves the leading indent (first 2 spaces) to maintain paragraph structure.
Note: Glamour's padding ensures background colors render properly across the full width (see https://github.com/charmbracelet/glamour/issues/235). We remove this padding for cleaner output while TrimRight preserves intentional suffix spaces in styled content.
Types ¶
This section is empty.