ansi

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 2 Imported by: 8

README

ansi

import "github.com/engmtcdrm/go-ansi"

Package ansi provides functions for creating ANSI escape sequences and determining their lengths.

Index

Constants

const (
    Escape = "\x1b"       // ANSI escape character.
    CSI    = Escape + "[" // ANSI control sequence introducer.
)

const (
    ClearFromCursorToEndScreen   = CSI + "0J"             // Clears from the cursor to the end of the screen.
    ClearFromCursorToBeginScreen = CSI + "1J"             // Clears from the cursor to the beginning of the screen.
    ClearScreen                  = CSI + "2J"             // Clear the entire screen.
    ClearToEnd                   = CSI + "K"              // Clear from the cursor to the end of the row (line).
    ClearToBegin                 = CSI + "1K"             // Clear from the cursor to the beginning of the row (line).
    ClearLine                    = CSI + "2K"             // Clear the entire row (line).
    ClearLineReset               = ClearLine + CSI + "1G" // Clear the entire row (line) and move the cursor to the start of the row (line).
)

ANSI color codes.

const (
    ANSIBlack = iota
    ANSIRed
    ANSIGreen
    ANSIYellow
    ANSIBlue
    ANSIMagenta
    ANSICyan
    ANSIWhite
    ANSIBrightBlack
    ANSIBrightRed
    ANSIBrightGreen
    ANSIBrightYellow
    ANSIBrightBlue
    ANSIBrightMagenta
    ANSIBrightCyan
    ANSIBrightWhite
)

const (
    Black   = CSI + "30m" // Black foreground color.
    Red     = CSI + "31m" // Red foreground color.
    Green   = CSI + "32m" // Green foreground color.
    Yellow  = CSI + "33m" // Yellow foreground color.
    Blue    = CSI + "34m" // Blue foreground color.
    Magenta = CSI + "35m" // Magenta foreground color.
    Cyan    = CSI + "36m" // Cyan foreground color.
    White   = CSI + "37m" // White foreground color.
    Default = CSI + "39m" // Default foreground color.
)

const (
    BlackBg   = CSI + "40m" // Black background color.
    RedBg     = CSI + "41m" // Red background color.
    GreenBg   = CSI + "42m" // Green background color.
    YellowBg  = CSI + "43m" // Yellow background color.
    BlueBg    = CSI + "44m" // Blue background color.
    MagentaBg = CSI + "45m" // Magenta background color.
    CyanBg    = CSI + "46m" // Cyan background color.
    WhiteBg   = CSI + "47m" // White background color.
    DefaultBg = CSI + "49m" // Default background color.
)

const (
    IntenseBlack   = CSI + "90m" // Intense Black foreground color.
    IntenseRed     = CSI + "91m" // Intense Red foreground color.
    IntenseGreen   = CSI + "92m" // Intense Green foreground color.
    IntenseYellow  = CSI + "93m" // Intense Yellow foreground color.
    IntenseBlue    = CSI + "94m" // Intense Blue foreground color.
    IntenseMagenta = CSI + "95m" // Intense Magenta foreground color.
    IntenseCyan    = CSI + "96m" // Intense Cyan foreground color.
    IntenseWhite   = CSI + "97m" // Intense White foreground color.

)

const (
    IntenseBlackBg   = CSI + "100m" // Intense Black background color.
    IntenseRedBg     = CSI + "101m" // Intense Red background color.
    IntenseGreenBg   = CSI + "102m" // Intense Green background color.
    IntenseYellowBg  = CSI + "103m" // Intense Yellow background color.
    IntenseBlueBg    = CSI + "104m" // Intense Blue background color.
    IntenseMagentaBg = CSI + "105m" // Intense Magenta background color.
    IntenseCyanBg    = CSI + "106m" // Intense Cyan background color.
    IntenseWhiteBg   = CSI + "107m" // Intense White background color.
)

const (
    SaveCursorPos    = Escape + "7" // Save the cursor position.
    RestoreCursorPos = Escape + "8" // Restore the cursor position.
    HideCursor       = CSI + "?25l" // Hide the cursor.
    ShowCursor       = CSI + "?25h" // Show the cursor.
)

const (
    Reset           = CSI + "0m"  // Reset all formatting.
    Bold            = CSI + "1m"  // Bold or increased intensity.
    Dim             = CSI + "2m"  // Dim, decreased intensity, or dim.
    Italic          = CSI + "3m"  // Italicized text.
    Underline       = CSI + "4m"  // Underlined text.
    SlowBlink       = CSI + "5m"  // Slow blinking text.
    RapidBlink      = CSI + "6m"  // Rapid blinking text.
    Reverse         = CSI + "7m"  // Reversed colors. Not widely supported.
    Hide            = CSI + "8m"  // Hide text. Not widely supported.
    Strike          = CSI + "9m"  // Strikethrough text.
    DoubleUnderline = CSI + "21m" // Double underlined text.

    ResetIntensity = CSI + "22m" // Reset bold or faint.
    ResetItalic    = CSI + "23m" // Reset italic.
    ResetUnderline = CSI + "24m" // Reset underline.
    ResetBlink     = CSI + "25m" // Reset blink.
    ResetReverse   = CSI + "27m" // Reset reverse.
    ResetHide      = CSI + "28m" // Reset hide.
    ResetStrike    = CSI + "29m" // Reset strikethrough.

    Framed         = CSI + "51m" // Framed text.
    Encircled      = CSI + "52m" // Encircled text.
    Overlined      = CSI + "53m" // Overlined text.
    ResetFramed    = CSI + "54m" // Reset framed and encircled.
    ResetOverlined = CSI + "55m" // Reset overlined.
    IdeogramRight  = CSI + "73m" // Ideogram right underline.
)

Variables

var (
    CursorTopLeft      = CursorPosition(1, 1)        // Move the cursor to the top left corner of screen.
    CursorLineBegin    = CursorHorizontalAbsolute(1) // Move the cursor to beginning of the row (line).
    CursorNextLine     = CursorNextLineN(1)          // Move cursor down one row (line).
    CursorPreviousLine = CursorPreviousLineN(1)      // Move cursor up one row (line).
)

var (
    ScrollUp1   = ScrollUp(1)   // Scroll the screen up one row (line).
    ScrollDown1 = ScrollDown(1) // Scroll the screen down one row (line).
)

func Background24Bit

func Background24Bit(r, g, b int) string

24-bit background color r, g, b must be between 0 and 255 otherwise it will return an empty string.

func Background8Bit

func Background8Bit(color int) string

8-bit background color color must be between 0 and 255 otherwise it will return an empty string.

func CursorBackward

func CursorBackward(n int) string

CursorBackward moves the cursor backward n columns. If n is less than 1, it returns an empty string.

func CursorDown

func CursorDown(n int) string

CursorDown moves the cursor down n rows (lines). If n is less than 1, it returns an empty string.

func CursorForward

func CursorForward(n int) string

CursorForward moves the cursor forward n columns. If n is less than 1, it returns an empty string.

func CursorHorizontalAbsolute

func CursorHorizontalAbsolute(n int) string

CursorHorizontalAbsolute moves the cursor to the nth column. If n is less than 1, it returns an empty string.

func CursorNextLineN

func CursorNextLineN(n int) string

CursorNextLine moves the cursor down n rows (lines). If n is less than 1, it returns an empty string.

func CursorPosition

func CursorPosition(row, column int) string

CursorPosition moves the cursor to the specified position of row (line) and column. If either row or column are less than 1, it returns an empty string.

func CursorPreviousLineN

func CursorPreviousLineN(n int) string

CursorPreviousLine moves the cursor up n rows (lines). If n is less than 1, it returns an empty string.

func CursorUp

func CursorUp(n int) string

CursorUp moves the cursor up n rows (lines). If n is less than 1, it returns an empty string.

func EscapeLength

func EscapeLength[T ~string | ~[]byte | ~[]rune](input T) int

EscapeLength returns the byte length of a valid 7-bit ANSI escape sequence at the start of input, or 0 if none.

Recognized forms (ECMA-48 / ISO 6429):

  • CSI: ESC [ then parameter bytes (0x30-0x3F), intermediate (0x20-0x2F), final (0x40-0x7E)
  • OSC: ESC ] then payload until BEL (0x07), 7-bit ST (ESC \), CAN (0x18), or SUB (0x1A)
  • DCS, SOS, PM, APC: ESC P/X/^/_ then payload until 7-bit ST (ESC \), CAN, or SUB
  • Two-byte: ESC + Fe/Fs (0x40-0x7E excluding above), or Fp (0x30-0x3F), or nF (0x20-0x2F then final)

func EscapeLength8Bit

func EscapeLength8Bit[T ~string | ~[]byte | ~[]rune](input T) int

EscapeLength8Bit returns the byte length of a valid 8-bit C1 ANSI sequence at the start of input, or 0 if none.

Recognized forms (ECMA-48 / ISO 6429):

  • C1 CSI (0x9B) body as parameter/intermediate/final bytes
  • C1 OSC (0x9D) body terminated by BEL, C1 ST, CAN, or SUB
  • C1 DCS/SOS/PM/APC (0x90/0x98/0x9E/0x9F) body terminated by C1 ST, CAN, or SUB
  • Standalone C1 controls (0x80..0x9F not listed above): single byte

func Foreground24Bit

func Foreground24Bit(r, g, b int) string

24-bit foreground color r, g, b must be between 0 and 255 otherwise it will return an empty string.

func Foreground8Bit

func Foreground8Bit(color int) string

8-bit foreground color color must be between 0 and 255 otherwise it will return an empty string.

func ScrollDown

func ScrollDown(n int) string

ScrollDown scrolls the screen down n rows (lines). If n is less than 1, it returns an empty string.

func ScrollUp

func ScrollUp(n int) string

ScrollUp scrolls the screen up n rows (lines). If n is less than 1, it returns an empty string.

func Strip

func Strip[T ~string | ~[]byte](input T) T

Strip removes all 7-bit ANSI escape sequences from the input.

func Strip8Bit

func Strip8Bit[T ~string | ~[]byte](input T) T

Strip8Bit removes all 8-bit C1 ANSI escape sequences from the input.

func Strip8BitRunes

func Strip8BitRunes[T ~[]rune](input T) T

Strip8BitRunes removes all 8-bit C1 ANSI escape sequences from the input.

func StripRunes

func StripRunes[T ~[]rune](input T) T

StripRunes removes all 7-bit ANSI escape sequences from input.

Generated by gomarkdoc

Documentation

Overview

Package ansi provides functions for creating ANSI escape sequences and determining their lengths.

Index

Constants

View Source
const (
	Escape = "\x1b"       // ANSI escape character.
	CSI    = Escape + "[" // ANSI control sequence introducer.
)
View Source
const (
	ClearFromCursorToEndScreen   = CSI + "0J"             // Clears from the cursor to the end of the screen.
	ClearFromCursorToBeginScreen = CSI + "1J"             // Clears from the cursor to the beginning of the screen.
	ClearScreen                  = CSI + "2J"             // Clear the entire screen.
	ClearToEnd                   = CSI + "K"              // Clear from the cursor to the end of the row (line).
	ClearToBegin                 = CSI + "1K"             // Clear from the cursor to the beginning of the row (line).
	ClearLine                    = CSI + "2K"             // Clear the entire row (line).
	ClearLineReset               = ClearLine + CSI + "1G" // Clear the entire row (line) and move the cursor to the start of the row (line).
)
View Source
const (
	ANSIBlack = iota
	ANSIRed
	ANSIGreen
	ANSIYellow
	ANSIBlue
	ANSIMagenta
	ANSICyan
	ANSIWhite
	ANSIBrightBlack
	ANSIBrightRed
	ANSIBrightGreen
	ANSIBrightYellow
	ANSIBrightBlue
	ANSIBrightMagenta
	ANSIBrightCyan
	ANSIBrightWhite
)

ANSI color codes.

View Source
const (
	Black   = CSI + "30m" // Black foreground color.
	Red     = CSI + "31m" // Red foreground color.
	Green   = CSI + "32m" // Green foreground color.
	Yellow  = CSI + "33m" // Yellow foreground color.
	Blue    = CSI + "34m" // Blue foreground color.
	Magenta = CSI + "35m" // Magenta foreground color.
	Cyan    = CSI + "36m" // Cyan foreground color.
	White   = CSI + "37m" // White foreground color.
	Default = CSI + "39m" // Default foreground color.
)
View Source
const (
	BlackBg   = CSI + "40m" // Black background color.
	RedBg     = CSI + "41m" // Red background color.
	GreenBg   = CSI + "42m" // Green background color.
	YellowBg  = CSI + "43m" // Yellow background color.
	BlueBg    = CSI + "44m" // Blue background color.
	MagentaBg = CSI + "45m" // Magenta background color.
	CyanBg    = CSI + "46m" // Cyan background color.
	WhiteBg   = CSI + "47m" // White background color.
	DefaultBg = CSI + "49m" // Default background color.
)
View Source
const (
	IntenseBlack   = CSI + "90m" // Intense Black foreground color.
	IntenseRed     = CSI + "91m" // Intense Red foreground color.
	IntenseGreen   = CSI + "92m" // Intense Green foreground color.
	IntenseYellow  = CSI + "93m" // Intense Yellow foreground color.
	IntenseBlue    = CSI + "94m" // Intense Blue foreground color.
	IntenseMagenta = CSI + "95m" // Intense Magenta foreground color.
	IntenseCyan    = CSI + "96m" // Intense Cyan foreground color.
	IntenseWhite   = CSI + "97m" // Intense White foreground color.

)
View Source
const (
	IntenseBlackBg   = CSI + "100m" // Intense Black background color.
	IntenseRedBg     = CSI + "101m" // Intense Red background color.
	IntenseGreenBg   = CSI + "102m" // Intense Green background color.
	IntenseYellowBg  = CSI + "103m" // Intense Yellow background color.
	IntenseBlueBg    = CSI + "104m" // Intense Blue background color.
	IntenseMagentaBg = CSI + "105m" // Intense Magenta background color.
	IntenseCyanBg    = CSI + "106m" // Intense Cyan background color.
	IntenseWhiteBg   = CSI + "107m" // Intense White background color.
)
View Source
const (
	SaveCursorPos    = Escape + "7" // Save the cursor position.
	RestoreCursorPos = Escape + "8" // Restore the cursor position.
	HideCursor       = CSI + "?25l" // Hide the cursor.
	ShowCursor       = CSI + "?25h" // Show the cursor.
)
View Source
const (
	Reset           = CSI + "0m"  // Reset all formatting.
	Bold            = CSI + "1m"  // Bold or increased intensity.
	Dim             = CSI + "2m"  // Dim, decreased intensity, or dim.
	Italic          = CSI + "3m"  // Italicized text.
	Underline       = CSI + "4m"  // Underlined text.
	SlowBlink       = CSI + "5m"  // Slow blinking text.
	RapidBlink      = CSI + "6m"  // Rapid blinking text.
	Reverse         = CSI + "7m"  // Reversed colors. Not widely supported.
	Hide            = CSI + "8m"  // Hide text. Not widely supported.
	Strike          = CSI + "9m"  // Strikethrough text.
	DoubleUnderline = CSI + "21m" // Double underlined text.

	ResetIntensity = CSI + "22m" // Reset bold or faint.
	ResetItalic    = CSI + "23m" // Reset italic.
	ResetUnderline = CSI + "24m" // Reset underline.
	ResetBlink     = CSI + "25m" // Reset blink.
	ResetReverse   = CSI + "27m" // Reset reverse.
	ResetHide      = CSI + "28m" // Reset hide.
	ResetStrike    = CSI + "29m" // Reset strikethrough.

	Framed         = CSI + "51m" // Framed text.
	Encircled      = CSI + "52m" // Encircled text.
	Overlined      = CSI + "53m" // Overlined text.
	ResetFramed    = CSI + "54m" // Reset framed and encircled.
	ResetOverlined = CSI + "55m" // Reset overlined.
	IdeogramRight  = CSI + "73m" // Ideogram right underline.
)

Variables

View Source
var (
	CursorTopLeft      = CursorPosition(1, 1)        // Move the cursor to the top left corner of screen.
	CursorLineBegin    = CursorHorizontalAbsolute(1) // Move the cursor to beginning of the row (line).
	CursorNextLine     = CursorNextLineN(1)          // Move cursor down one row (line).
	CursorPreviousLine = CursorPreviousLineN(1)      // Move cursor up one row (line).
)
View Source
var (
	ScrollUp1   = ScrollUp(1)   // Scroll the screen up one row (line).
	ScrollDown1 = ScrollDown(1) // Scroll the screen down one row (line).
)

Functions

func Background8Bit

func Background8Bit(color int) string

8-bit background color color must be between 0 and 255 otherwise it will return an empty string.

func Background24Bit

func Background24Bit(r, g, b int) string

24-bit background color r, g, b must be between 0 and 255 otherwise it will return an empty string.

func CursorBackward

func CursorBackward(n int) string

CursorBackward moves the cursor backward n columns. If n is less than 1, it returns an empty string.

func CursorDown

func CursorDown(n int) string

CursorDown moves the cursor down n rows (lines). If n is less than 1, it returns an empty string.

func CursorForward

func CursorForward(n int) string

CursorForward moves the cursor forward n columns. If n is less than 1, it returns an empty string.

func CursorHorizontalAbsolute

func CursorHorizontalAbsolute(n int) string

CursorHorizontalAbsolute moves the cursor to the nth column. If n is less than 1, it returns an empty string.

func CursorNextLineN

func CursorNextLineN(n int) string

CursorNextLine moves the cursor down n rows (lines). If n is less than 1, it returns an empty string.

func CursorPosition

func CursorPosition(row, column int) string

CursorPosition moves the cursor to the specified position of row (line) and column. If either row or column are less than 1, it returns an empty string.

func CursorPreviousLineN

func CursorPreviousLineN(n int) string

CursorPreviousLine moves the cursor up n rows (lines). If n is less than 1, it returns an empty string.

func CursorUp

func CursorUp(n int) string

CursorUp moves the cursor up n rows (lines). If n is less than 1, it returns an empty string.

func EscapeLength added in v1.1.0

func EscapeLength[T ~string | ~[]byte | ~[]rune](input T) int

EscapeLength returns the byte length of a valid 7-bit ANSI escape sequence at the start of input, or 0 if none.

Recognized forms (ECMA-48 / ISO 6429):

  • CSI: ESC [ then parameter bytes (0x30-0x3F), intermediate (0x20-0x2F), final (0x40-0x7E)
  • OSC: ESC ] then payload until BEL (0x07), 7-bit ST (ESC \), CAN (0x18), or SUB (0x1A)
  • DCS, SOS, PM, APC: ESC P/X/^/_ then payload until 7-bit ST (ESC \), CAN, or SUB
  • Two-byte: ESC + Fe/Fs (0x40-0x7E excluding above), or Fp (0x30-0x3F), or nF (0x20-0x2F then final)

func EscapeLength8Bit added in v1.1.0

func EscapeLength8Bit[T ~string | ~[]byte | ~[]rune](input T) int

EscapeLength8Bit returns the byte length of a valid 8-bit C1 ANSI sequence at the start of input, or 0 if none.

Recognized forms (ECMA-48 / ISO 6429):

  • C1 CSI (0x9B) body as parameter/intermediate/final bytes
  • C1 OSC (0x9D) body terminated by BEL, C1 ST, CAN, or SUB
  • C1 DCS/SOS/PM/APC (0x90/0x98/0x9E/0x9F) body terminated by C1 ST, CAN, or SUB
  • Standalone C1 controls (0x80..0x9F not listed above): single byte

func Foreground8Bit

func Foreground8Bit(color int) string

8-bit foreground color color must be between 0 and 255 otherwise it will return an empty string.

func Foreground24Bit

func Foreground24Bit(r, g, b int) string

24-bit foreground color r, g, b must be between 0 and 255 otherwise it will return an empty string.

func ScrollDown added in v1.0.2

func ScrollDown(n int) string

ScrollDown scrolls the screen down n rows (lines). If n is less than 1, it returns an empty string.

func ScrollUp added in v1.0.2

func ScrollUp(n int) string

ScrollUp scrolls the screen up n rows (lines). If n is less than 1, it returns an empty string.

func Strip added in v1.0.2

func Strip[T ~string | ~[]byte](input T) T

Strip removes all 7-bit ANSI escape sequences from the input.

func Strip8Bit added in v1.1.0

func Strip8Bit[T ~string | ~[]byte](input T) T

Strip8Bit removes all 8-bit C1 ANSI escape sequences from the input.

func Strip8BitRunes added in v1.1.0

func Strip8BitRunes[T ~[]rune](input T) T

Strip8BitRunes removes all 8-bit C1 ANSI escape sequences from the input.

func StripRunes added in v1.1.0

func StripRunes[T ~[]rune](input T) T

StripRunes removes all 7-bit ANSI escape sequences from input.

Types

This section is empty.

Jump to

Keyboard shortcuts

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