bunt

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2019 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package bunt provides terminal true color support.

Terminals that support 24 bit (true color) colors with ANSI sequences, the `bunt` package provides functions to wrap text into the respective sequence.

In case the terminal does not support true color, a fallback option is used to calcucate amd pick the best matching color out of the 16 color basic color schema instead.

If the input string already contains ANSI sequences, the wrapping function will break up the string first and apply the target color to each respective segment of the input string to preserve any existing text emphasis like `bold`, or `italic` while additionally applying the desired color.

Index

Constants

View Source
const (
	OFF  = SwitchState(-1)
	AUTO = SwitchState(0)
	ON   = SwitchState(+1)
)

Supported setting states

View Source
const (
	Bold      = 1
	Italic    = 3
	Underline = 4
)

Text modifiers (emphasis)

Variables

View Source
var (
	Pink            = hexColor("#FFC0CB")
	LightPink       = hexColor("#FFB6C1")
	HotPink         = hexColor("#FF69B4")
	DeepPink        = hexColor("#FF1493")
	PaleVioletRed   = hexColor("#DB7093")
	MediumVioletRed = hexColor("#C71585")
)

Pink colors

View Source
var (
	LightSalmon = hexColor("#FFA07A")
	Salmon      = hexColor("#FA8072")
	DarkSalmon  = hexColor("#E9967A")
	LightCoral  = hexColor("#F08080")
	IndianRed   = hexColor("#CD5C5C")
	Crimson     = hexColor("#DC143C")
	FireBrick   = hexColor("#B22222")
	DarkRed     = hexColor("#8B0000")
	Red         = hexColor("#FF0000")
)

Red colors

View Source
var (
	OrangeRed  = hexColor("#FF4500")
	Tomato     = hexColor("#FF6347")
	Coral      = hexColor("#FF7F50")
	DarkOrange = hexColor("#FF8C00")
	Orange     = hexColor("#FFA500")
)

Orange colors

View Source
var (
	Yellow               = hexColor("#FFFF00")
	LightYellow          = hexColor("#FFFFE0")
	LemonChiffon         = hexColor("#FFFACD")
	LightGoldenrodYellow = hexColor("#FAFAD2")
	PapayaWhip           = hexColor("#FFEFD5")
	Moccasin             = hexColor("#FFE4B5")
	PeachPuff            = hexColor("#FFDAB9")
	PaleGoldenrod        = hexColor("#EEE8AA")
	Khaki                = hexColor("#F0E68C")
	DarkKhaki            = hexColor("#BDB76B")
	Gold                 = hexColor("#FFD700")
)

Yellow colors

View Source
var (
	Cornsilk       = hexColor("#FFF8DC")
	BlanchedAlmond = hexColor("#FFEBCD")
	Bisque         = hexColor("#FFE4C4")
	NavajoWhite    = hexColor("#FFDEAD")
	Wheat          = hexColor("#F5DEB3")
	BurlyWood      = hexColor("#DEB887")
	Tan            = hexColor("#D2B48C")
	RosyBrown      = hexColor("#BC8F8F")
	SandyBrown     = hexColor("#F4A460")
	Goldenrod      = hexColor("#DAA520")
	DarkGoldenrod  = hexColor("#B8860B")
	Peru           = hexColor("#CD853F")
	Chocolate      = hexColor("#D2691E")
	SaddleBrown    = hexColor("#8B4513")
	Sienna         = hexColor("#A0522D")
	Brown          = hexColor("#A52A2A")
	Maroon         = hexColor("#800000")
)

Brown colors

View Source
var (
	DarkOliveGreen    = hexColor("#556B2F")
	Olive             = hexColor("#808000")
	OliveDrab         = hexColor("#6B8E23")
	YellowGreen       = hexColor("#9ACD32")
	LimeGreen         = hexColor("#32CD32")
	Lime              = hexColor("#00FF00")
	LawnGreen         = hexColor("#7CFC00")
	Chartreuse        = hexColor("#7FFF00")
	GreenYellow       = hexColor("#ADFF2F")
	SpringGreen       = hexColor("#00FF7F")
	MediumSpringGreen = hexColor("#00FA9A")
	LightGreen        = hexColor("#90EE90")
	PaleGreen         = hexColor("#98FB98")
	DarkSeaGreen      = hexColor("#8FBC8F")
	MediumAquamarine  = hexColor("#66CDAA")
	MediumSeaGreen    = hexColor("#3CB371")
	SeaGreen          = hexColor("#2E8B57")
	ForestGreen       = hexColor("#228B22")
	Green             = hexColor("#008000")
	DarkGreen         = hexColor("#006400")
)

Green colors

View Source
var (
	Aqua            = hexColor("#00FFFF")
	Cyan            = hexColor("#00FFFF")
	LightCyan       = hexColor("#E0FFFF")
	PaleTurquoise   = hexColor("#AFEEEE")
	Aquamarine      = hexColor("#7FFFD4")
	Turquoise       = hexColor("#40E0D0")
	MediumTurquoise = hexColor("#48D1CC")
	DarkTurquoise   = hexColor("#00CED1")
	LightSeaGreen   = hexColor("#20B2AA")
	CadetBlue       = hexColor("#5F9EA0")
	DarkCyan        = hexColor("#008B8B")
	Teal            = hexColor("#008080")
)

Cyan colors

View Source
var (
	LightSteelBlue = hexColor("#B0C4DE")
	PowderBlue     = hexColor("#B0E0E6")
	LightBlue      = hexColor("#ADD8E6")
	SkyBlue        = hexColor("#87CEEB")
	LightSkyBlue   = hexColor("#87CEFA")
	DeepSkyBlue    = hexColor("#00BFFF")
	DodgerBlue     = hexColor("#1E90FF")
	CornflowerBlue = hexColor("#6495ED")
	SteelBlue      = hexColor("#4682B4")
	RoyalBlue      = hexColor("#4169E1")
	Blue           = hexColor("#0000FF")
	MediumBlue     = hexColor("#0000CD")
	DarkBlue       = hexColor("#00008B")
	Navy           = hexColor("#000080")
	MidnightBlue   = hexColor("#191970")
)

Blue colors

View Source
var (
	Lavender        = hexColor("#E6E6FA")
	Thistle         = hexColor("#D8BFD8")
	Plum            = hexColor("#DDA0DD")
	Violet          = hexColor("#EE82EE")
	Orchid          = hexColor("#DA70D6")
	Fuchsia         = hexColor("#FF00FF")
	Magenta         = hexColor("#FF00FF")
	MediumOrchid    = hexColor("#BA55D3")
	MediumPurple    = hexColor("#9370DB")
	BlueViolet      = hexColor("#8A2BE2")
	DarkViolet      = hexColor("#9400D3")
	DarkOrchid      = hexColor("#9932CC")
	DarkMagenta     = hexColor("#8B008B")
	Purple          = hexColor("#800080")
	Indigo          = hexColor("#4B0082")
	DarkSlateBlue   = hexColor("#483D8B")
	SlateBlue       = hexColor("#6A5ACD")
	MediumSlateBlue = hexColor("#7B68EE")
)

Purple, violet, and magenta colors

View Source
var (
	White         = hexColor("#FFFFFF")
	Snow          = hexColor("#FFFAFA")
	Honeydew      = hexColor("#F0FFF0")
	MintCream     = hexColor("#F5FFFA")
	Azure         = hexColor("#F0FFFF")
	AliceBlue     = hexColor("#F0F8FF")
	GhostWhite    = hexColor("#F8F8FF")
	WhiteSmoke    = hexColor("#F5F5F5")
	Seashell      = hexColor("#FFF5EE")
	Beige         = hexColor("#F5F5DC")
	OldLace       = hexColor("#FDF5E6")
	FloralWhite   = hexColor("#FFFAF0")
	Ivory         = hexColor("#FFFFF0")
	AntiqueWhite  = hexColor("#FAEBD7")
	Linen         = hexColor("#FAF0E6")
	LavenderBlush = hexColor("#FFF0F5")
	MistyRose     = hexColor("#FFE4E1")
)

White colors

View Source
var (
	Gainsboro      = hexColor("#DCDCDC")
	LightGray      = hexColor("#D3D3D3")
	Silver         = hexColor("#C0C0C0")
	DarkGray       = hexColor("#A9A9A9")
	Gray           = hexColor("#808080")
	DimGray        = hexColor("#696969")
	LightSlateGray = hexColor("#778899")
	SlateGray      = hexColor("#708090")
	DarkSlateGray  = hexColor("#2F4F4F")
	Black          = hexColor("#000000")
)

Gray and black colors

View Source
var (
	AdditionGreen      = hexColor("#58BF38")
	RemovalRed         = hexColor("#B9311B")
	ModificationYellow = hexColor("#C7C43F")
)

Special additional colors

View Source
var ColorSetting = AUTO

ColorSetting defines the coloring setting to be used

View Source
var TrueColorSetting = AUTO

TrueColorSetting defines the true color usage setting to be used

Functions

func BoldText

func BoldText(text string) string

BoldText is a convenience function to make the string bold

func Colorize

func Colorize(text string, color colorful.Color, modifiers ...Attribute) string

Colorize applies an ANSI truecolor sequence for the provided color to the given text.

func Colorize4bit

func Colorize4bit(text string, color colorful.Color, modifiers ...Attribute) string

Colorize4bit applies the best suitable 4-bit color attribute to the given text.

func ColorizeFgBg

func ColorizeFgBg(text string, foreground colorful.Color, background colorful.Color, modifiers ...Attribute) string

ColorizeFgBg applies an ANSI truecolor sequence for the provided foreground and background colors to the given text.

func Fprint

func Fprint(w io.Writer, a ...interface{}) (n int, err error)

Fprint wraps fmt.Fprint(w io.Writer, a ...interface{}) and evaluates any text markers into its respective format

func Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf wraps fmt.Fprintf(w io.Writer, format string, a ...interface{}) and evaluates any text markers into its respective format

func Fprintln

func Fprintln(w io.Writer, a ...interface{}) (n int, err error)

Fprintln wraps fmt.Fprintln(w io.Writer, a ...interface{}) and evaluates any text markers into its respective format

func PlainTextLength

func PlainTextLength(text string) int

PlainTextLength returns the length of the input text without any ANSI escape sequences.

func Print

func Print(a ...interface{}) (n int, err error)

Print wraps fmt.Print(a ...interface{}) and evaluates any text markers into its respective format

func Printf

func Printf(format string, a ...interface{}) (n int, err error)

Printf wraps fmt.Printf(format string, a ...interface{}) and evaluates any text markers into its respective format

func Println

func Println(a ...interface{}) (n int, err error)

Println wraps fmt.Println(a ...interface{}) and evaluates any text markers into its respective format

func RemoveAllEscapeSequences

func RemoveAllEscapeSequences(input string) string

RemoveAllEscapeSequences return the input string with all escape sequences removed

func Sprint

func Sprint(a ...interface{}) string

Sprint wraps fmt.Sprint(a ...interface{}) and evaluates any text markers into its respective format

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf wraps fmt.Sprintf(format string, a ...interface{}) and evaluates any text markers into its respective format

func Sprintln

func Sprintln(a ...interface{}) string

Sprintln wraps fmt.Sprintln(a ...interface{}) and evaluates any text markers into its respective format

func Style

func Style(text string, modifiers ...Attribute) string

Style applies only text modifications like Bold, Italic, or Underline to the text

func Substring added in v1.7.4

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

Substring returns a substring of a text that contains ANSI escape sequences

func UseColors

func UseColors() bool

UseColors return whether colors are used or not based on the configured color setting

func UseTrueColor

func UseTrueColor() bool

UseTrueColor returns whether true color colors should be used or not base on the configured true color usage setting

Types

type Attribute

type Attribute uint8

Attribute is used to specify the attributes (codes) used in ANSI sequences

func Get4bitEquivalentColorAttribute

func Get4bitEquivalentColorAttribute(targetColor colorful.Color) Attribute

Get4bitEquivalentColorAttribute returns the color attribute which matches the best with the provided color.

type Segment

type Segment struct {
	Attributes []Attribute
	Data       string
}

Segment defines a piece of text and its designed style attributes (which can be empty)

type String

type String []Segment

String defines a string that consists of differently colored text segments

func BreakUpStringIntoColorSegments

func BreakUpStringIntoColorSegments(input string) (String, error)

BreakUpStringIntoColorSegments takes the input string and breaks it up into individual substrings that each have their own coloring already applied

type SwitchState

type SwitchState int

SwitchState is the type to cover different preferences/settings like: on, off, or auto

func ParseSetting

func ParseSetting(setting string) (SwitchState, error)

ParseSetting converts a string with a setting into its respective state

Jump to

Keyboard shortcuts

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