GoColor

package module
v0.0.0-...-92a3887 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

GoColor

A small library to output colored text to the console
It uses ANSI color codes to output 24 bit (if available) deep colored text, and also supports bold and underline text

Usage

Install

To install the library, just copy the following command to your command line
go get github.com/Kollabiz/GoColor

Examples

The GoColor package has a set of functions that behave similarly to the built-in fmt library.
The naming of the functions is similar, with ...print...() functions outputting ANSI formatted strings and ...println...() functions adding a "\n" character to the end of the output string.

For example:

package main

import (
	"GoColor"
	"image/color"
)

func main() {
	// The PrintlnFg function writes a string formatted with ANSI background color
	// code to the console, appending it with "\n"
	GoColor.PrintlnFg("Hello, World!", color.RGBA{R:168, G:97, B:206})
}

Also, GoColor module has Set...() functions, which set color and bold/underline styles globally, until the next Reset function is called (or the color is overriden)

package main

import (
	"GoColor"
	"fmt"
	"image/color"
)

func main() {
	GoColor.SetBold()
	GoColor.SetForegroundColor(color.RGBA{R:168, G:97, B:206})
	// Now, everything outputted to the console will be bold and have the set foreground color
	fmt.Println("Hello, World!")
	// The color will remain until the next Reset() call, or until it is overriden with another GoColor function
	// Note: All Print...() functions add a reset sequence to the end of the string
	GoColor.PrintBg("Uh oh, the color got overriden!", color.RGBA{R:78,G:211,B:191})
	// Also, you can reset the bold/underline separately from color using respective functions
	GoColor.ResetBold()
}

Documentation

Index

Constants

View Source
const (
	Black          = 0
	DarkRed        = 1
	DarkGreen      = 2
	DarkYellow     = 3
	DarkBlue       = 4
	DarkMagenta    = 5
	DarkTurquoise  = 6
	Gray           = 7
	DarkGray       = 8
	LightRed       = 9
	LightGreen     = 10
	LightYellow    = 11
	LightBlue      = 12
	LightMagenta   = 13
	LightTurquoise = 14
	White          = 15
)

Variables

This section is empty.

Functions

func Clear

func Clear()

func Grayscale

func Grayscale(brightness int) int

func MoveCursor

func MoveCursor(x int, y int)

func Print

func Print(str string, colorFg color.Color, colorBg color.Color)

Print formats the given string with ANSI color codes and outputs it to the console

Param str: string to be formatted

Param colorFg: foreground color of the text

Param colorBg: background color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func Print256

func Print256(str string, colorFg int, colorBg int)

Print256 formats the given string with ANSI color codes and outputs it to the console

Param str: string to be formatted

Param colorFg: foreground color of the text, ranging from 0 to 255 (extended ANSI palette)

Param colorBg: background color of the text, ranging from 0 to 255 (extended ANSI palette)

func PrintBg

func PrintBg(str string, color color.Color)

PrintBg formats the given string with ANSI background color code and outputs it to the console

Param str: string to be formatted

Param color: background color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func PrintBg256

func PrintBg256(str string, color int)

PrintBg256 formats the given string with ANSI color codes and outputs it to the console

Param str: string to be formatted

Param color: background color of the text, ranging from 0 to 255 (extended ANSI palette)

func PrintBold

func PrintBold(str string)

PrintBold formats the given string with ANSI bold code and outputs it to the console

Param str: string to be formatted

func PrintFg

func PrintFg(str string, color color.Color)

PrintFg formats the given string with ANSI foreground color code and outputs it to the console

Param str: string to be formatted

Param color: foreground color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func PrintFg256

func PrintFg256(str string, color int)

PrintFg256 formats the given string with ANSI color codes and outputs it to the console

Param str: string to be formatted

Param color: foreground color of the text, ranging from 0 to 255 (extended ANSI palette)

func PrintUnderline

func PrintUnderline(str string)

PrintUnderline formats the given string with ANSI underline code and outputs it to the console

Param str: string to be formatted

func Println

func Println(str string, colorFg color.Color, colorBg color.Color)

Println formats the given string with ANSI color codes and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

Param colorFg: foreground color of the text

Param colorBg: background color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func Println256

func Println256(str string, colorFg int, colorBg int)

Println256 formats the given string with ANSI color codes and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

Param colorFg: foreground color of the text, ranging from 0 to 255 (extended ANSI palette)

Param colorBg: background color of the text, ranging from 0 to 255 (extended ANSI palette)

func PrintlnBg

func PrintlnBg(str string, color color.Color)

PrintlnBg formats the given string with ANSI background color code and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

Param color: background color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func PrintlnBg256

func PrintlnBg256(str string, color int)

PrintlnBg256 formats the given string with ANSI color codes and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

Param color: background color of the text, ranging from 0 to 255 (extended ANSI palette)

func PrintlnBold

func PrintlnBold(str string)

PrintlnBold formats the given string with ANSI bold code and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

func PrintlnFg

func PrintlnFg(str string, color color.Color)

PrintlnFg formats the given string with ANSI foreground color code and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

Param color: foreground color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func PrintlnFg256

func PrintlnFg256(str string, color int)

PrintlnFg256 formats the given string with ANSI color codes and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

Param color: foreground color of the text, ranging from 0 to 255 (extended ANSI palette)

func PrintlnUnderline

func PrintlnUnderline(str string)

PrintlnUnderline formats the given string with ANSI underline code and outputs it to the console, appending it with the "\n" character

Param str: string to be formatted

func Reset

func Reset()

Reset resets foreground, background colors and text styles

func ResetBold

func ResetBold()

ResetBold resets the bold style

func ResetCursor

func ResetCursor()

func ResetUnderline

func ResetUnderline()

ResetUnderline resets the underline style

func SetBackgroundColor

func SetBackgroundColor(color color.Color)

SetBackgroundColor sets the background color of the console output to given color until the next Reset call

Param color: background color to be set

func SetBackgroundColor256

func SetBackgroundColor256(color int)

SetBackgroundColor256 sets the background color of console output until the next Reset call

Param color: background color to be set, ranging from 0 to 255 (extended ANSI palette)

func SetBold

func SetBold()

SetBold sets the console output to be in bold style until the next ResetBold or Reset call

func SetForegroundColor

func SetForegroundColor(color color.Color)

SetForegroundColor sets the foreground color of the console output to given color until the next Reset call

Param color: foreground color to be set

func SetForegroundColor256

func SetForegroundColor256(color int)

SetForegroundColor256 sets the foreground color of console output until the next Reset call

Param color: foreground color to be set, ranging from 0 to 255 (extended ANSI palette)

func SetUnderline

func SetUnderline()

SetUnderline sets the console output to be in underline style until the next ResetUnderline or Reset call

func Sprint

func Sprint(str string, colorFg color.Color, colorBg color.Color) string

Sprint formats the given string with ANSI color codes and returns it

Param str: string to be formatted

Param colorFg: foreground color of the text

Param colorBg: background color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func SprintBg

func SprintBg(str string, color color.Color) string

SprintBg formats the given string with ANSI background color code and returns it

Param str: string to be formatted

Param color: background color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func SprintBold

func SprintBold(str string) string

SprintBold formats the given string with ANSI bold code and returns it

Param str: string to be formatted

func SprintFg

func SprintFg(str string, color color.Color) string

SprintFg formats the given string with ANSI foreground color code and returns it

Param str: string to be formatted

Param color: foreground color of the text

Note: Even if your console does not support RGB color codes, it will still select the closest color from its palette

func SprintUnderline

func SprintUnderline(str string) string

SprintUnderline formats the given string with ANSI underline code and returns it

Param str: string to be formatted

Types

This section is empty.

Jump to

Keyboard shortcuts

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