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:
- by NewColor16, or use Color16 constants directly like FgBlack, BgGreen, ...
- by NewColor256 to make a 8-bit 256-colors object
- by NewColor16m to make a true-color object
- by NewControlCode or ControlCode constants
- by NewFeCode or FeCode constants
- by NewSGR or use CSIsgr constants directly like SGRdim, SGRstrike, ...
- by NewStyle to make a compounded object
- ...
See also docsite.
Index ¶
- Constants
- func Color256table(out io.Writer)
- func Colored(clr Color, format string, args ...any)
- func Coloredf(clr Color, format string, args ...any)
- func Dim(format string, args ...any)
- func Dimf(format string, args ...any)
- func Down(n int)
- func Hide()
- func Highlight(format string, args ...any)
- func Left(n int)
- func NewFmtBuf() (s *fmtbufS)
- func ResetColor(c Color)
- func RestorePos()
- func Right(n int)
- func SavePos()
- func ScrollDown(n int)
- func ScrollUp(n int)
- func Show()
- func StripHTMLTags(s string) string
- func StripLeftTabs(s string) string
- func StripLeftTabsC(s string) string
- func StripLeftTabsOnly(s string) string
- func Text(format string, args ...any)
- func ToColor(clr Color, format string, args ...any) (str string)
- func ToColorString(clr Color) string
- func ToDim(format string, args ...any) (str string)
- func ToHighlight(format string, args ...any) (str string)
- func Up(n int)
- func WrapColorAndBgTo(out io.Writer, clr, bg Color, text string)
- func WrapColorTo(out io.Writer, clr Color, text string)
- func WrapDimTo(out io.Writer, text string)
- func WrapDimToLite(out io.Writer, text string)
- func WrapHighlightTo(out io.Writer, text string)
- func Write(b []byte) (n int, e error)
- type CSICode
- type CSICode2
- type CSICodes
- type CSIsgr
- type CSIsuffix
- type CWriter
- type Color
- type Color16
- type Color16m
- type Color256
- type ControlCode
- type Cursor
- func (s *Cursor) AUXPortOff() csiS
- func (s *Cursor) AUXPortOn() csiS
- func (s *Cursor) Bg(bg Color, format string, args ...any) *Cursor
- func (s *Cursor) Bg256(n byte) c256S
- func (s *Cursor) BgBlack(format string, args ...any) *Cursor
- func (s *Cursor) BgBlue(format string, args ...any) *Cursor
- func (s *Cursor) BgCyan(format string, args ...any) *Cursor
- func (s *Cursor) BgDarkGray(format string, args ...any) *Cursor
- func (s *Cursor) BgGreen(format string, args ...any) *Cursor
- func (s *Cursor) BgLightBlue(format string, args ...any) *Cursor
- func (s *Cursor) BgLightCyan(format string, args ...any) *Cursor
- func (s *Cursor) BgLightGray(format string, args ...any) *Cursor
- func (s *Cursor) BgLightGreen(format string, args ...any) *Cursor
- func (s *Cursor) BgLightMagenta(format string, args ...any) *Cursor
- func (s *Cursor) BgLightRed(format string, args ...any) *Cursor
- func (s *Cursor) BgLightYellow(format string, args ...any) *Cursor
- func (s *Cursor) BgMagenta(format string, args ...any) *Cursor
- func (s *Cursor) BgRGB(r, g, b int) c16MS
- func (s *Cursor) BgRed(format string, args ...any) *Cursor
- func (s *Cursor) BgWhite(format string, args ...any) *Cursor
- func (s *Cursor) BgYellow(format string, args ...any) *Cursor
- func (s *Cursor) Black(format string, args ...any) *Cursor
- func (s *Cursor) Blue(format string, args ...any) *Cursor
- func (s *Cursor) Build() (r string)
- func (s *Cursor) CSI(what uint8, n ...int) csiS
- func (s *Cursor) Color(clr Color, format string, args ...any) *Cursor
- func (s *Cursor) Color16(clr Color16) c16S
- func (s *Cursor) Color256(n byte) c256S
- func (s *Cursor) CursorBack(n int) csiS
- func (s *Cursor) CursorDown(n int) csiS
- func (s *Cursor) CursorErase(n EraseTo) csiS
- func (s *Cursor) CursorEraseInLine(n int) csiS
- func (s *Cursor) CursorForward(n int) csiS
- func (s *Cursor) CursorHorzCol(colAbs int) csiS
- func (s *Cursor) CursorHorzVertPos(n, m int) csiS
- func (s *Cursor) CursorNextLine(n int) csiS
- func (s *Cursor) CursorPos(col, row int) csiS
- func (s *Cursor) CursorPrevLine(n int) csiS
- func (s *Cursor) CursorRestorePos() csiS
- func (s *Cursor) CursorSGR(n int) csiS
- func (s *Cursor) CursorSavePos() csiS
- func (s *Cursor) CursorScrollDown(n int) csiS
- func (s *Cursor) CursorScrollUp(n int) csiS
- func (s *Cursor) CursorUp(n int) csiS
- func (s *Cursor) Cyan(format string, args ...any) *Cursor
- func (s *Cursor) DSR() csiS
- func (s *Cursor) Down(n int) *Cursor
- func (s *Cursor) EBlink(format string, args ...any) *Cursor
- func (s *Cursor) EBold(format string, args ...any) *Cursor
- func (s *Cursor) EDim(format string, args ...any) *Cursor
- func (s *Cursor) EHidden(format string, args ...any) *Cursor
- func (s *Cursor) EHighlight(format string, args ...any) *Cursor
- func (s *Cursor) EInverse(format string, args ...any) *Cursor
- func (s *Cursor) EItalic(format string, args ...any) *Cursor
- 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 (s *Cursor) EResetBold(format string, args ...any) *Cursor
- func (s *Cursor) EResetDim(format string, args ...any) *Cursor
- func (s *Cursor) EResetHidden(format string, args ...any) *Cursor
- func (s *Cursor) EResetInverse(format string, args ...any) *Cursor
- func (s *Cursor) EResetItalic(format string, args ...any) *Cursor
- func (s *Cursor) EResetStrikeout(format string, args ...any) *Cursor
- func (s *Cursor) EResetUnderline(format string, args ...any) *Cursor
- func (s *Cursor) EStrikeout(format string, args ...any) *Cursor
- func (s *Cursor) EUnderline(format string, args ...any) *Cursor
- func (s *Cursor) Echo(args ...string) *Cursor
- func (s *Cursor) Effect(bg Color, format string, args ...any) *Cursor
- func (s *Cursor) EraseLine() *Cursor
- func (s *Cursor) Flush() *Cursor
- func (s *Cursor) Green(format string, args ...any) *Cursor
- func (s *Cursor) HorizontalAbsolute(n int) *Cursor
- func (s *Cursor) Left(n int) *Cursor
- func (s *Cursor) LightBlack(format string, args ...any) *Cursor
- func (s *Cursor) LightBlue(format string, args ...any) *Cursor
- func (s *Cursor) LightCyan(format string, args ...any) *Cursor
- func (s *Cursor) LightGray(format string, args ...any) *Cursor
- func (s *Cursor) LightGreen(format string, args ...any) *Cursor
- func (s *Cursor) LightMagenta(format string, args ...any) *Cursor
- func (s *Cursor) LightRed(format string, args ...any) *Cursor
- func (s *Cursor) LightYellow(format string, args ...any) *Cursor
- func (s *Cursor) Magenta(format string, args ...any) *Cursor
- func (s *Cursor) Print(args ...any) *Cursor
- func (s *Cursor) Printf(format string, args ...any) *Cursor
- func (s *Cursor) Println(args ...any) *Cursor
- func (s *Cursor) RGB(r, g, b int) c16MS
- func (s *Cursor) Red(format string, args ...any) *Cursor
- func (s *Cursor) Reset()
- func (s *Cursor) ResetColor() *Cursor
- func (s *Cursor) RestorePos() *Cursor
- func (s *Cursor) Right(n int) *Cursor
- func (s *Cursor) SavePos() *Cursor
- func (s *Cursor) ScrollDown(n int) *Cursor
- func (s *Cursor) ScrollUp(n int) *Cursor
- func (s *Cursor) String() string
- func (s *Cursor) StripHTMLTags(str string) *Cursor
- func (s *Cursor) StripLeftTabs(str string) *Cursor
- func (s *Cursor) StripLeftTabsColorful(str string) *Cursor
- func (s *Cursor) StripLeftTabsOnly(str string) *Cursor
- func (s *Cursor) Translate(str string) *Cursor
- func (s *Cursor) Up(n int) *Cursor
- func (s *Cursor) White(format string, args ...any) *Cursor
- func (s *Cursor) WithWriter(w io.Writer) *Cursor
- func (s *Cursor) Yellow(format string, args ...any) *Cursor
- type EraseTo
- type FeCode
- type FmtBuf
- type Index
- func (Index) ColoredFast(out io.Writer, clr Color, text string)
- func (Index) DimFast(out io.Writer, text string)
- func (Index) GetColorTranslater() Translator
- func (Index) GetColorTranslaterAlways() Translator
- func (Index) GetColorTranslaterNever() Translator
- func (Index) GetDummyTranslater() Translator
- func (Index) HighlightFast(out io.Writer, text string)
- func (Index) ToColorInt(s string) Color
- func (Index) ToColorString(clr Color) string
- func (Index) WrapColorAndBgTo(out io.Writer, clr, bg Color, text string)
- func (Index) WrapDimToLite(out io.Writer, text string)
- type Result
- type RowsBlock
- func (s *RowsBlock) Bottom()
- func (s *RowsBlock) Clear()
- func (s *RowsBlock) ClearLinesDown(n int)
- func (s *RowsBlock) ClearLinesUp(n int)
- func (s *RowsBlock) Cursor() *Cursor
- func (s *RowsBlock) Down(n int)
- func (s *RowsBlock) DownAndClear(n int)
- func (s *RowsBlock) HideCursor()
- func (s *RowsBlock) Home()
- func (s *RowsBlock) HomeAndLineDown(n int)
- func (s *RowsBlock) HomeAndLineUp(n int)
- func (s *RowsBlock) Move(x, y int)
- func (s *RowsBlock) ShowCursor()
- func (s *RowsBlock) Top()
- func (s *RowsBlock) Up(n int)
- func (s *RowsBlock) UpAndClear(n int)
- func (s *RowsBlock) Update(content string)
- func (s *RowsBlock) WithWriter(writer Writer) *RowsBlock
- type Style
- type Translator
- type Writer
Examples ¶
Constants ¶
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) )
const ESCAPE = '\x1b'
Variables ¶
This section is empty.
Functions ¶
func Color256table ¶ added in v0.6.68
Color256table prints a 8-bit color table for testing
func Colored ¶
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 ¶
Coloredf outputs formatted message to stdout while logger level less than level.WarnLevel and cmdr is in VERBOSE mode.
func Dim ¶
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 ¶
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 Highlight ¶
Highlight outputs formatted message to stdout while logger level less than level.WarnLevel. For level.SetLevel(level.ErrorLevel), the text will be discarded.
func ResetColor ¶
func ResetColor(c Color)
func RestorePos ¶ added in v0.6.68
func RestorePos()
func ScrollDown ¶ added in v0.6.68
func ScrollDown(n int)
func StripHTMLTags ¶
StripHTMLTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.
func StripLeftTabs ¶
StripLeftTabs strips the least left side tab chars from lines. StripLeftTabs strips html tags too.
func StripLeftTabsC ¶
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 ¶
StripLeftTabsOnly strips the least left side tab chars from lines.
func ToColorString ¶ added in v0.5.5
func ToHighlight ¶
func WrapDimToLite ¶
func WrapHighlightTo ¶
Types ¶
type CSICode ¶ added in v0.6.68
CSICode will be expanded to `CSI n byte` form. For example, Cursor Up ('A') expands as `CSI n A`.
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 CSIAddCode1 ¶ added in v0.6.68
func CSIAddCode2 ¶ added in v0.6.68
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
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
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. )
type Color ¶
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
type Color16 ¶ added in v0.6.68
type Color16 int // ANSI Escaped Sequences here
func NewColor16 ¶ added in v0.6.68
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
type Color16m ¶ added in v0.6.68
type Color16m struct {
// contains filtered or unexported fields
}
func NewColor16m ¶ added in v0.6.68
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
type Color256 ¶ added in v0.6.68
type Color256 struct {
// contains filtered or unexported fields
}
func NewColor256 ¶ added in v0.6.68
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
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
}
Output:
func (*Cursor) AUXPortOff ¶ added in v0.6.68
func (s *Cursor) AUXPortOff() csiS
func (*Cursor) Bg ¶ added in v0.6.68
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
Bg256 starts a child builder for 256-colors background color. The `n` is in 0..255.
func (*Cursor) BgBlack ¶ added in v0.6.68
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) BgDarkGray ¶ added in v0.6.68
func (*Cursor) BgLightBlue ¶ added in v0.6.68
func (*Cursor) BgLightCyan ¶ added in v0.6.68
func (*Cursor) BgLightGray ¶ added in v0.6.68
func (*Cursor) BgLightGreen ¶ added in v0.6.68
func (*Cursor) BgLightMagenta ¶ added in v0.6.68
func (*Cursor) BgLightRed ¶ added in v0.6.68
func (*Cursor) BgLightYellow ¶ added in v0.6.68
func (*Cursor) BgRGB ¶ added in v0.6.68
BgRGB starts a child builder for true-colors background color. The `r`, `g`, and `b` are a 0..255 number.
func (*Cursor) Black ¶ added in v0.6.68
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) Color ¶ added in v0.6.68
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
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
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 (*Cursor) CursorDown ¶ added in v0.8.25
func (*Cursor) CursorErase ¶ added in v0.8.25
func (*Cursor) CursorEraseInLine ¶ added in v0.8.25
func (*Cursor) CursorForward ¶ added in v0.8.25
func (*Cursor) CursorHorzCol ¶ added in v0.8.25
func (*Cursor) CursorHorzVertPos ¶ added in v0.8.25
func (*Cursor) CursorNextLine ¶ added in v0.8.25
func (*Cursor) CursorPrevLine ¶ added in v0.8.25
func (*Cursor) CursorRestorePos ¶ added in v0.8.25
func (s *Cursor) CursorRestorePos() csiS
func (*Cursor) CursorSavePos ¶ added in v0.8.25
func (s *Cursor) CursorSavePos() csiS
func (*Cursor) CursorScrollDown ¶ added in v0.8.25
func (*Cursor) CursorScrollUp ¶ added in v0.8.25
func (*Cursor) EDim ¶ added in v0.6.68
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) EHighlight ¶ added in v0.6.68
func (*Cursor) ERapidBlink ¶ added in v0.6.68
func (*Cursor) EResetBlink ¶ added in v0.6.68
func (*Cursor) EResetBold ¶ added in v0.6.68
func (*Cursor) EResetHidden ¶ added in v0.6.68
func (*Cursor) EResetInverse ¶ added in v0.6.68
func (*Cursor) EResetItalic ¶ added in v0.6.68
func (*Cursor) EResetStrikeout ¶ added in v0.6.68
func (*Cursor) EResetUnderline ¶ added in v0.6.68
func (*Cursor) EStrikeout ¶ added in v0.6.68
func (*Cursor) EUnderline ¶ added in v0.6.68
func (*Cursor) Effect ¶ added in v0.6.68
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) HorizontalAbsolute ¶ added in v0.8.25
func (*Cursor) LightBlack ¶ added in v0.6.68
func (*Cursor) LightGreen ¶ added in v0.6.68
func (*Cursor) LightMagenta ¶ added in v0.6.68
func (*Cursor) LightYellow ¶ added in v0.6.68
func (*Cursor) RGB ¶ added in v0.6.68
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) ResetColor ¶ added in v0.6.68
func (*Cursor) RestorePos ¶ added in v0.6.68
=color.RestorePos()
func (*Cursor) ScrollDown ¶ added in v0.6.68
=color.ScrollDown()
func (*Cursor) StripHTMLTags ¶ added in v0.6.68
StripHTMLTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.
func (*Cursor) StripLeftTabs ¶ added in v0.6.68
StripLeftTabs strips the least left side tab chars from lines. It also strips html tags.
func (*Cursor) StripLeftTabsColorful ¶ added in v0.6.68
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
StripLeftTabsOnly strips the least left side tab chars from lines.
func (*Cursor) Translate ¶ added in v0.6.68
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
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 [`).
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) 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) ToColorInt ¶ added in v0.5.20
func (Index) ToColorString ¶ added in v0.5.20
func (Index) WrapColorAndBgTo ¶ added in v0.5.20
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!
}
Output:
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
ClearLinesDown clears n lines downwards from the current position and moves the cursor.
func (*RowsBlock) ClearLinesUp ¶ added in v0.6.68
ClearLinesUp clears n lines upwards from the current position and moves the cursor.
func (*RowsBlock) Cursor ¶ added in v0.6.68
Cursor returns the *cS (Cursor) object so that you could render the colorful text with it.
func (*RowsBlock) DownAndClear ¶ added in v0.6.68
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
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
HomeAndLineUp moves the cursor up by n lines, then moves to cursor to the start of the line.
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) UpAndClear ¶ added in v0.6.68
UpAndClear moves the cursor up by n lines, then clears the line.
func (*RowsBlock) Update ¶ added in v0.6.68
Update overwrites the content of the RowsBlock and adjusts its height based on content.
func (*RowsBlock) WithWriter ¶ added in v0.6.68
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
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