termcolors

package module
v0.0.0-...-e2c1f9f Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 4 Imported by: 0

README

Go TermColors

The package was designed around pulling in the ANSI codes the being able to colorize the terminal with minimal effort. The package tests that it runs in both interactive and non interactive environments.

ANSI Codes:

Black = "\033[30m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
Reset = "\033[0m"
Clear = "\033[H\033[2J"
CurOn = "\033[?25h"
CurOff = "\033[?25l"

termcolor.go:

package termcolors

import (
	"fmt"
	"math/rand"
	"os"

	"golang.org/x/term"
)

var (
	forcedColorOn = false
	Black         = ""
	K             = ""
	Red           = ""
	R             = ""
	Green         = ""
	G             = ""
	Yellow        = ""
	Y             = ""
	Blue          = ""
	B             = ""
	Magenta       = ""
	M             = ""
	Cyan          = ""
	C             = ""
	White         = ""
	W             = ""
	Reset         = ""
	X             = ""
	Clear         = ""
	CurOn         = ""
	CurOff        = ""
)

func init() {
	checkingInteractive(SetColor)
}

// Sets the terminal color variables if the stdout is missing. Telling us that
// the terminal is likely non-interactive.
func SetColor() {
	Black = "\033[30m"
	K = Black
	Red = "\033[31m"
	R = Red
	Green = "\033[32m"
	G = Green
	Yellow = "\033[33m"
	Y = Yellow
	Blue = "\033[34m"
	B = Blue
	Magenta = "\033[35m"
	M = Magenta
	Cyan = "\033[36m"
	C = Cyan
	White = "\033[37m"
	W = White
	Reset = "\033[0m"
	X = Reset
	Clear = "\033[H\033[2J"
	CurOn = "\033[?25h"
	CurOff = "\033[?25l"
}

// Checking if interactive terminal and running the function taken in from
// input if the terminal is interactive
func checkingInteractive(fn func()) {
	if term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) {
		forcedColorOn = true
		fn()
	}
}

// Rand returns a random color ANSI escape between Black (30) and White (37)
func RandomColor(forceColorOns ...bool) string {
	if len(forceColorOns) > 0 {
		forcedColorOn = forceColorOns[0]
	}

	if forcedColorOn {
		var b [1]byte
		rand.Read(b[:])
		colorCode := int(b[0]) % 8
		color := fmt.Sprintf("\033[3%dm", colorCode)
		return color
	}
	return ""
}

If you would like to see examples you can checkout and run the tests.

One thing I want to mention. If you turn the color on. You also have to clear the color afterwards. Or the terminal will maintain the color.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Black   = ""
	K       = ""
	Red     = ""
	R       = ""
	Green   = ""
	G       = ""
	Yellow  = ""
	Y       = ""
	Blue    = ""
	B       = ""
	Magenta = ""
	M       = ""
	Cyan    = ""
	C       = ""
	White   = ""
	W       = ""
	Reset   = ""
	X       = ""
	Clear   = ""
	CurOn   = ""
	CurOff  = ""
)

Functions

func RandomColor

func RandomColor(forceColorOns ...bool) string

RandomColor returns a random color ANSI escape between Black (30) and White (37)

Example (Interactive)
package main

import (
	"fmt"
	"strings"

	C "github.com/ScriptAlchemist/go-termcolor"
)

func main() {
	string := C.RandomColor(true)
	colorAdded := strings.HasPrefix(string, "\x1b[3")
	fmt.Println(colorAdded)

}
Output:

true
Example (Noninteractive)
package main

import (
	"fmt"

	C "github.com/ScriptAlchemist/go-termcolor"
)

func main() {
	fmt.Printf("%q\n", C.RandomColor())

}
Output:

""

func SetColor

func SetColor()

SetColor will place the color ANSI escape characters in place of the empty strings. Allowing color to be used.

Example (Interactive)
package main

import (
	"fmt"

	C "github.com/ScriptAlchemist/go-termcolor"
)

func main() {
	C.SetColor()
	fmt.Printf("%q\n", C.Black+"black"+C.Reset)
	fmt.Printf("%q\n", C.Red+"red"+C.Reset)
	fmt.Printf("%q\n", C.Green+"green"+C.Reset)
	fmt.Printf("%q\n", C.Yellow+"yellow"+C.Reset)
	fmt.Printf("%q\n", C.Blue+"blue"+C.Reset)
	fmt.Printf("%q\n", C.Magenta+"magenta"+C.Reset)
	fmt.Printf("%q\n", C.Cyan+"cyan"+C.Reset)
	fmt.Printf("%q\n", C.White+"white"+C.Reset)

}
Output:

"\x1b[30mblack\x1b[0m"
"\x1b[31mred\x1b[0m"
"\x1b[32mgreen\x1b[0m"
"\x1b[33myellow\x1b[0m"
"\x1b[34mblue\x1b[0m"
"\x1b[35mmagenta\x1b[0m"
"\x1b[36mcyan\x1b[0m"
"\x1b[37mwhite\x1b[0m"
Example (Noninteractive)
package main

import (
	"fmt"

	C "github.com/ScriptAlchemist/go-termcolor"
)

func main() {
	fmt.Printf("%q\n", C.Black+"black"+C.Reset)
	fmt.Printf("%q\n", C.Red+"red"+C.Reset)
	fmt.Printf("%q\n", C.Green+"green"+C.Reset)
	fmt.Printf("%q\n", C.Yellow+"yellow"+C.Reset)
	fmt.Printf("%q\n", C.Blue+"blue"+C.Reset)
	fmt.Printf("%q\n", C.Magenta+"magenta"+C.Reset)
	fmt.Printf("%q\n", C.Cyan+"cyan"+C.Reset)
	fmt.Printf("%q\n", C.White+"white"+C.Reset)

}
Output:

"black"
"red"
"green"
"yellow"
"blue"
"magenta"
"cyan"
"white"
Example (Shortcutinteractive)
package main

import (
	"fmt"

	C "github.com/ScriptAlchemist/go-termcolor"
)

func main() {
	fmt.Printf("%q\n", C.K+"black"+C.X)
	fmt.Printf("%q\n", C.R+"red"+C.X)
	fmt.Printf("%q\n", C.G+"green"+C.X)
	fmt.Printf("%q\n", C.Y+"yellow"+C.X)
	fmt.Printf("%q\n", C.B+"blue"+C.X)
	fmt.Printf("%q\n", C.M+"magenta"+C.X)
	fmt.Printf("%q\n", C.C+"cyan"+C.X)
	fmt.Printf("%q\n", C.W+"white"+C.X)

}
Output:

"\x1b[30mblack\x1b[0m"
"\x1b[31mred\x1b[0m"
"\x1b[32mgreen\x1b[0m"
"\x1b[33myellow\x1b[0m"
"\x1b[34mblue\x1b[0m"
"\x1b[35mmagenta\x1b[0m"
"\x1b[36mcyan\x1b[0m"
"\x1b[37mwhite\x1b[0m"
Example (Shortcutnoninteractive)
package main

import (
	"fmt"

	C "github.com/ScriptAlchemist/go-termcolor"
)

func main() {
	fmt.Printf("%q\n", C.K+"black"+C.X)
	fmt.Printf("%q\n", C.R+"red"+C.X)
	fmt.Printf("%q\n", C.G+"green"+C.X)
	fmt.Printf("%q\n", C.Y+"yellow"+C.X)
	fmt.Printf("%q\n", C.B+"blue"+C.X)
	fmt.Printf("%q\n", C.M+"magenta"+C.X)
	fmt.Printf("%q\n", C.C+"cyan"+C.X)
	fmt.Printf("%q\n", C.W+"white"+C.X)

}
Output:

"black"
"red"
"green"
"yellow"
"blue"
"magenta"
"cyan"
"white"

Types

This section is empty.

Directories

Path Synopsis
cmd
nyan command

Jump to

Keyboard shortcuts

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