ansi

package
v1.205.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Length

func Length(s string) int

Length returns the visible character count of a string, excluding ANSI codes.

func Strip

func Strip(s string) string

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

func TrimLeftSpaces(s string) string

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

func TrimLinesRight(s string) string

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

func TrimRight(s string) string

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

func TrimRightSpaces(s string) string

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.

Jump to

Keyboard shortcuts

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