Documentation
¶
Overview ¶
Package ansi provides functions for creating ANSI escape sequences and determining their lengths.
Index ¶
- Constants
- Variables
- func Background8Bit(color int) string
- func Background24Bit(r, g, b int) string
- func CursorBackward(n int) string
- func CursorDown(n int) string
- func CursorForward(n int) string
- func CursorHorizontalAbsolute(n int) string
- func CursorNextLineN(n int) string
- func CursorPosition(row, column int) string
- func CursorPreviousLineN(n int) string
- func CursorUp(n int) string
- func EscapeLength[T ~string | ~[]byte | ~[]rune](input T) int
- func EscapeLength8Bit[T ~string | ~[]byte | ~[]rune](input T) int
- func Foreground8Bit(color int) string
- func Foreground24Bit(r, g, b int) string
- func ScrollDown(n int) string
- func ScrollUp(n int) string
- func Strip[T ~string | ~[]byte](input T) T
- func Strip8Bit[T ~string | ~[]byte](input T) T
- func Strip8BitRunes[T ~[]rune](input T) T
- func StripRunes[T ~[]rune](input T) T
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). )
const ( ANSIBlack = iota ANSIRed ANSIGreen ANSIYellow ANSIBlue ANSIMagenta ANSICyan ANSIWhite ANSIBrightBlack ANSIBrightRed ANSIBrightGreen ANSIBrightYellow ANSIBrightBlue ANSIBrightMagenta ANSIBrightCyan ANSIBrightWhite )
ANSI color codes.
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). )
Functions ¶
func Background8Bit ¶
8-bit background color color must be between 0 and 255 otherwise it will return an empty string.
func Background24Bit ¶
24-bit background color r, g, b must be between 0 and 255 otherwise it will return an empty string.
func CursorBackward ¶
CursorBackward moves the cursor backward n columns. If n is less than 1, it returns an empty string.
func CursorDown ¶
CursorDown moves the cursor down n rows (lines). If n is less than 1, it returns an empty string.
func CursorForward ¶
CursorForward moves the cursor forward n columns. If n is less than 1, it returns an empty string.
func CursorHorizontalAbsolute ¶
CursorHorizontalAbsolute moves the cursor to the nth column. If n is less than 1, it returns an empty string.
func CursorNextLineN ¶
CursorNextLine moves the cursor down n rows (lines). If n is less than 1, it returns an empty string.
func CursorPosition ¶
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 ¶
CursorPreviousLine moves the cursor up n rows (lines). If n is less than 1, it returns an empty string.
func CursorUp ¶
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
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
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 ¶
8-bit foreground color color must be between 0 and 255 otherwise it will return an empty string.
func Foreground24Bit ¶
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
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
ScrollUp scrolls the screen up n rows (lines). If n is less than 1, it returns an empty string.
func Strip8Bit ¶ added in v1.1.0
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.