color

package
v0.8.31 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: Apache-2.0 Imports: 15 Imported by: 23

Documentation

Overview

Package color provides a wrapped standard output device like printf but with colored enhancements.

The main types are Cursor and Translator.

Cursor allows formatting colorful text and moving cursor to another coordinate.

New will return a Cursor object.

RowsBlock is another cursor controller, which can treat the current line and following lines as a block and updating these lines repeatedly. This feature will help the progressbar writers or the continuous lines updater.

Translator is a text and tiny HTML tags translator to convert these markup text into colorful console text sequences. GetCPT can return a smart translator which translate colorful text or strip the ansi escaped sequence from result text if `states.Env().IsNoColorMode()` is true.

Color is an interface type to represent a terminal color object, which can be serialized to ansi escaped sequence directly by Color.Color.

To create a Color object, there are several ways:

See also docsite.

Index

Examples

Constants

View Source
const (

	// FgBlack terminal color code
	FgBlack = Color16(30)
	// FgRed terminal color code
	FgRed = Color16(31)
	// FgGreen terminal color code
	FgGreen = Color16(32)
	// FgYellow terminal color code
	FgYellow = Color16(33)
	// FgBlue terminal color code
	FgBlue = Color16(34)
	// FgMagenta terminal color code
	FgMagenta = Color16(35)
	// FgCyan terminal color code
	FgCyan = Color16(36)
	// FgLightGray terminal color code (White)
	FgLightGray = Color16(37)

	// FgDarkGray terminal color code (Gray, Light Black).
	//
	// A highlight/bright black color, maybe 50% gray. See FgLightBlack.
	FgDarkGray = Color16(90)
	// FgLightBlack terminal color code (Gray, Light Black).
	//
	// A highlight/bright black color, maybe 50% gray. See FgDarkGray.
	FgLightBlack = Color16(90)
	// FgLightRed terminal color code
	FgLightRed = Color16(91)
	// FgLightGreen terminal color code
	FgLightGreen = Color16(92)
	// FgLightYellow terminal color code
	FgLightYellow = Color16(93)
	// FgLightBlue terminal color code
	FgLightBlue = Color16(94)
	// FgLightMagenta terminal color code
	FgLightMagenta = Color16(95)
	// FgLightCyan terminal color code
	FgLightCyan = Color16(96)
	// FgWhite terminal color code (Light White)
	FgWhite = Color16(97)

	// BgBlack terminal color code
	BgBlack = Color16(40)
	// BgRed terminal color code
	BgRed = Color16(41)
	// BgGreen terminal color code
	BgGreen = Color16(42)
	// BgYellow terminal color code
	BgYellow = Color16(43)
	// BgBlue terminal color code
	BgBlue = Color16(44)
	// BgMagenta terminal color code
	BgMagenta = Color16(45)
	// BgCyan terminal color code
	BgCyan = Color16(46)
	// BgLightGray terminal color code
	BgLightGray = Color16(47)

	// BgDarkGray terminal color code
	BgDarkGray = Color16(100)
	// BgLightRed terminal color code
	BgLightRed = Color16(101)
	// BgLightGreen terminal color code
	BgLightGreen = Color16(102)
	// BgLightYellow terminal color code
	BgLightYellow = Color16(103)
	// BgLightBlue terminal color code
	BgLightBlue = Color16(104)
	// BgLightMagenta terminal color code
	BgLightMagenta = Color16(105)
	// BgLightCyan terminal color code
	BgLightCyan = Color16(106)
	// BgWhite terminal color code
	BgWhite = Color16(107)

	// BgNormal terminal color code.
	//
	// All attributes become turned off.
	BgNormal = Color16(0)
	// BgBoldOrBright terminal color code
	//
	// Bold or increased intensity
	BgBoldOrBright = Color16(1)
	// BgDim terminal color code.
	//
	// Faint, decreased intensity, or dim.
	// May be implemented as a light font weight like bold.
	BgDim = Color16(2)
	// BgItalic terminal color code.
	//
	// Not widely supported. Sometimes treated as inverse or blink
	BgItalic = Color16(3)
	// BgUnderline terminal color code.
	//
	// Style extensions exist for Kitty, VTE, mintty, iTerm2 and Konsole.
	BgUnderline = Color16(4)
	// BgBlink terminal color code.
	//
	// Slow blink, Sets blinking to less than 150 times per minute.
	// But in many tty it's no effect.
	//
	// Sometimes it can be used for switching to 'normal' bg state without
	// reset all fg and bg settings (if using bg code 0)
	BgBlink = Color16(5)
	// BgRapidBlink terminal color code.
	//
	// MS-DOS ANSI.SYS, 150+ per minute; not widely supported.
	//
	// Sometimes it can be used for switching to 'normal' bg state without
	// reset all fg and bg settings (if using bg code 0)
	BgRapidBlink = Color16(6)
	// BgInverse terminal color code.
	//
	// Swap foreground and background colors; inconsistent emulation
	BgInverse = Color16(7)
	// BgHidden terminal color code.
	//
	// not widely supported.
	BgHidden = Color16(8)
	// BgStrikeout terminal color code.
	//
	// marked as if for deletion.
	BgStrikeout = Color16(9)

	BgResetBoldOrDoubleUnderLine = Color16(21)
	BgResetNormalColorAndBright  = Color16(22) // = BgResetDim
	BgResetItalic                = Color16(23)
	BgResetUnderline             = Color16(24)
	BgResetBlink                 = Color16(25)
	BgResetInverse               = Color16(27)
	BgResetHidden                = Color16(28)
	BgResetStrikeout             = Color16(29)

	FgDarkColor = FgLightGray

	FgDefault = Color16(39)
	BgDefault = Color16(49)

	ResetToNormalColor = Color16(0)

	// NoColor is not a declared ansi code but we can use it for identifying
	// a variable isn't initializing yet.
	NoColor = Color16(-1)
)
View Source
const ESCAPE = '\x1b'

Variables

This section is empty.

Functions

func Color256table added in v0.6.68

func Color256table(out io.Writer)

Color256table prints a 8-bit color table for testing

func Colored

func Colored(clr Color, format string, args ...any)

Colored outputs formatted message to stdout while logger level less than level.WarnLevel. For level.SetLevel(level.ErrorLevel), the text will be discarded.

func Coloredf

func Coloredf(clr Color, format string, args ...any)

Coloredf outputs formatted message to stdout while logger level less than level.WarnLevel and cmdr is in VERBOSE mode.

func Dim

func Dim(format string, args ...any)

Dim outputs formatted message to stdout while logger level less than level.WarnLevel.

For example, after level.SetLevel(level.ErrorLevel), the text via Dim will be discarded.

func Dimf

func Dimf(format string, args ...any)

Dimf outputs formatted message to stdout while logger level less than level.WarnLevel and cmdr is in verbose mode.

For example, after level.SetLevel(level.ErrorLevel), the text via Dimf will be discarded.

While env-var VERBOSE=1, the text via Dimf will be shown.

func Down added in v0.6.68

func Down(n int)

func Hide added in v0.6.68

func Hide()

func Highlight

func Highlight(format string, args ...any)

Highlight outputs formatted message to stdout while logger level less than level.WarnLevel. For level.SetLevel(level.ErrorLevel), the text will be discarded.

func Left added in v0.6.68

func Left(n int)

Left moves cursor left by n

func NewFmtBuf added in v0.6.68

func NewFmtBuf() (s *fmtbufS)

func ResetColor

func ResetColor(c Color)

func RestorePos added in v0.6.68

func RestorePos()
func Right(n int)

func SavePos added in v0.6.68

func SavePos()

func ScrollDown added in v0.6.68

func ScrollDown(n int)

func ScrollUp added in v0.6.68

func ScrollUp(n int)

func Show added in v0.6.68

func Show()

func StripHTMLTags

func StripHTMLTags(s string) string

StripHTMLTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.

func StripLeftTabs

func StripLeftTabs(s string) string

StripLeftTabs strips the least left side tab chars from lines. StripLeftTabs strips html tags too.

func StripLeftTabsC

func StripLeftTabsC(s string) string

StripLeftTabsC strips the least left side tab chars from lines. StripLeftTabsC strips html tags too. At the end, StripLeftTabsC try translate color code in string.

func StripLeftTabsOnly

func StripLeftTabsOnly(s string) string

StripLeftTabsOnly strips the least left side tab chars from lines.

func Text

func Text(format string, args ...any)

Text prints formatted message without any predefined ansi escaping.

func ToColor

func ToColor(clr Color, format string, args ...any) (str string)

func ToColorString added in v0.5.5

func ToColorString(clr Color) string

func ToDim

func ToDim(format string, args ...any) (str string)

func ToHighlight

func ToHighlight(format string, args ...any) (str string)

func Up added in v0.6.68

func Up(n int)

Up moves cursor up by n

func WrapColorAndBgTo

func WrapColorAndBgTo(out io.Writer, clr, bg Color, text string)

func WrapColorTo

func WrapColorTo(out io.Writer, clr Color, text string)

func WrapDimTo

func WrapDimTo(out io.Writer, text string)

func WrapDimToLite

func WrapDimToLite(out io.Writer, text string)

func WrapHighlightTo

func WrapHighlightTo(out io.Writer, text string)

func Write added in v0.6.68

func Write(b []byte) (n int, e error)

Types

type CSICode added in v0.6.68

type CSICode struct {
	N      int
	Suffix CSIsuffix
}

CSICode will be expanded to `CSI n byte` form. For example, Cursor Up ('A') expands as `CSI n A`.

func (CSICode) And added in v0.6.68

func (c CSICode) And(cs CSIsuffix, n ...int) (codes CSICodes)

func (CSICode) Color added in v0.6.68

func (c CSICode) Color() string

func (CSICode) ColorTo added in v0.6.68

func (c CSICode) ColorTo(out io.Writer)

func (CSICode) Int added in v0.6.68

func (c CSICode) Int() (color int)

func (CSICode) String added in v0.6.68

func (c CSICode) String() string

type CSICode2 added in v0.6.68

type CSICode2 struct {
	M int
	CSICode
}

type CSICodes added in v0.6.68

type CSICodes struct {
	Items []csiCode
}

CSICodes wraps several csiCode together.

The best way for creating a CSI sequence is using CSIsuffix.Code, CSIsuffix.Code1 and CSIsuffix.Code2.

Or you can use thees function: CSIAddCode, CSIAddCode1 and CSIAddCode2.

For example,

c := CSICursorUp.Code1(7)
fmt.Printf("%s", c) // move cursor up 7 lines
c := CSICursorPosition.Code2(2, 3) // move cursor to row 2 col 3
fmt.Printf("%s", c)

c := color.CSICursorUp.Code1(7)
fmt.Printf("%s", c) // move cursor up 7 lines
d := color.CSICursorDown.Code1(7)
fmt.Printf("%s", d) // move cursor down 7 lines

fmt.Printf("%s", color.New().SavePos().Build())

var cx color.CSICodes
cx.AddCode2(color.CSICursorPosition, 2, 3) // move cursor to row 2 col 3
fmt.Printf("%s", cx)

fmt.Printf("%s", color.New().RestorePos().Build())
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	c := color.CSICursorUp.Code1(7)
	fmt.Printf("%s", c) // move cursor up 7 lines
	d := color.CSICursorDown.Code1(7)
	fmt.Printf("%s", d) // move cursor down 7 lines

	// move cursor to new position

	fmt.Printf("%s", color.New().SavePos().Build())

	var cx color.CSICodes
	cx.AddCode2(color.CSICursorPosition, 2, 3) // move cursor to row 2 col 3
	fmt.Printf("%s", cx)

	fmt.Printf("%s", color.New().RestorePos().Build())

}
Output:
�[7A�[7B�[s�[2,3H�[u

func CSIAddCode added in v0.6.68

func CSIAddCode(code CSIsuffix) (c CSICodes)

func CSIAddCode1 added in v0.6.68

func CSIAddCode1(code CSIsuffix, n int) (c CSICodes)

func CSIAddCode2 added in v0.6.68

func CSIAddCode2(code CSIsuffix, n, m int) (c CSICodes)

func (*CSICodes) AddCode added in v0.6.68

func (c *CSICodes) AddCode(code CSIsuffix)

func (*CSICodes) AddCode1 added in v0.6.68

func (c *CSICodes) AddCode1(code CSIsuffix, n int)

func (*CSICodes) AddCode2 added in v0.6.68

func (c *CSICodes) AddCode2(code CSIsuffix, n, m int)

func (CSICodes) Color added in v0.6.68

func (c CSICodes) Color() string

func (CSICodes) ColorTo added in v0.6.68

func (c CSICodes) ColorTo(out io.Writer)

func (CSICodes) Int added in v0.6.68

func (c CSICodes) Int() (color int)

func (CSICodes) String added in v0.6.68

func (c CSICodes) String() string

type CSIsgr added in v0.6.68

type CSIsgr byte
const (
	SGRreset                        CSIsgr = iota // reset or normal
	SGRbold                                       // bold or increased intensity
	SGRdim                                        // faint, decreased intensity, or dim
	SGRitalic                                     // italic
	SGRunderline                                  // underline
	SGRslowblink                                  // blink
	SGRrapidblink                                 // fast blink
	SGRinverse                                    // reverse video or invert
	SGRhide                                       // conceal or hide
	SGRstrike                                     // crossed-out or strike
	SGRprimaryfont                                //
	SGRalternativefont1                           //
	SGRalternativefont2                           //
	SGRalternativefont3                           //
	SGRalternativefont4                           //
	SGRalternativefont5                           //
	SGRalternativefont6                           //
	SGRalternativefont7                           //
	SGRalternativefont8                           //
	SGRalternativefont9                           //
	SGRgothic                                     // Fraktur (Gothic), rarely supported
	SGRdoublyUnderline                            // doubly underlined, or not bold
	SGRresetBoldAndDim                            // neither bold nor faint
	SGRresetItalic                                // reset italic
	SGRresetUnderline                             // reset singly or doubly underlined
	SGRresetSlowBlink                             // turn blink off
	SGRresetRapidBlink                            // proportional spacing
	SGRresetInverse                               // reset inverse
	SGRresetHide                                  // not concealed
	SGRresetStrike                                // not cross-out
	SGRfgBlack                                    //
	SGRfgRed                                      //
	SGRfgGreen                                    //
	SGRfgYellow                                   //
	SGRfgBlue                                     //
	SGRfgMagenta                                  //
	SGRfgCyan                                     //
	SGRfgLightGray                                //
	SGRsetFg                                      // use [NewColor256] or [NewColor16m]. 8-bit color; next arguments are `5;n` or `2;r;g;b`
	SGRdefaultFg                                  // reset fg set by [SGRsetFg]
	SGRbgBlack                                    //
	SGRbgRed                                      //
	SGRbgGreen                                    //
	SGRbgYellow                                   //
	SGRbgBlue                                     //
	SGRbgMagenta                                  //
	SGRbgCyan                                     //
	SGRbgLightGray                                //
	SGRsetBg                                      // use [NewColor256] or [NewColor16m]. 8-bit color; next arguments are `5;n` or `2;r;g;b`.
	SGRdefaultBg                                  // reset bg set by [SGRsetBg]
	SGRdisableProportionalSpacing                 //
	SGRframed                                     //
	SGRencircled                                  //
	SGRoverlined                                  // not supported in Terminal.app
	SGRneitherFramedNorEncircled                  //
	SGRnotoverlined                               //
	SGRreserved56                                 //
	SGRreserved57                                 //
	SGRsetUnderlineColor                          // not om standard; implemented in Kitty, VTE, mintty, and iTerm2. Next arguments are `5;n` or `2;r;g;b`.
	SGRdefaultUnderlineColor                      //
	SGRideogramUnderline                          // Ideogram underline or right side line
	SGRideogramDoubleUnderline                    //
	SGRideogramOverline                           //
	SGRideogramDoubleOverline                     //
	SGRideogramStressMarking                      //
	SGRresetIdeogram                              //
	SGRreserved66                                 //
	SGRreserved67                                 //
	SGRreserved68                                 //
	SGRreserved69                                 //
	SGRreserved70                                 //
	SGRreserved71                                 //
	SGRreserved72                                 //
	SGRsuperscript                                //
	SGRsubscript                                  //
	SGRresetSuperscriptAndSubscript               //
	SGRreserved76                                 //
	SGRreserved77                                 //
	SGRreserved78                                 //
	SGRreserved79                                 //
	SGRreserved80                                 //
	SGRreserved81                                 //
	SGRreserved82                                 //
	SGRreserved83                                 //
	SGRreserved84                                 //
	SGRreserved85                                 //
	SGRreserved86                                 //
	SGRreserved87                                 //
	SGRreserved88                                 //
	SGRreserved89                                 //
	SGRfgDarkGray                                 // high density colors, fg, light black
	SGRfgLightRed                                 //
	SGRfgLightGreen                               //
	SGRfgLightYellow                              //
	SGRfgLightBlue                                //
	SGRfgLightMagenta                             //
	SGRfgLightCyan                                //
	SGRfgWhite                                    // Light LightGray
	SGRreserved98                                 //
	SGRreserved99                                 //
	SGRbgDarkGray                                 // high density colors, bg, light black
	SGRbgLightRed                                 //
	SGRbgLightGreen                               //
	SGRbgLightYellow                              //
	SGRbgLightBlue                                //
	SGRbgLightMagenta                             //
	SGRbgLightCyan                                //
	SGRbgWhite                                    // Light LightGray
)

func NewSGR added in v0.6.68

func NewSGR(code CSIsgr) CSIsgr

NewSGR return the given CSIsgr code directly.

A CSIsgr code is a `ESC n m` sequence to represent bold, dim, fg color, bg colors, ...

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	for i, sgrs := range []struct {
		pre, post color.CSIsgr
		desc      string
	}{
		{color.SGRbold, color.SGRresetBoldAndDim, "bold"},
		{color.SGRdim, color.SGRresetBoldAndDim, "dim"},
		{color.SGRitalic, color.SGRresetItalic, "italic"},
		{color.SGRunderline, color.SGRresetUnderline, "underline"},
		{color.SGRslowblink, color.SGRresetSlowBlink, "blink"},
		{color.SGRrapidblink, color.SGRresetRapidBlink, "fast blink"},
		{color.SGRinverse, color.SGRresetInverse, "inverse"},
		{color.SGRhide, color.SGRresetHide, "hide"},
		{color.SGRstrike, color.SGRresetStrike, "strike"},
		{color.SGRframed, color.SGRneitherFramedNorEncircled, "framed"},
		{color.SGRencircled, color.SGRneitherFramedNorEncircled, "encircled"},
		{color.SGRoverlined, color.SGRnotoverlined, "overlined"},
		{color.SGRideogramUnderline, color.SGRresetIdeogram, "ideogram underline"},
		{color.SGRideogramDoubleUnderline, color.SGRresetIdeogram, "ideogram double underline"},
		{color.SGRideogramOverline, color.SGRresetIdeogram, "ideogram overline"},
		{color.SGRideogramDoubleOverline, color.SGRresetIdeogram, "ideogram double overline"},
		{color.SGRideogramStressMarking, color.SGRresetIdeogram, "ideogram stress marking"},
		{color.SGRsuperscript, color.SGRresetSuperscriptAndSubscript, "superscript"},
		{color.SGRsubscript, color.SGRresetSuperscriptAndSubscript, "subscript"},
		// {color.SGRdim, color.SGRresetDim},
	} {
		str := fmt.Sprintf(`%5d. %s%s%s %s`,
			i, sgrs.pre,
			"Hello, World!",
			sgrs.post,
			sgrs.desc,
		)
		fmt.Println(str)
	}

	fmt.Println(color.SGRreset)

}
Output:
    0. �[1mHello, World!�[22m bold
    1. �[2mHello, World!�[22m dim
    2. �[3mHello, World!�[23m italic
    3. �[4mHello, World!�[24m underline
    4. �[5mHello, World!�[25m blink
    5. �[6mHello, World!�[26m fast blink
    6. �[7mHello, World!�[27m inverse
    7. �[8mHello, World!�[28m hide
    8. �[9mHello, World!�[29m strike
    9. �[51mHello, World!�[54m framed
   10. �[52mHello, World!�[54m encircled
   11. �[53mHello, World!�[55m overlined
   12. �[60mHello, World!�[65m ideogram underline
   13. �[61mHello, World!�[65m ideogram double underline
   14. �[62mHello, World!�[65m ideogram overline
   15. �[63mHello, World!�[65m ideogram double overline
   16. �[64mHello, World!�[65m ideogram stress marking
   17. �[73mHello, World!�[75m superscript
   18. �[74mHello, World!�[75m subscript
�[0m

func (CSIsgr) Color added in v0.6.68

func (c CSIsgr) Color() string

func (CSIsgr) ColorTo added in v0.6.68

func (c CSIsgr) ColorTo(out io.Writer)

func (CSIsgr) Int added in v0.6.68

func (c CSIsgr) Int() (color int)

func (CSIsgr) String added in v0.6.68

func (c CSIsgr) String() string

type CSIsuffix added in v0.6.68

type CSIsuffix byte
const (
	CSICursorUp       CSIsuffix = 'A' // ESC n A   - CUU - Cursor Up. move the cursor n (default 1) cells in the given direction
	CSICursorDown     CSIsuffix = 'B' // ESC n B   - CUD - Cursor Down. move the cursor n (default 1) cells in the given direction
	CSICursorForward  CSIsuffix = 'C' // ESC n C   - CUF - Cursor Forward. move the cursor n (default 1) cells in the given direction
	CSICursorBack     CSIsuffix = 'D' // ESC n D   - CUB - Cursor Back. move the cursor n (default 1) cells in the given direction
	CSICursorNextLine CSIsuffix = 'E' // ESC n E   - CNL - Cursor Next Line. move to beginning of the line n lines down
	CSICursorPrevLine CSIsuffix = 'F' // ESC n F   - CPL - Cursor Previous Line. move to beginning of the line n lines up
	CSICursorHorzAbs  CSIsuffix = 'G' // ESC n G   - CHA - Cursor Horizontal Absolute. move to column n
	CSICursorPosition CSIsuffix = 'H' // ESC n;m H - CUP - Cursor Position. move the cursor to row n, column m (1-based)
	CSIEraseInDisplay CSIsuffix = 'J' // ESC n J   - ED  - Erase in Screen. clears part of the screen. if n is 0, clear from cursor to end of scrren. 1 to beginning, 2 for entire screen and move to up-left corner, 3 for all screen and clear them in the scrollback buffer.
	CSIEraseInLine    CSIsuffix = 'K' // ESC n K   - EL  - Erase in Line. clears part of the line. if n is 0, clear from cursor to the end of the line. 1 to begining, 2 for entire line. cursor position does not change.
	CSIScrollUp       CSIsuffix = 'S' // ESC n S   - SU  - Scroll UP. scroll whole page up by n lines.
	CSIScrollDown     CSIsuffix = 'T' // ESC n T   - SD  - Scroll DOWN. scroll whole page down by n lines.

	CSIHorzVertPosition   CSIsuffix = 'f' // ESC n;m f - HVP - Horizontal Vertical Position. Same as CUP, but counts as a format effector function (lick CR or LF) rather than an editor function (like CUD or CNL).
	CSIAuxPortOn          CSIsuffix = '5' // ESC 5i    -     - Enable aux serial port usually for local serial printer
	CSIAuxPortOff         CSIsuffix = '4' // ESC 4i    -     - Disable aux serial port
	CSIDeviceStatusReport CSIsuffix = '6' // ESC 6n    -     - Reports the cursor postion (CPR) by transmitting `ESC [n;mR`

	// use SGRxxx instead CSISGR
	CSISGR CSIsuffix = 'H' // ESC n m   - SGR - Select Grapthic Rendition. Sets colors and style of the characters following this code.
)

func (CSIsuffix) Code added in v0.6.68

func (c CSIsuffix) Code() CSICode

func (CSIsuffix) Code1 added in v0.6.68

func (c CSIsuffix) Code1(n int) CSICode

func (CSIsuffix) Code2 added in v0.6.68

func (c CSIsuffix) Code2(n, m int) CSICode2

func (CSIsuffix) Color added in v0.6.68

func (c CSIsuffix) Color() string

func (CSIsuffix) ColorTo added in v0.6.68

func (c CSIsuffix) ColorTo(out io.Writer)

func (CSIsuffix) Int added in v0.6.68

func (c CSIsuffix) Int() (color int)

func (CSIsuffix) String added in v0.6.68

func (c CSIsuffix) String() string

type CWriter added in v0.6.68

type CWriter interface {
	io.Writer
	WriteString(str string) (n int, err error)
	WriteInt(int) (n int, err error)
	WriteRune(rune) (n int, err error)
	WriteByte(byte) (err error)
}

type Color

type Color interface {
	Int() int
	Color() string
	ColorTo(out io.Writer)
}

Color interface represents an ansi escaped sequences code in terminal/tty/console.

The supported object includes 4-bit (16-colors), 8-bit (256 colors) and 24-bit (true-colors) encoders. See also NewColor16, NewColor256 and NewColor16m.

These objects are Color (s):

  • Style (multiple Color items)
  • ControlCode (eg: ESC, CR, FF, LF, HT, BS, BEL)
  • FeCode (eg: SS2, SS3, DCS, ...)

Control codes and more sequences will be supported soon.

func ToColorInt added in v0.5.5

func ToColorInt(s string) Color

type Color16 added in v0.6.68

type Color16 int // ANSI Escaped Sequences here
const (
	Reset Color16 = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)
const (
	ResetBold Color16 = iota + 22
	ResetItalic
	ResetUnderline
	ResetBlinking

	ResetReversed
	ResetConcealed
	ResetCrossedOut
)

func NewColor16 added in v0.6.68

func NewColor16(clr Color16) Color16

NewColor16 cast a clr to Color16.

Valid Color16 color codes include: fore- and bg-color (eg FgRed, BgBlack, ...), and effects (such as bold/hilight - BgBoldOrBright, italic - BgItalic, dim - BgDim, ...)

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	var clr = color.NewColor16(color.FgCyan)
	var str = clr.Color()
	fmt.Println(str, "hello")
}
Output:
�[36m hello

func (Color16) Color added in v0.6.68

func (c Color16) Color() string

func (Color16) ColorTo added in v0.6.68

func (c Color16) ColorTo(out io.Writer)

func (Color16) Int added in v0.6.68

func (c Color16) Int() int

func (Color16) String added in v0.6.68

func (c Color16) String() string

type Color16m added in v0.6.68

type Color16m struct {
	// contains filtered or unexported fields
}

func NewColor16m added in v0.6.68

func NewColor16m(r, g, b byte, isBg bool) Color16m

NewColor16m constrcuts a true-color object which can be serialized as ansi escaped sequences by calling Color() string.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	var clr = color.NewColor16m(173, 137, 73, false)
	fmt.Println(clr.Color(), "hello")
	clr = color.NewColor16m(173, 137, 73, true)
	fmt.Println(clr.Color(), "hello")
}
Output:
�[38;2;173;137;73m hello
�[48;2;173;137;73m hello

func (Color16m) Color added in v0.6.68

func (c Color16m) Color() string

func (Color16m) ColorTo added in v0.6.68

func (c Color16m) ColorTo(out io.Writer)

func (Color16m) Int added in v0.6.68

func (c Color16m) Int() (color int)

func (Color16m) String added in v0.6.68

func (c Color16m) String() string

type Color256 added in v0.6.68

type Color256 struct {
	// contains filtered or unexported fields
}

func NewColor256 added in v0.6.68

func NewColor256(clr byte, isBg bool) Color256

NewColor16m constrcuts a 8-bit object which can be serialized as ansi escaped sequences by calling Color() string.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	var clr = color.NewColor256(byte(137), false)
	fmt.Println(clr.Color(), "hello")
	clr = color.NewColor256(byte(137), true)
	fmt.Println(clr.Color(), "hello")
}
Output:
�[38;5;137m hello
�[48;5;137m hello

func (Color256) Color added in v0.6.68

func (c Color256) Color() string

func (Color256) ColorTo added in v0.6.68

func (c Color256) ColorTo(out io.Writer)

func (Color256) Int added in v0.6.68

func (c Color256) Int() (color int)

func (Color256) String added in v0.6.68

func (c Color256) String() string

type ControlCode added in v0.6.68

type ControlCode byte
const (
	BEL ControlCode = bell           // CTRL-G BEL, Makes an audible noise.
	BS  ControlCode = backspace      // CTRL-H BS, Moves the cursor left (but may "backwards wrap" if cursor is at start of line).
	HT  ControlCode = tabstop        // CTRL-I HT, Moves the cursor right to next tab stop.
	LF  ControlCode = linefeed       // CTRL-J LF, Moves to next line, scrolls the display up if at bottom of the screen. Usually does not move horizontally, though programs should not rely on this.
	FF  ControlCode = formfeed       // CTRL-L FF, Move a printer to top of next page. Usually does not move horizontally, though programs should not rely on this. Effect on video terminals varies.
	CR  ControlCode = carriagereturn // CTRL-M CR, Moves the cursor to column zero.
	ESC ControlCode = escape         // CTRL-[ ESC, Starts all the escape sequences
)

See also these rune(s)

const bell = '\x07' // CTRL-G BEL, Makes an audible noise. const backspace = '\x08' // CTRL-H BS, Moves the cursor left (but may "backwards wrap" if cursor is at start of line). const tabstop = '\x09' // CTRL-I HT, Moves the cursor right to next tab stop. const linefeed = '\x0a' // CTRL-J LF, Moves to next line, scrolls the display up if at bottom of the screen. Usually does not move horizontally, though programs should not rely on this. const formfeed = '\x0c' // CTRL-L FF, Move a printer to top of next page. Usually does not move horizontally, though programs should not rely on this. Effect on video terminals varies. const carriagereturn = '\x0d' // CTRL-M CR, Moves the cursor to column zero. const escape = '\x1b' // CTRL-[ ESC, Starts all the escape sequences

func NewControlCode added in v0.6.68

func NewControlCode(code ControlCode) ControlCode

NewControlCode return the given ControlCode code directly.

func (ControlCode) Color added in v0.6.68

func (c ControlCode) Color() string

func (ControlCode) ColorTo added in v0.6.68

func (c ControlCode) ColorTo(out io.Writer)

func (ControlCode) Int added in v0.6.68

func (c ControlCode) Int() (color int)

func (ControlCode) String added in v0.6.68

func (c ControlCode) String() string

type Cursor added in v0.6.68

type Cursor struct {
	// contains filtered or unexported fields
}

func New added in v0.6.68

func New() (s *Cursor)

New returns a *cS (Cursor) object so that you could render the colorful text with it.

Each cursor object must have ended by `Build()`.

For example:

// another colorful builfer
c = color.New()
fmt.Println(c.Color16(color.FgRed).

Printf("hello, %s.", "world").Println().Build())

With *cS (Cursor) object, you could make Color16(), Color256(), or RGB() text.

See the example codes.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/states"
	"github.com/hedzr/is/term/color"
)

func main() {
	// start a color text builder
	var c = color.New()

	// specially for running on remote ci server
	if states.Env().IsNoColorMode() {
		states.Env().SetNoColorMode(true)
	}

	// paint and get the result (with ansi-color-seq ready)
	var result = c.Println().
		Color16(color.FgRed).
		Printf("hello, %s.", "world").Println().
		SavePos().
		Println("x").
		Color16(color.FgGreen).Printf("hello, %s.\n", "world").
		Color256(160).Printf("[160] hello, %s.\n", "world").
		Color256(161).Printf("[161] hello, %s.\n", "world").
		Color256(162).Printf("[162] hello, %s.\n", "world").
		Color256(163).Printf("[163] hello, %s.\n", "world").
		Color256(164).Printf("[164] hello, %s.\n", "world").
		Color256(165).Printf("[165] hello, %s.\n", "world").
		Up(3).Echo(" ERASED ").
		RGB(211, 211, 33).Printf("[16m] hello, %s.", "world").
		Println().
		RestorePos().
		Println("z").
		Down(8).
		Println("DONE").
		Build()

		// and render the result
	fmt.Println(result)

	// For most of ttys, the output looks like:
	//
	// �[31mhello, world.�[0m
	// �[sx
	// �[32mhello, world.
	// �[38;5;160m[160] hello, world.
	// �[38;5;161m[161] hello, world.
	// �[38;5;162m[162] hello, world.
	// �[38;5;163m[163] hello, world.
	// �[38;5;164m[164] hello, world.
	// �[38;5;165m[165] hello, world.
	// �[0m�[3A ERASED �[38;2;211;211;33m[16m] hello, world.
	// �[uz
	// �[8BDONE
}

func (*Cursor) AUXPortOff added in v0.6.68

func (s *Cursor) AUXPortOff() csiS

func (*Cursor) AUXPortOn added in v0.6.68

func (s *Cursor) AUXPortOn() csiS

func (*Cursor) Bg added in v0.6.68

func (s *Cursor) Bg(bg Color, format string, args ...any) *Cursor
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Println(c.Bg(color.BgRed, "hello, %s.", "world").Build())
}
Output:
�[41mhello, world.�[0m

func (*Cursor) Bg256 added in v0.6.68

func (s *Cursor) Bg256(n byte) c256S

Bg256 starts a child builder for 256-colors background color. The `n` is in 0..255.

func (*Cursor) BgBlack added in v0.6.68

func (s *Cursor) BgBlack(format string, args ...any) *Cursor
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c. // Color16(color.FgRed).
			BgBlack("[BGBLACK] hello, %s.\n", "world").String())
}
Output:
�[40m[BGBLACK] hello, world.
�[0m

func (*Cursor) BgBlue added in v0.6.68

func (s *Cursor) BgBlue(format string, args ...any) *Cursor

func (*Cursor) BgCyan added in v0.6.68

func (s *Cursor) BgCyan(format string, args ...any) *Cursor

func (*Cursor) BgDarkGray added in v0.6.68

func (s *Cursor) BgDarkGray(format string, args ...any) *Cursor

func (*Cursor) BgGreen added in v0.6.68

func (s *Cursor) BgGreen(format string, args ...any) *Cursor

func (*Cursor) BgLightBlue added in v0.6.68

func (s *Cursor) BgLightBlue(format string, args ...any) *Cursor

func (*Cursor) BgLightCyan added in v0.6.68

func (s *Cursor) BgLightCyan(format string, args ...any) *Cursor

func (*Cursor) BgLightGray added in v0.6.68

func (s *Cursor) BgLightGray(format string, args ...any) *Cursor

func (*Cursor) BgLightGreen added in v0.6.68

func (s *Cursor) BgLightGreen(format string, args ...any) *Cursor

func (*Cursor) BgLightMagenta added in v0.6.68

func (s *Cursor) BgLightMagenta(format string, args ...any) *Cursor

func (*Cursor) BgLightRed added in v0.6.68

func (s *Cursor) BgLightRed(format string, args ...any) *Cursor

func (*Cursor) BgLightYellow added in v0.6.68

func (s *Cursor) BgLightYellow(format string, args ...any) *Cursor

func (*Cursor) BgMagenta added in v0.6.68

func (s *Cursor) BgMagenta(format string, args ...any) *Cursor

func (*Cursor) BgRGB added in v0.6.68

func (s *Cursor) BgRGB(r, g, b int) c16MS

BgRGB starts a child builder for true-colors background color. The `r`, `g`, and `b` are a 0..255 number.

func (*Cursor) BgRed added in v0.6.68

func (s *Cursor) BgRed(format string, args ...any) *Cursor

func (*Cursor) BgWhite added in v0.6.68

func (s *Cursor) BgWhite(format string, args ...any) *Cursor

func (*Cursor) BgYellow added in v0.6.68

func (s *Cursor) BgYellow(format string, args ...any) *Cursor

func (*Cursor) Black added in v0.6.68

func (s *Cursor) Black(format string, args ...any) *Cursor
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c. // Color16(color.FgRed).
			Black("[BLACK] hello, %s.\n", "world").String())
}
Output:
�[30m[BLACK] hello, world.
�[0m

func (*Cursor) Blue added in v0.6.68

func (s *Cursor) Blue(format string, args ...any) *Cursor

func (*Cursor) Build added in v0.6.68

func (s *Cursor) Build() (r string)

func (*Cursor) CSI added in v0.6.68

func (s *Cursor) CSI(what uint8, n ...int) csiS

func (*Cursor) Color added in v0.6.68

func (s *Cursor) Color(clr Color, format string, args ...any) *Cursor
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Println(c.Color(color.FgRed, "hello, %s.", "world").Build())
}
Output:
�[31mhello, world.�[0m

func (*Cursor) Color16 added in v0.6.68

func (s *Cursor) Color16(clr Color16) c16S

Color16 make a csi 16-colors sequences.

For example,

var c = color.New()

// don't close it, but Println() will close it automatically
c.Color16(color.FgRed, false).Printf("hello, %s", "world\n").Println()
// don;t close, but String() will close it automatically
c.Color16(color.FgGreen, false).Printf("hello, %s", "world\n")

t.Logf("%s", c.String())
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Println(c.Color16(color.FgRed).
		Printf("hello, %s.", "world").Println().Build())
}
Output:
�[31mhello, world.�[0m

func (*Cursor) Color256 added in v0.6.68

func (s *Cursor) Color256(n byte) c256S

Color256 starts a child builder for 256-colors foreground color. The `n` is in 0..255.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c.
		Color256(163).Printf("[163] hello, %s.\n", "world").
		Color256(164).Printf("[164] hello, %s.\n", "world").
		Color256(165).Printf("[165] hello, %s.\n", "world").
		Build())
}
Output:
�[38;5;163m[163] hello, world.
�[38;5;164m[164] hello, world.
�[38;5;165m[165] hello, world.
�[0m

func (*Cursor) CursorBack added in v0.8.25

func (s *Cursor) CursorBack(n int) csiS

func (*Cursor) CursorDown added in v0.8.25

func (s *Cursor) CursorDown(n int) csiS

func (*Cursor) CursorErase added in v0.8.25

func (s *Cursor) CursorErase(n EraseTo) csiS

func (*Cursor) CursorEraseInLine added in v0.8.25

func (s *Cursor) CursorEraseInLine(n int) csiS

func (*Cursor) CursorForward added in v0.8.25

func (s *Cursor) CursorForward(n int) csiS

func (*Cursor) CursorHorzCol added in v0.8.25

func (s *Cursor) CursorHorzCol(colAbs int) csiS

func (*Cursor) CursorHorzVertPos added in v0.8.25

func (s *Cursor) CursorHorzVertPos(n, m int) csiS

func (*Cursor) CursorNextLine added in v0.8.25

func (s *Cursor) CursorNextLine(n int) csiS

func (*Cursor) CursorPos added in v0.8.25

func (s *Cursor) CursorPos(col, row int) csiS

func (*Cursor) CursorPrevLine added in v0.8.25

func (s *Cursor) CursorPrevLine(n int) csiS

func (*Cursor) CursorRestorePos added in v0.8.25

func (s *Cursor) CursorRestorePos() csiS

func (*Cursor) CursorSGR added in v0.8.25

func (s *Cursor) CursorSGR(n int) csiS

func (*Cursor) CursorSavePos added in v0.8.25

func (s *Cursor) CursorSavePos() csiS

func (*Cursor) CursorScrollDown added in v0.8.25

func (s *Cursor) CursorScrollDown(n int) csiS

func (*Cursor) CursorScrollUp added in v0.8.25

func (s *Cursor) CursorScrollUp(n int) csiS

func (*Cursor) CursorUp added in v0.8.25

func (s *Cursor) CursorUp(n int) csiS

func (*Cursor) Cyan added in v0.6.68

func (s *Cursor) Cyan(format string, args ...any) *Cursor

func (*Cursor) DSR added in v0.6.68

func (s *Cursor) DSR() csiS

func (*Cursor) Down added in v0.6.68

func (s *Cursor) Down(n int) *Cursor

=color.Down()

func (s *Cursor) EBlink(format string, args ...any) *Cursor

func (*Cursor) EBold added in v0.6.68

func (s *Cursor) EBold(format string, args ...any) *Cursor

func (*Cursor) EDim added in v0.6.68

func (s *Cursor) EDim(format string, args ...any) *Cursor
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c. // Color16(color.FgRed).
			EDim("[DIM] hello, %s.\n", "world").String())
}
Output:
�[2m[DIM] hello, world.
�[0m

func (*Cursor) EHidden added in v0.6.68

func (s *Cursor) EHidden(format string, args ...any) *Cursor

func (*Cursor) EHighlight added in v0.6.68

func (s *Cursor) EHighlight(format string, args ...any) *Cursor

func (*Cursor) EInverse added in v0.6.68

func (s *Cursor) EInverse(format string, args ...any) *Cursor

func (*Cursor) EItalic added in v0.6.68

func (s *Cursor) EItalic(format string, args ...any) *Cursor

func (*Cursor) ENormal added in v0.6.68

func (s *Cursor) ENormal(format string, args ...any) *Cursor
func (s *Cursor) ERapidBlink(format string, args ...any) *Cursor
func (s *Cursor) EResetBlink(format string, args ...any) *Cursor

func (*Cursor) EResetBold added in v0.6.68

func (s *Cursor) EResetBold(format string, args ...any) *Cursor

func (*Cursor) EResetDim added in v0.6.68

func (s *Cursor) EResetDim(format string, args ...any) *Cursor

func (*Cursor) EResetHidden added in v0.6.68

func (s *Cursor) EResetHidden(format string, args ...any) *Cursor

func (*Cursor) EResetInverse added in v0.6.68

func (s *Cursor) EResetInverse(format string, args ...any) *Cursor

func (*Cursor) EResetItalic added in v0.6.68

func (s *Cursor) EResetItalic(format string, args ...any) *Cursor

func (*Cursor) EResetStrikeout added in v0.6.68

func (s *Cursor) EResetStrikeout(format string, args ...any) *Cursor

func (*Cursor) EResetUnderline added in v0.6.68

func (s *Cursor) EResetUnderline(format string, args ...any) *Cursor

func (*Cursor) EStrikeout added in v0.6.68

func (s *Cursor) EStrikeout(format string, args ...any) *Cursor

func (*Cursor) EUnderline added in v0.6.68

func (s *Cursor) EUnderline(format string, args ...any) *Cursor

func (*Cursor) Echo added in v0.6.68

func (s *Cursor) Echo(args ...string) *Cursor

func (*Cursor) Effect added in v0.6.68

func (s *Cursor) Effect(bg Color, format string, args ...any) *Cursor
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Println(c.Effect(color.BgDim, "hello, %s.", "world").Build())
}
Output:
�[2mhello, world.�[0m

func (*Cursor) EraseLine added in v0.8.25

func (s *Cursor) EraseLine() *Cursor

func (*Cursor) Flush added in v0.6.68

func (s *Cursor) Flush() *Cursor

func (*Cursor) Green added in v0.6.68

func (s *Cursor) Green(format string, args ...any) *Cursor

func (*Cursor) HorizontalAbsolute added in v0.8.25

func (s *Cursor) HorizontalAbsolute(n int) *Cursor

func (*Cursor) Left added in v0.8.25

func (s *Cursor) Left(n int) *Cursor

=color.Left()

func (*Cursor) LightBlack added in v0.6.68

func (s *Cursor) LightBlack(format string, args ...any) *Cursor

func (*Cursor) LightBlue added in v0.6.68

func (s *Cursor) LightBlue(format string, args ...any) *Cursor

func (*Cursor) LightCyan added in v0.6.68

func (s *Cursor) LightCyan(format string, args ...any) *Cursor

func (*Cursor) LightGray added in v0.6.68

func (s *Cursor) LightGray(format string, args ...any) *Cursor

func (*Cursor) LightGreen added in v0.6.68

func (s *Cursor) LightGreen(format string, args ...any) *Cursor

func (*Cursor) LightMagenta added in v0.6.68

func (s *Cursor) LightMagenta(format string, args ...any) *Cursor

func (*Cursor) LightRed added in v0.6.68

func (s *Cursor) LightRed(format string, args ...any) *Cursor

func (*Cursor) LightYellow added in v0.6.68

func (s *Cursor) LightYellow(format string, args ...any) *Cursor

func (*Cursor) Magenta added in v0.6.68

func (s *Cursor) Magenta(format string, args ...any) *Cursor

func (*Cursor) Print added in v0.6.68

func (s *Cursor) Print(args ...any) *Cursor

func (*Cursor) Printf added in v0.6.68

func (s *Cursor) Printf(format string, args ...any) *Cursor

func (*Cursor) Println added in v0.6.68

func (s *Cursor) Println(args ...any) *Cursor

func (*Cursor) RGB added in v0.6.68

func (s *Cursor) RGB(r, g, b int) c16MS

RGB starts a child builder for true-colors foreground color. The `r`, `g`, and `b` are a 0..255 number.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c.
		RGB(211, 211, 33).Printf("[16m] hello, %s.\n", "world").
		BgRGB(211, 211, 33).Printf("[16m] hello, %s.\n", "world").
		Build())
}
Output:
�[38;2;211;211;33m[16m] hello, world.
�[48;2;211;211;33m[16m] hello, world.
�[0m

func (*Cursor) Red added in v0.6.68

func (s *Cursor) Red(format string, args ...any) *Cursor

func (*Cursor) Reset added in v0.6.68

func (s *Cursor) Reset()

func (*Cursor) ResetColor added in v0.6.68

func (s *Cursor) ResetColor() *Cursor

func (*Cursor) RestorePos added in v0.6.68

func (s *Cursor) RestorePos() *Cursor

=color.RestorePos()

func (*Cursor) Right added in v0.8.25

func (s *Cursor) Right(n int) *Cursor

=color.Right()

func (*Cursor) SavePos added in v0.6.68

func (s *Cursor) SavePos() *Cursor

=color.SavePos()

func (*Cursor) ScrollDown added in v0.6.68

func (s *Cursor) ScrollDown(n int) *Cursor

=color.ScrollDown()

func (*Cursor) ScrollUp added in v0.6.68

func (s *Cursor) ScrollUp(n int) *Cursor

=color.ScrollUp()

func (*Cursor) String added in v0.6.68

func (s *Cursor) String() string

func (*Cursor) StripHTMLTags added in v0.6.68

func (s *Cursor) StripHTMLTags(str string) *Cursor

StripHTMLTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.

func (*Cursor) StripLeftTabs added in v0.6.68

func (s *Cursor) StripLeftTabs(str string) *Cursor

StripLeftTabs strips the least left side tab chars from lines. It also strips html tags.

func (*Cursor) StripLeftTabsColorful added in v0.6.68

func (s *Cursor) StripLeftTabsColorful(str string) *Cursor

StripLeftTabsColorful strips the least left side tab chars from lines. It also strips html tags. At the end, StripLeftTabsC try translate color code in string.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c. // Color16(color.FgRed).
			StripLeftTabsColorful(`
		<code>code</code> | <kbd>CTRL</kbd>
		<b>bold / strong / em</b>
		<i>italic / cite</i>
		<u>underline</u>
		<mark>inverse mark</mark>
		<del>strike / del </del>
		<font color="green">green text</font>
		`).String())
}
Output:
�[51;1mcode�[0m�[0m | �[51;1mCTRL�[0m�[0m
�[1mbold / strong / em�[0m�[0m
�[3mitalic / cite�[0m�[0m
�[4munderline�[0m�[0m
�[7minverse mark�[0m�[0m
�[9mstrike / del �[0m�[0m
�[32mgreen text�[0m�[0m

func (*Cursor) StripLeftTabsOnly added in v0.6.68

func (s *Cursor) StripLeftTabsOnly(str string) *Cursor

StripLeftTabsOnly strips the least left side tab chars from lines.

func (*Cursor) Translate added in v0.6.68

func (s *Cursor) Translate(str string) *Cursor

Translate translates color tags from input string and make it colorful.

The tags include:

<kbd></kbd>, <b></b>, ...
Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	// another colorful builfer
	var c = color.New()
	fmt.Print(c. // Color16(color.FgRed).
			Translate(`<code>code</code> | <kbd>CTRL</kbd>
		<b>bold / strong / em</b>
		<i>italic / cite</i>
		<u>underline</u>
		<mark>inverse mark</mark>
		<del>strike / del </del>
		<font color="green">green text</font>
		`).String())
}
Output:
�[51;1mcode�[0m�[39m | �[51;1mCTRL�[0m�[39m
		�[1mbold / strong / em�[0m�[39m
		�[3mitalic / cite�[0m�[39m
		�[4munderline�[0m�[39m
		�[7minverse mark�[0m�[39m
		�[9mstrike / del �[0m�[39m
		�[32mgreen text�[0m�[39m

func (*Cursor) Up added in v0.6.68

func (s *Cursor) Up(n int) *Cursor

=color.Up()

func (*Cursor) White added in v0.6.68

func (s *Cursor) White(format string, args ...any) *Cursor

func (*Cursor) WithWriter added in v0.6.68

func (s *Cursor) WithWriter(w io.Writer) *Cursor

func (*Cursor) Yellow added in v0.6.68

func (s *Cursor) Yellow(format string, args ...any) *Cursor

type EraseTo added in v0.6.68

type EraseTo int
const (
	CursorEraseToEnd EraseTo = iota
	CursorEraseToBegin
	CursorEraseAll
)

type FeCode added in v0.6.68

type FeCode byte

FeCode will be expanded as ESC + byte sequence. For example, CSI ('\x9B') will be expanded to '\x1B\x9B' (`ESC [`).

const (
	SS2 FeCode = '\x8E' // ESC N
	SS3 FeCode = '\x8F' // ESC 0
	DCS FeCode = '\x90' // ESC P
	CSI FeCode = '\x9B' // ESC [
	ST  FeCode = '\x9c' // ESC \
	OSC FeCode = '\x9D' // ESC ]
	SOS FeCode = '\x98' // ESC X
	PM  FeCode = '\x9E' // ESC ^
	APC FeCode = '\x9F' // ESC _
)

func NewFeCode added in v0.6.68

func NewFeCode(code FeCode) FeCode

NewFeCode return the given FeCode code directly.

func (FeCode) Color added in v0.6.68

func (c FeCode) Color() string

func (FeCode) ColorTo added in v0.6.68

func (c FeCode) ColorTo(out io.Writer)

func (FeCode) Int added in v0.6.68

func (c FeCode) Int() (color int)

func (FeCode) String added in v0.6.68

func (c FeCode) String() string

type FmtBuf added in v0.6.68

type FmtBuf interface {
	CWriter
	PutBack() (str string)
}

type Index added in v0.5.20

type Index struct{}

Index is an indexer for retrieving the entries in this subpackage.

func (Index) ColoredFast added in v0.5.20

func (Index) ColoredFast(out io.Writer, clr Color, text string)

func (Index) DimFast added in v0.5.20

func (Index) DimFast(out io.Writer, text string)

func (Index) GetColorTranslater added in v0.5.20

func (Index) GetColorTranslater() Translator

GetColorTranslater returns a translator about ANSI Escaped Code. It may or may not translating colored text depending on cabin.GetNoColorMode.

func (Index) GetColorTranslaterAlways added in v0.5.20

func (Index) GetColorTranslaterAlways() Translator

func (Index) GetColorTranslaterNever added in v0.5.20

func (Index) GetColorTranslaterNever() Translator

func (Index) GetDummyTranslater added in v0.5.20

func (Index) GetDummyTranslater() Translator

func (Index) HighlightFast added in v0.5.20

func (Index) HighlightFast(out io.Writer, text string)

func (Index) ToColorInt added in v0.5.20

func (Index) ToColorInt(s string) Color

func (Index) ToColorString added in v0.5.20

func (Index) ToColorString(clr Color) string

func (Index) WrapColorAndBgTo added in v0.5.20

func (Index) WrapColorAndBgTo(out io.Writer, clr, bg Color, text string)

func (Index) WrapDimToLite added in v0.5.20

func (Index) WrapDimToLite(out io.Writer, text string)

type Result added in v0.6.68

type Result struct {
	bytes.Buffer
}

func (Result) String added in v0.6.68

func (s Result) String() string

type RowsBlock added in v0.6.68

type RowsBlock struct {
	// contains filtered or unexported fields
}

RowsBlock displays content which can be updated on the fly. You can use this to create live output, such as progressbar, etc.

func NewRowsBlock added in v0.6.68

func NewRowsBlock() RowsBlock

NewRowsBlock returns a new RowsBlock.

A RowsBlock displays content which can be updated on the fly. You can use this to create live output, such as progressbar, etc.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/hedzr/is/term/color"
)

func main() {
	rb := color.NewRowsBlock()

	// the following outputs will be displayed in first
	// line of the RowsBlock.
	for ul := range 10 {
		spc := strings.Repeat("+", ul)
		str := fmt.Sprintf("%sHello, World!\n", spc)
		rb.Update(str)
	}

	// don't test this example because the outputs on different tty (or ci servers) could fail.

	// Outputs:
	// �[0G�[2KHello, World!
	// �[0G�[2K�[1A�[2K�[0G+Hello, World!
	// �[0G�[2K�[1A�[2K�[0G++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G+++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G++++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G+++++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G++++++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G+++++++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G++++++++Hello, World!
	// �[0G�[2K�[1A�[2K�[0G+++++++++Hello, World!
}

func (*RowsBlock) Bottom added in v0.6.68

func (s *RowsBlock) Bottom()

Bottom moves the cursor to the bottom of the RowsBlock. This is done by calculating how many lines were moved by Up and Down.

func (*RowsBlock) Clear added in v0.6.68

func (s *RowsBlock) Clear()

Clear clears the content of the RowsBlock.

func (*RowsBlock) ClearLinesDown added in v0.6.68

func (s *RowsBlock) ClearLinesDown(n int)

ClearLinesDown clears n lines downwards from the current position and moves the cursor.

func (*RowsBlock) ClearLinesUp added in v0.6.68

func (s *RowsBlock) ClearLinesUp(n int)

ClearLinesUp clears n lines upwards from the current position and moves the cursor.

func (*RowsBlock) Cursor added in v0.6.68

func (s *RowsBlock) Cursor() *Cursor

Cursor returns the *cS (Cursor) object so that you could render the colorful text with it.

func (*RowsBlock) Down added in v0.6.68

func (s *RowsBlock) Down(n int)

Down moves the cursor of the RowsBlock down one line.

func (*RowsBlock) DownAndClear added in v0.6.68

func (s *RowsBlock) DownAndClear(n int)

DownAndClear moves the cursor down by n lines, then clears the line.

func (*RowsBlock) HideCursor added in v0.6.68

func (s *RowsBlock) HideCursor()

HideCursor make the console cursor invisible

func (*RowsBlock) Home added in v0.6.68

func (s *RowsBlock) Home()

Home moves the cursor to the start of the current line.

func (*RowsBlock) HomeAndLineDown added in v0.6.68

func (s *RowsBlock) HomeAndLineDown(n int)

HomeAndLineDown moves the cursor down by n lines, then moves to cursor to the start of the line.

func (*RowsBlock) HomeAndLineUp added in v0.6.68

func (s *RowsBlock) HomeAndLineUp(n int)

HomeAndLineUp moves the cursor up by n lines, then moves to cursor to the start of the line.

func (*RowsBlock) Move added in v0.6.68

func (s *RowsBlock) Move(x, y int)

Move moves the cursor relative by x and y.

func (*RowsBlock) ShowCursor added in v0.6.68

func (s *RowsBlock) ShowCursor()

ShowCursor make the console cursor visible

func (*RowsBlock) Top added in v0.6.68

func (s *RowsBlock) Top()

Top moves the cursor to the top of the RowsBlock. This is done by calculating how many lines were moved by Up and Down.

func (*RowsBlock) Up added in v0.6.68

func (s *RowsBlock) Up(n int)

Up moves the cursor of the RowsBlock up one line.

func (*RowsBlock) UpAndClear added in v0.6.68

func (s *RowsBlock) UpAndClear(n int)

UpAndClear moves the cursor up by n lines, then clears the line.

func (*RowsBlock) Update added in v0.6.68

func (s *RowsBlock) Update(content string)

Update overwrites the content of the RowsBlock and adjusts its height based on content.

func (*RowsBlock) WithWriter added in v0.6.68

func (s *RowsBlock) WithWriter(writer Writer) *RowsBlock

WithWriter sets the custom writer.

type Style added in v0.6.68

type Style struct {
	Items []Color
}

Style is an array of Color objects

func NewStyle added in v0.6.68

func NewStyle() Style

NewStyle creates a container of Color objects. All of these children will be bound and printed in a one sequences.

Example
package main

import (
	"fmt"

	"github.com/hedzr/is/term/color"
)

func main() {
	c := color.NewStyle()
	c.Add(
		color.NewColor16(color.FgYellow),    // fg
		color.NewColor16m(77, 88, 99, true), // bg
		// color.Reset,
	)
	fmt.Printf("%sHello, World!%s\n", c, color.Reset)
}
Output:
�[33m�[48;2;77;88;99mHello, World!�[0m

func (*Style) Add added in v0.6.68

func (c *Style) Add(colors ...Color) *Style

func (Style) Color added in v0.6.68

func (c Style) Color() string

func (Style) ColorTo added in v0.6.68

func (c Style) ColorTo(out io.Writer)

func (Style) Int added in v0.6.68

func (c Style) Int() (color int)

func (Style) String added in v0.6.68

func (c Style) String() string

type Translator

type Translator interface {
	Translate(s string, initialFg Color) string

	ColoredFast(out io.Writer, clr Color, text string)
	DimFast(out io.Writer, text string)
	HighlightFast(out io.Writer, text string)

	WriteColor(out io.Writer, clr Color)   // echo ansi color bytes for foreground color
	WriteBgColor(out io.Writer, clr Color) // echo ansi color bytes for background color
	Reset(out io.Writer)                   // echo ansi color bytes for resetting color

	Bold(out io.Writer, cb func(out io.Writer))
	Italic(out io.Writer, cb func(out io.Writer))
	Underline(out io.Writer, cb func(out io.Writer))
	Inverse(out io.Writer, cb func(out io.Writer))
	Dim(out io.Writer, cb func(out io.Writer))
	Blink(out io.Writer, cb func(out io.Writer))
	Bg(out io.Writer, bgColor Color, cb func(out io.Writer))

	// TranslateTo(s string, initialState Color) string
	TranslateTo(s string, initialState Color) string // translate a string with html tags to a colored string

	StripLeftTabsAndColorize(s string) string // strip left tabs and colorize the string
	StripLeftTabs(s string) string            // strip left tabs and colorize the string
	StripLeftTabsOnly(s string) string        // strip left tabs only

	ToColorString(clr Color) string
	ToColorInt(s string) Color
	// contains filtered or unexported methods
}

Translator _

func GetCPT

func GetCPT() Translator

GetCPT returns a translator about ANSI Escaped Code. It may or may not translating colored text depending on cabin.GetNoColorMode.

func GetCPTC

func GetCPTC() Translator

func GetCPTNC

func GetCPTNC() Translator

func GetDummyTranslator

func GetDummyTranslator() Translator

type Writer added in v0.6.68

type Writer interface {
	io.Writer
	Fd() uintptr
}
var Out Writer = os.Stdout

Out is the default output writer for the Writer

Jump to

Keyboard shortcuts

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