color

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

README

go-color

This is a Go library for colorizing strings.

Usage

To give a color name, write as follows:

package main

import (
	"fmt"
	"log"

	"github.com/ttkzw/go-color"
)

func main() {
	c, err := color.NewColor("Green")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(c.Colorize("OK"))
}

To use built-in constants, write as follows:

package main

import (
	"fmt"

	"github.com/ttkzw/go-color"
)

func main() {
	fmt.Println(color.Green.Colorize("OK"))
}

To use a background color, write as follows:

package main

import (
	"fmt"

	"github.com/ttkzw/go-color"
)

func main() {
	fmt.Println(color.Colorize("OK", color.White, color.GreenBackground))
}

If you want to support Windows, use go-colorable.

package main

import (
	"fmt"

	"github.com/mattn/go-colorable"
	"github.com/ttkzw/go-color"
)

func main() {
	var output = colorable.NewColorableStdout()

	fmt.Fprintln(output, color.Green.Colorize("OK"))
}

Supported colors

Installation

$ go get github.com/ttkzw/go-color

License

BSD 3-Clause License

Auther

Takashi Takizawa

Documentation

Index

Constants

View Source
const (
	// Default foreground color
	Default = Color(iota)

	// Black for system foreground color
	SystemBlack

	// Red for system foreground color
	SystemRed

	// Green for system foreground color
	SystemGreen

	// Yellow for system foreground color
	SystemYellow

	// Blue for system foreground color
	SystemBlue

	// Magenta for system foreground color
	SystemMagenta

	// Cyan for system foreground color
	SystemCyan

	// White for system foreground color
	SystemWhite

	// BrightBlack for system foreground color
	SystemBrightBlack

	// BrightRed for system foreground color
	SystemBrightRed

	// BrightGreen for system foreground color
	SystemBrightGreen

	// BrightYellow for system foreground color
	SystemBrightYellow

	// BrightBlue for system foreground color
	SystemBrightBlue

	// BrightMagenta for system foreground color
	SystemBrightMagenta

	// BrightCyan for system foreground color
	SystemBrightCyan

	// BrightWhite for system foreground color
	SystemBrightWhite

	// Red: HTML Hex Color Code #FF0000, Ansi 8-bit Color Index 196
	Red

	// Orange: HTML Hex Color Code #FF8700, Ansi 8-bit Color Index 208
	Orange

	// Yellow: HTML Hex Color Code #FFFF00, Ansi 8-bit Color Index 226
	Yellow

	// Chartreuse: HTML Hex Color Code #87FF00, Ansi 8-bit Color Index 118
	Chartreuse

	// Green: HTML Hex Color Code #00FF00, Ansi 8-bit Color Index 46
	Green

	// SpringGreen: HTML Hex Color Code #00FF87, Ansi 8-bit Color Index 48
	SpringGreen

	// Cyan: HTML Hex Color Code #00FFFF, Ansi 8-bit Color Index 51
	Cyan

	// Azure: HTML Hex Color Code #0087FF, Ansi 8-bit Color Index 33
	Azure

	// Blue: HTML Hex Color Code #0000FF, Ansi 8-bit Color Index 21
	Blue

	// Violet: HTML Hex Color Code #8700FF, Ansi 8-bit Color Index 93
	Violet

	// Magenta: HTML Hex Color Code #FF00FF, Ansi 8-bit Color Index 201
	Magenta

	// Rose: HTML Hex Color Code #FF0087, Ansi 8-bit Color Index 198
	Rose

	// White: HTML Hex Color Code #FFFFFF, Ansi 8-bit Color Index 231
	White

	// Silver: HTML Hex Color Code #BCBCBC, Ansi 8-bit Color Index 250
	Silver

	// Grey: HTML Hex Color Code #808080, Ansi 8-bit Color Index 244
	Grey

	// Black: HTML Hex Color Code #000000, Ansi 8-bit Color Index 16
	Black
)

Foreground color name

View Source
const (
	// Default background color
	DefaultBackground = BackgroundColor(iota)

	// Black for system background color
	SystemBlackBackground

	// Red for system background color
	SystemRedBackground

	// Green for system background color
	SystemGreenBackground

	// Yellow for system background color
	SystemYellowBackground

	// Blue for system background color
	SystemBlueBackground

	// Magenta for system background color
	SystemMagentaBackground

	// Cyan for system background color
	SystemCyanBackground

	// White for system background color
	SystemWhiteBackground

	// BrightBlack for system background color
	SystemBrightBlackBackground

	// BrightRed for system background color
	SystemBrightRedBackground

	// BrightGreen for system background color
	SystemBrightGreenBackground

	// BrightYellow for system background color
	SystemBrightYellowBackground

	// BrightBlue for system background color
	SystemBrightBlueBackground

	// BrightMagenta for system background color
	SystemBrightMagentaBackground

	// BrightCyan for system background color
	SystemBrightCyanBackground

	// BrightWhite for system background color
	SystemBrightWhiteBackground

	// Red for background color: HTML Hex Color Code #FF0000, Ansi 8-bit Color Index 196
	RedBackground

	// Orange for background color: HTML Hex Color Code #FF8700, Ansi 8-bit Color Index 208
	OrangeBackground

	// Yellow for background color: HTML Hex Color Code #FFFF00, Ansi 8-bit Color Index 226
	YellowBackground

	// Chartreuse for background color: HTML Hex Color Code #87FF00, Ansi 8-bit Color Index 118
	ChartreuseBackground

	// Green for background color: HTML Hex Color Code #00FF00, Ansi 8-bit Color Index 46
	GreenBackground

	// SpringGreen for background color: HTML Hex Color Code #00FF87, Ansi 8-bit Color Index 48
	SpringGreenBackground

	// Cyan for background color: HTML Hex Color Code #00FFFF, Ansi 8-bit Color Index 51
	CyanBackground

	// Azure for background color: HTML Hex Color Code #0087FF, Ansi 8-bit Color Index 33
	AzureBackground

	// Blue for background color: HTML Hex Color Code #0000FF, Ansi 8-bit Color Index 21
	BlueBackground

	// Violet for background color: HTML Hex Color Code #8700FF, Ansi 8-bit Color Index 93
	VioletBackground

	// Magenta for background color: HTML Hex Color Code #FF00FF, Ansi 8-bit Color Index 201
	MagentaBackground

	// Rose for background color: HTML Hex Color Code #FF0087, Ansi 8-bit Color Index 198
	RoseBackground

	// White for background color: HTML Hex Color Code #FFFFFF, Ansi 8-bit Color Index 231
	WhiteBackground

	// Silver for background color: HTML Hex Color Code #BCBCBC, Ansi 8-bit Color Index 250
	SilverBackground

	// Grey for background color: HTML Hex Color Code #808080, Ansi 8-bit Color Index 244
	GreyBackground

	// Black for background color: HTML Hex Color Code #000000, Ansi 8-bit Color Index 16
	BlackBackground
)

Background color name

View Source
const Version = "0.4.1"

Variables

This section is empty.

Functions

func Colorize added in v0.3.0

func Colorize(str string, foreground Color, background BackgroundColor) string

Colorize returns the string with foreground and background colors.

func ColorizeBackground added in v0.3.0

func ColorizeBackground(str string, background BackgroundColor) string

ColorizeBackground returns the string with the background color.

func ColorizeForeground added in v0.3.0

func ColorizeForeground(str string, foreground Color) string

ColorizeForeground returns the string with the foreground color.

func DisableTerminalDetection

func DisableTerminalDetection()

DisableTerminalDetection disables terminal detection. Even if the terminal is not detected, colorization is not suppressed. By default, the terminal detection is enabled.

func EnableTerminalDetection

func EnableTerminalDetection()

EnableTerminalDetection enables terminal detection. If no terminal is detected, colorization is suppressed. By default, the terminal detection is enabled.

Types

type BackgroundColor added in v0.2.0

type BackgroundColor int

BackgroundColor is a background color for output.

func NewBackgroundColor added in v0.2.0

func NewBackgroundColor(name string) (BackgroundColor, error)

NewBackgroundColor creates a BackgroundColor.

func (BackgroundColor) Colorize added in v0.2.0

func (c BackgroundColor) Colorize(str string) string

Colorize returns the string with the background color.

func (BackgroundColor) String added in v0.2.0

func (c BackgroundColor) String() string

String returns the name of the color.

type Color

type Color int

Color is a color that handles ANSI escape sequences.

func NewColor

func NewColor(name string) (Color, error)

NewColor creates a Color.

func SupportedColors added in v0.2.0

func SupportedColors() []Color

func (Color) Colorize

func (c Color) Colorize(str string) string

Colorize returns the string with the foreground color.

func (Color) String

func (c Color) String() string

String returns the name of the color.

Directories

Path Synopsis
_example
cmd

Jump to

Keyboard shortcuts

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