ansi

package module
v0.0.0-...-261416c Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 6 Imported by: 1

README

License Contributors Stars Issues Pull requests


ansi-go

✨ ANSI escape sequences wrapper library for Go

Documentation · Report Bug · Request Feature


Table of contents

Introduction

Since i like to always reinvent the wheel, heres the 1000th wrapper library around ANSI escape sequences for Go.

Usage

Add this package to your project

$ go get github.com/LordOfTrident/ansi-go

And import it

package main

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

func main() {
	style := ansi.Bold + ansi.LightMagenta

	ansi.Println(ansi.ClearScreen, ansi.Goto(1, 1), style, "Hello, world!")
}

Example

$ cd example
$ go build
$ ./example

Documentation

This library is very small and the code itself is pretty much documentation. Just look around the source files.

Bugs

If you find any bugs, please, create an issue and report them.



Go ANSI

Made with ❤️ love

(Back to top)

Documentation

Index

Constants

View Source
const (
	Reset     = "\x1b[0m"
	Bold      = "\x1b[1m"
	Dim       = "\x1b[2m"
	Italic    = "\x1b[3m"
	Underline = "\x1b[4m"
	Blink     = "\x1b[5m"
	Reverse   = "\x1b[7m"
	Invisible = "\x1b[8m"
	Crossout  = "\x1b[9m"
)
View Source
const (
	Black   = 0
	Red     = 1
	Green   = 2
	Yellow  = 3
	Blue    = 4
	Magenta = 5
	Cyan    = 6
	White   = 7

	LightBlack   = 8
	LightRed     = 9
	LightGreen   = 10
	LightYellow  = 11
	LightBlue    = 12
	LightMagenta = 13
	LightCyan    = 14
	LightWhite   = 15

	BlackFg   = "\x1b[30m"
	RedFg     = "\x1b[31m"
	GreenFg   = "\x1b[32m"
	YellowFg  = "\x1b[33m"
	BlueFg    = "\x1b[34m"
	MagentaFg = "\x1b[35m"
	CyanFg    = "\x1b[36m"
	WhiteFg   = "\x1b[37m"

	BlackBg   = "\x1b[40m"
	RedBg     = "\x1b[41m"
	GreenBg   = "\x1b[42m"
	YellowBg  = "\x1b[43m"
	BlueBg    = "\x1b[44m"
	MagentaBg = "\x1b[45m"
	CyanBg    = "\x1b[46m"
	WhiteBg   = "\x1b[47m"

	LightBlackFg   = "\x1b[90m"
	LightRedFg     = "\x1b[91m"
	LightGreenFg   = "\x1b[92m"
	LightYellowFg  = "\x1b[93m"
	LightBlueFg    = "\x1b[94m"
	LightMagentaFg = "\x1b[95m"
	LightCyanFg    = "\x1b[96m"
	LightWhiteFg   = "\x1b[97m"

	LightBlackBg   = "\x1b[100m"
	LightRedBg     = "\x1b[101m"
	LightGreenBg   = "\x1b[102m"
	LightYellowBg  = "\x1b[103m"
	LightBlueBg    = "\x1b[104m"
	LightMagentaBg = "\x1b[105m"
	LightCyanBg    = "\x1b[106m"
	LightWhiteBg   = "\x1b[107m"
)
View Source
const (
	SaveCursor        = "\x1b[s"
	RestoreCursor     = "\x1b[u"
	SaveCursorAttr    = "\x1b7"
	RestoreCursorAttr = "\x1b8"
	GetCursorPosition = "\x1b[6n"
)
View Source
const (
	ScrollDown         = "\x1bD"
	ScrollUp           = "\x1bM"
	ScrollScreen       = "\x1br"
	ClearScreen        = "\x1b[2J"
	ClearToScreenStart = "\x1b[1J"
	ClearToScreenEnd   = "\x1b[J"
	ClearLine          = "\x1b[2K"
	ClearToLineStart   = "\x1b[1K"
	ClearToLineEnd     = "\x1b[K"
	SaveScreen         = "\x1b[?47h"
	RestoreScreen      = "\x1b[?47l"
)
View Source
const Marker = '\x1b'
View Source
const (
	ResetDevice = "\x1bc"
)

Variables

View Source
var Regex = regexp.MustCompile("[\u001b\u009b][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z" +
	"\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf" +
	"-ntqry=><~]))")

Regex from https://github.com/acarl005/stripansi

Functions

func AltScreen

func AltScreen(enable bool) string

func Bg

func Bg(color uint8) string

func BgHex

func BgHex(hex string) string

func BgRGB

func BgRGB(r uint8, g uint8, b uint8) string

func Contains

func Contains(text string) bool

func Count

func Count(text string) int

func Cursor

func Cursor(enable bool) string

func CursorDown

func CursorDown(n int) string

func CursorLeft

func CursorLeft(n int) string

func CursorRight

func CursorRight(n int) string

func CursorUp

func CursorUp(n int) string

func Eprint

func Eprint(args ...any) (int, error)

func Eprintf

func Eprintf(format string, args ...any) (int, error)

Stderr variants

func Eprintln

func Eprintln(args ...any) (int, error)

func Fg

func Fg(color uint8) string

TODO: If terminal does not support truecolor or 256 colors, find the closest supported color

func FgHex

func FgHex(hex string) string

func FgRGB

func FgRGB(r uint8, g uint8, b uint8) string

func FindNext

func FindNext(text string) []int

func Fprint

func Fprint(w io.Writer, args ...any) (int, error)

func Fprintf

func Fprintf(w io.Writer, format string, args ...any) (int, error)

func Fprintln

func Fprintln(w io.Writer, args ...any) (int, error)

func Goto

func Goto(row int, col int) string

func InclusiveSlice

func InclusiveSlice(text string, start int, end int) string

Include the ansi sequences before the first character

func IsBright16

func IsBright16(color uint8) bool

func LengthNoAnsi

func LengthNoAnsi(text string) int

func LineWrap

func LineWrap(enable bool) string

func Print

func Print(args ...any) (int, error)

func Printf

func Printf(format string, args ...any) (int, error)

Stdout variants

func Println

func Println(args ...any) (int, error)

func RunePosToByteIndex

func RunePosToByteIndex(text string, runePos int) int

Convert rune index without ansi sequences to index including ansi sequences

func ScrollViewport

func ScrollViewport(start int, end int) string

func SizeNoAnsi

func SizeNoAnsi(text string) int

func Slice

func Slice(text string, start int, end int) string

func Strip

func Strip(text string) string

func Title

func Title(title string) string

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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