ansi

package module
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 3 Imported by: 9

README

go-ansi

ANSI escape sequences for Golang

demo image

Install:

go get github.com/bit101/go-ansi

Import:

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

Functions:

Set foreground (text) color
ansi.SetFg(fg AnsiColor) // see below for `AnsiColor` type

Affects the color of all text rendered with fmt.Print* functions. This color will remain active until it is changed or reset.

Set background color
ansi.SetBg(bg AnsiColor) // see below for `AnsiColor` type

Affects the background color of all text rendered with fmt.Print* functions. This color will remain active until it is changed or reset.

Set bold
ansi.SetBold(bold bool)

Affects whether text will be rendered in bold or regular. This setting will remain active until it is changed or reset.

Set underline
ansi.SetUnderline(underline bool)

Affects whether text will be rendered underlined or not. This setting will remain active until it is changed or reset.

Set reversed
ansi.SetReversed(reversed bool)

Affects whether text will be rendered with foreground and background reversed. This setting will remain active until it is changed or reset.

Set all properties
ansi.SetAll(fg AnsiColor, bg AnsiColor, bold bool, underline bool, reversed bool)

Sets all of the above properties in one shot.

ResetAll()
ansi.ResetAll()

Sets all of the above properties to their default values.

Print functions

All of the below are "one-shot" functions. They can only be used to set ONE of the following: foreground color, bold, underline, reversed.

The properties selected will only be in effect for the single print call. After the call, all properties will revert to what they were set to with the above functions.

In addition to passing an actual color, such as ansi.Red or ansi.Green, these functions will accept ansi.Bold, ansi.Underline and ansi.Reversed. If you use one of these three, the default color will be used.

If you use one of the bold colors, such as ansi.BoldRed, it has the effect of setting the color to red AND setting the bold flag to true.

Setting the background color is not supported with these methods.

Print
ansi.Print(color AnsiColor, message ...any)

Behaves exactly like fmt.Print but takes an AnsiColor as a first argument.

Print line
ansi.Println(color AnsiColor, message ...any)

Behaves exactly like fmt.Println but takes an AnsiColor as a first argument.

Print formatted
ansi.Printf(color AnsiColor, formatStr string, args ...any)

Behaves exactly like fmt.Printf but takes an AnsiColor as a first argument.

Fprint functions
ansi.Fprint(output io.Writer, color AnsiColor, message ...any)
ansi.Fprintln(output io.Writer, color AnsiColor, message ...any)
ansi.Fprintf(output io.Writer, color AnsiColor, formatStr string, args ...any)

Exactly like the other print functions, but print to the io.Writer of your choice.

The AnsiColor type

AnsiColors are a set of predefined values that encapsule text properties - mostly colors.

Colors

The following AnsiColors define actual colors. They can be used in ansi.SetFg, ansi.SetBg or any of the ansi.Print* functions:

ansi.Black
ansi.BoldBlack
ansi.Red
ansi.BoldRed
ansi.Green
ansi.BoldGreen
ansi.Yellow
ansi.BoldYellow
ansi.Blue
ansi.BoldBlue
ansi.Purple
ansi.BoldPurple
ansi.Cyan
ansi.BoldCyan
ansi.White
ansi.BoldWhite

Another special color value is:

ansi.Default

This can be used in all the same places and will set either the foreground or background color to its default value as defined within the terminal emulator / shell / colorscheme.

In addition, the following values can be used in any of the ansi.Print* functions and do what you would expect:

ansi.Bold
ansi.NotBold
ansi.Underline
ansi.NotUnderline
ansi.Reversed
ansi.NotReversed
Note on bold colors

If you choose one of the bold colors, such as ansi.BoldGreen, it has the effect of setting the color to green and bold to true. This is useful for the ansi.Print* functions. If you use a bold color in the ansi.SetFg function, it will set bold to true even if you previously called ansi.SetBold(false). See the following:

ansi.SetBold(false)
ansi.SetFg(ansi.BoldGreen)
fmt.Println("this will be bold green")

The order of operations reversed has the opposite effect:

ansi.SetFg(ansi.BoldGreen)
ansi.SetBold(false)
fmt.Println("this will just be green")

When using the ansi.SetAll function, the bold version of the color will be ignored and the bold flag will be based solely on the bold flag that you passed in as the third parameter, as demonstrated below.

ansi.SetAll(ansi.BoldGreen, ansi.Default, false, false, false)
fmt.Println("this will just be green")

Movements and Clearing

The following functions provide some ability to move the cursor to different positions in the shell and clear content from the screen. They pretty much do exactly what you would expect.

ansi.ClearLine()
ansi.ClearScreen()
ansi.Backspace(n int)
ansi.Tab()
ansi.CarriageReturn()
ansi.NewLine()
ansi.MoveTo(x, y int)
ansi.MoveUp(n int)
ansi.MoveDown(n int)
ansi.MoveRight(n int)
ansi.MoveLeft(n int)
ansi.Save() // saves position
ansi.Restore() // restores position
ansi.SetScrollRegion(start, end int)
ansi.ResetScrollRegion()
ansi.TermSize() // returns x and y int size of terminal

Known issues

If underline, reversed or a background color is set and then unset and a print call forces a scroll, the style may incorrectly extend to the next line. The following code demonstrates this:

ansi.SetReversed(true)
fmt.Println("reversed")
ansi.SetReversed(false)
fmt.Println("normal")

This is the result once the screen scrolls:

image issue

This is more of an underlying issue that is hard to fix within the library because it happens in fmt.Println. There is a fix in the library print methods though, so a workaround is to use them, like so:

ansi.SetReversed(true)
fmt.Println("reversed")
ansi.SetReversed(false)
ansi.Println(ansi.Default, "normal")

The result:

image fixed

Documentation

Overview

Package ansi allows for advanced terminal text manipulation.

Package ansi allows for advanced terminal text manipulation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Black sets the ansi color black.
	Black = AnsiColor{false, 30}
	// BoldBlack sets the ansi color black and bold.
	BoldBlack = AnsiColor{true, 30}
	// Red sets the ansi color red.
	Red = AnsiColor{false, 31}
	// BoldRed sets the ansi color red and bold.
	BoldRed = AnsiColor{true, 31}
	// Green sets the ansi color green.
	Green = AnsiColor{false, 32}
	// BoldGreen sets the ansi color green and bold.
	BoldGreen = AnsiColor{true, 32}
	// Yellow sets the ansi color yellow.
	Yellow = AnsiColor{false, 33}
	// BoldYellow sets the ansi color yellow and bold.
	BoldYellow = AnsiColor{true, 33}
	// Blue sets the ansi color blue.
	Blue = AnsiColor{false, 34}
	// BoldBlue sets the ansi color blue and bold.
	BoldBlue = AnsiColor{true, 34}
	// Purple sets the ansi color purple.
	Purple = AnsiColor{false, 35}
	// BoldPurple sets the ansi color purple and bold.
	BoldPurple = AnsiColor{true, 35}
	// Cyan sets the ansi color cyan.
	Cyan = AnsiColor{false, 36}
	// BoldCyan sets the ansi color cyan and bold.
	BoldCyan = AnsiColor{true, 36}
	// White sets the ansi color white.
	White = AnsiColor{false, 37}
	// BoldWhite sets the ansi color white and bold.
	BoldWhite = AnsiColor{true, 37}
	// Default sets the ansi color to the default color.
	Default = AnsiColor{false, 39}

	// Bold sets the text to be bold.
	Bold = AnsiColor{false, 1}
	// NotBold sets the text to be not bold.
	NotBold = AnsiColor{false, 22}
	// Underline sets the text to be underlined.
	Underline = AnsiColor{false, 4}
	// NotUnderline sets the text to be not underlined.
	NotUnderline = AnsiColor{false, 24}
	// Reversed sets the text to be reversed.
	Reversed = AnsiColor{false, 7}
	// NotReversed sets the text to be not reversed.
	NotReversed = AnsiColor{false, 27}
)

Functions

func Backspace

func Backspace(n int)

Backspace deletes the previous character.

func CarriageReturn

func CarriageReturn()

CarriageReturn inserts a carriage return character.

func ClearLine

func ClearLine()

ClearLine clears the current line.

func ClearScreen

func ClearScreen()

ClearScreen clears the screen.

func ClearToEnd

func ClearToEnd()

ClearToEnd clears from the cursor onwards.

func Fprint

func Fprint(output io.Writer, col AnsiColor, s ...any)

Fprint is a replacement for fmt.Fprint, which accepts an AnsiColor as the first argument.

func Fprintf

func Fprintf(output io.Writer, col AnsiColor, s string, args ...any)

Fprintf is a replacement for fmt.Fprintf, which accepts an AnsiColor as the first argument.

func Fprintln

func Fprintln(output io.Writer, col AnsiColor, s ...any)

Fprintln is a replacement for fmt.Fprintln, which accepts an AnsiColor as the first argument.

func GetPos

func GetPos() (int, int)

GetPos gets the current x, y position of the cursor.

func HideCursor added in v1.5.3

func HideCursor()

HideCursor hides the cursor

func MoveDown

func MoveDown(n int)

MoveDown moves the cursor down the specified number of lines.

func MoveLeft

func MoveLeft(n int)

MoveLeft moves the cursor to the left the specified number of spaces.

func MoveRight

func MoveRight(n int)

MoveRight moves the cursor to the right the specified number of spaces.

func MoveTo

func MoveTo(x, y int)

MoveTo moves the cursor to the specified position.

func MoveUp

func MoveUp(n int)

MoveUp moves the cursor up the specified number of lines.

func NewLine

func NewLine()

NewLine inserts a newline character.

func Print

func Print(col AnsiColor, s ...any)

Print is a replacement for fmt.Print, which accepts an AnsiColor as the first argument.

func Printf

func Printf(col AnsiColor, s string, args ...any)

Printf is a replacement for fmt.Printf, which accepts an AnsiColor as the first argument.

func Println

func Println(col AnsiColor, s ...any)

Println is a replacement for fmt.Println, which accepts an AnsiColor as the first argument.

func ResetAll

func ResetAll()

ResetAll resets all properties to default.

func ResetScrollRegion

func ResetScrollRegion()

ResetScrollRegion resets the scroll region.

func Restore

func Restore()

Restore restores the cursor to the last saved position.

func Save

func Save()

Save saves the current cursor position.

func SetAll

func SetAll(fg, bg AnsiColor, bold, underline, reversed bool)

SetAll sets the foreground, background, bold, underline and reversed properties at once.

func SetBg

func SetBg(bg AnsiColor)

SetBg sets the background text color.

func SetBold

func SetBold(bold bool)

SetBold sets the text to be bold or not.

func SetFg

func SetFg(fg AnsiColor)

SetFg sets the foreground text color.

func SetReversed

func SetReversed(reversed bool)

SetReversed sets the text to be reversed or not.

func SetScrollRegion

func SetScrollRegion(start, end int)

SetScrollRegion sets the start and end lines that the screen will scroll within.

func SetUnderline

func SetUnderline(underline bool)

SetUnderline sets the text to be underlined or not.

func ShowCursor added in v1.5.3

func ShowCursor()

ShowCursor shows the cursor

func Tab

func Tab()

Tab inserts a tab character.

func TermSize

func TermSize() (int, int)

TermSize returns the size of the terminal.

Types

type AnsiColor added in v1.5.4

type AnsiColor struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
Package main contains some examples of using the functions in the go-ansi module.
Package main contains some examples of using the functions in the go-ansi module.

Jump to

Keyboard shortcuts

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