Package text
- Constants
- Variables
- func DisableColors()
- func EnableColors()
- func Escape(str string, escapeSeq string) string
- func Filter(s []string, f func(string) bool) []string
- func InsertEveryN(str string, runeToInsert rune, n int) string
- func LongestLineLen(str string) int
- func OverrideRuneWidthEastAsianWidth(val bool)
- func Pad(str string, maxLen int, paddingChar rune) string
- func RepeatAndTrim(str string, maxRunes int) string
- func RuneCount(str string) int
- func RuneWidth(r rune) int
- func RuneWidthWithoutEscSequences(str string) int
- func Snip(str string, length int, snipIndicator string) string
- func StripEscape(str string) string
- func Trim(str string, maxLen int) string
- func WrapHard(str string, wrapLen int) string
- func WrapSoft(str string, wrapLen int) string
- func WrapText(str string, wrapLen int) string
- type Align
- func (a Align) Apply(text string, maxLength int) string
- func (a Align) HTMLProperty() string
- func (a Align) MarkdownProperty() string
- type Color
- func (c Color) EscapeSeq() string
- func (c Color) HTMLProperty() string
- func (c Color) Sprint(a ...interface{}) string
- func (c Color) Sprintf(format string, a ...interface{}) string
- type Colors
- func (c Colors) EscapeSeq() string
- func (c Colors) HTMLProperty() string
- func (c Colors) Sprint(a ...interface{}) string
- func (c Colors) Sprintf(format string, a ...interface{}) string
- type Cursor
- func (c Cursor) Sprint() string
- func (c Cursor) Sprintn(n int) string
- type Direction
- func (d Direction) Modifier() string
- type Format
- func (tc Format) Apply(text string) string
- type Transformer
- func NewJSONTransformer(prefix string, indent string) Transformer
- func NewNumberTransformer(format string) Transformer
- func NewTimeTransformer(layout string, location *time.Location) Transformer
- func NewURLTransformer(colors ...Color) Transformer
- func NewUnixTimeTransformer(layout string, location *time.Location) Transformer
- type VAlign
- func (va VAlign) Apply(lines []string, maxLines int) []string
- func (va VAlign) ApplyStr(text string, maxLines int) []string
- func (va VAlign) HTMLProperty() string
Package files
align.go
ansi.go
ansi_unix.go
color.go
color_html.go
cursor.go
direction.go
filter.go
format.go
string.go
transformer.go
valign.go
wrap.go
Constants
Constants
const (
EscapeReset = EscapeStart + "0" + EscapeStop
EscapeStart = "\x1b["
EscapeStartRune = rune(27)
EscapeStop = "m"
EscapeStopRune = 'm'
)
Variables
ANSICodesSupported will be true on consoles where ANSI Escape Codes/Sequences
are supported.
var ANSICodesSupported = areANSICodesSupported()
func DisableColors()
DisableColors (forcefully) disables color coding globally.
func EnableColors()
EnableColors (forcefully) enables color coding globally.
func Escape(str string, escapeSeq string) string
Escape encodes the string with the ANSI Escape Sequence.
For ex.:
Escape("Ghost", "") == "Ghost"
Escape("Ghost", "\x1b[91m") == "\x1b[91mGhost\x1b[0m"
Escape("\x1b[94mGhost\x1b[0mLady", "\x1b[91m") == "\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"
Escape("Nymeria\x1b[94mGhost\x1b[0mLady", "\x1b[91m") == "\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"
Escape("Nymeria \x1b[94mGhost\x1b[0m Lady", "\x1b[91m") == "\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m"
▾ Example
Code:
fmt.Printf("Escape(%#v, %#v) == %#v\n", "Ghost", "", Escape("Ghost", ""))
fmt.Printf("Escape(%#v, %#v) == %#v\n", "Ghost", FgHiRed.EscapeSeq(), Escape("Ghost", FgHiRed.EscapeSeq()))
fmt.Printf("Escape(%#v, %#v) == %#v\n", FgHiBlue.Sprint("Ghost")+"Lady", FgHiRed.EscapeSeq(), Escape(FgHiBlue.Sprint("Ghost")+"Lady", FgHiRed.EscapeSeq()))
fmt.Printf("Escape(%#v, %#v) == %#v\n", "Nymeria"+FgHiBlue.Sprint("Ghost")+"Lady", FgHiRed.EscapeSeq(), Escape("Nymeria"+FgHiBlue.Sprint("Ghost")+"Lady", FgHiRed.EscapeSeq()))
fmt.Printf("Escape(%#v, %#v) == %#v\n", "Nymeria "+FgHiBlue.Sprint("Ghost")+" Lady", FgHiRed.EscapeSeq(), Escape("Nymeria "+FgHiBlue.Sprint("Ghost")+" Lady", FgHiRed.EscapeSeq()))
Output:
Escape("Ghost", "") == "Ghost"
Escape("Ghost", "\x1b[91m") == "\x1b[91mGhost\x1b[0m"
Escape("\x1b[94mGhost\x1b[0mLady", "\x1b[91m") == "\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"
Escape("Nymeria\x1b[94mGhost\x1b[0mLady", "\x1b[91m") == "\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"
Escape("Nymeria \x1b[94mGhost\x1b[0m Lady", "\x1b[91m") == "\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m"
func Filter(s []string, f func(string) bool) []string
Filter filters the slice 's' to items which return truth when passed to 'f'.
▾ Example
Code:
slice := []string{"Arya Stark", "Bran Stark", "Jon Snow", "Sansa Stark"}
filter := func(item string) bool {
return strings.HasSuffix(item, "Stark")
}
fmt.Printf("%#v\n", Filter(slice, filter))
Output:
[]string{"Arya Stark", "Bran Stark", "Sansa Stark"}
func InsertEveryN(str string, runeToInsert rune, n int) string
InsertEveryN inserts the rune every N characters in the string. For ex.:
InsertEveryN("Ghost", '-', 1) == "G-h-o-s-t"
InsertEveryN("Ghost", '-', 2) == "Gh-os-t"
InsertEveryN("Ghost", '-', 3) == "Gho-st"
InsertEveryN("Ghost", '-', 4) == "Ghos-t"
InsertEveryN("Ghost", '-', 5) == "Ghost"
▾ Example
Code:
fmt.Printf("InsertEveryN(\"Ghost\", '-', 0): %#v\n", InsertEveryN("Ghost", '-', 0))
fmt.Printf("InsertEveryN(\"Ghost\", '-', 1): %#v\n", InsertEveryN("Ghost", '-', 1))
fmt.Printf("InsertEveryN(\"Ghost\", '-', 2): %#v\n", InsertEveryN("Ghost", '-', 2))
fmt.Printf("InsertEveryN(\"Ghost\", '-', 3): %#v\n", InsertEveryN("Ghost", '-', 3))
fmt.Printf("InsertEveryN(\"Ghost\", '-', 4): %#v\n", InsertEveryN("Ghost", '-', 4))
fmt.Printf("InsertEveryN(\"Ghost\", '-', 5): %#v\n", InsertEveryN("Ghost", '-', 5))
fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 0): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 0))
fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 1): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 1))
fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 2): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 2))
fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 3): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 3))
fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 4): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 4))
fmt.Printf("InsertEveryN(\"\\x1b[33mGhost\\x1b[0m\", '-', 5): %#v\n", InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 5))
Output:
InsertEveryN("Ghost", '-', 0): "Ghost"
InsertEveryN("Ghost", '-', 1): "G-h-o-s-t"
InsertEveryN("Ghost", '-', 2): "Gh-os-t"
InsertEveryN("Ghost", '-', 3): "Gho-st"
InsertEveryN("Ghost", '-', 4): "Ghos-t"
InsertEveryN("Ghost", '-', 5): "Ghost"
InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 0): "\x1b[33mGhost\x1b[0m"
InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 1): "\x1b[33mG-h-o-s-t\x1b[0m"
InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 2): "\x1b[33mGh-os-t\x1b[0m"
InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 3): "\x1b[33mGho-st\x1b[0m"
InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 4): "\x1b[33mGhos-t\x1b[0m"
InsertEveryN("\x1b[33mGhost\x1b[0m", '-', 5): "\x1b[33mGhost\x1b[0m"
func LongestLineLen(str string) int
LongestLineLen returns the length of the longest "line" within the
argument string. For ex.:
LongestLineLen("Ghost!\nCome back here!\nRight now!") == 15
▾ Example
Code:
fmt.Printf("LongestLineLen(\"\"): %d\n", LongestLineLen(""))
fmt.Printf("LongestLineLen(\"\\n\\n\"): %d\n", LongestLineLen("\n\n"))
fmt.Printf("LongestLineLen(\"Ghost\"): %d\n", LongestLineLen("Ghost"))
fmt.Printf("LongestLineLen(\"Ghostツ\"): %d\n", LongestLineLen("Ghostツ"))
fmt.Printf("LongestLineLen(\"Winter\\nIs\\nComing\"): %d\n", LongestLineLen("Winter\nIs\nComing"))
fmt.Printf("LongestLineLen(\"Mother\\nOf\\nDragons\"): %d\n", LongestLineLen("Mother\nOf\nDragons"))
fmt.Printf("LongestLineLen(\"\\x1b[33mMother\\x1b[0m\\nOf\\nDragons\"): %d\n", LongestLineLen("\x1b[33mMother\x1b[0m\nOf\nDragons"))
Output:
LongestLineLen(""): 0
LongestLineLen("\n\n"): 0
LongestLineLen("Ghost"): 5
LongestLineLen("Ghostツ"): 7
LongestLineLen("Winter\nIs\nComing"): 6
LongestLineLen("Mother\nOf\nDragons"): 7
LongestLineLen("\x1b[33mMother\x1b[0m\nOf\nDragons"): 7
func OverrideRuneWidthEastAsianWidth(val bool)
OverrideRuneWidthEastAsianWidth can *probably* help with alignment, and
length calculation issues when dealing with Unicode character-set and a
non-English language set in the LANG variable.
Set this to 'false' to force the "runewidth" library to pretend to deal with
English character-set. Be warned that if the text/content you are dealing
with contains East Asian character-set, this may result in unexpected
behavior.
References:
* https://github.com/mattn/go-runewidth/issues/64#issuecomment-1221642154
* https://github.com/jedib0t/go-pretty/issues/220
* https://github.com/jedib0t/go-pretty/issues/204
func Pad(str string, maxLen int, paddingChar rune) string
Pad pads the given string with as many characters as needed to make it as
long as specified (maxLen). This function does not count escape sequences
while calculating length of the string. Ex.:
Pad("Ghost", 0, ' ') == "Ghost"
Pad("Ghost", 3, ' ') == "Ghost"
Pad("Ghost", 5, ' ') == "Ghost"
Pad("Ghost", 7, ' ') == "Ghost "
Pad("Ghost", 10, '.') == "Ghost....."
▾ Example
Code:
fmt.Printf("%#v\n", Pad("Ghost", 0, ' '))
fmt.Printf("%#v\n", Pad("Ghost", 3, ' '))
fmt.Printf("%#v\n", Pad("Ghost", 5, ' '))
fmt.Printf("%#v\n", Pad("\x1b[33mGhost\x1b[0m", 7, '-'))
fmt.Printf("%#v\n", Pad("\x1b[33mGhost\x1b[0m", 10, '.'))
Output:
"Ghost"
"Ghost"
"Ghost"
"\x1b[33mGhost\x1b[0m--"
"\x1b[33mGhost\x1b[0m....."
func RepeatAndTrim(str string, maxRunes int) string
RepeatAndTrim repeats the given string until it is as long as maxRunes.
For ex.:
RepeatAndTrim("", 5) == ""
RepeatAndTrim("Ghost", 0) == ""
RepeatAndTrim("Ghost", 5) == "Ghost"
RepeatAndTrim("Ghost", 7) == "GhostGh"
RepeatAndTrim("Ghost", 10) == "GhostGhost"
▾ Example
Code:
fmt.Printf("RepeatAndTrim(\"\", 5): %#v\n", RepeatAndTrim("", 5))
fmt.Printf("RepeatAndTrim(\"Ghost\", 0): %#v\n", RepeatAndTrim("Ghost", 0))
fmt.Printf("RepeatAndTrim(\"Ghost\", 3): %#v\n", RepeatAndTrim("Ghost", 3))
fmt.Printf("RepeatAndTrim(\"Ghost\", 5): %#v\n", RepeatAndTrim("Ghost", 5))
fmt.Printf("RepeatAndTrim(\"Ghost\", 7): %#v\n", RepeatAndTrim("Ghost", 7))
fmt.Printf("RepeatAndTrim(\"Ghost\", 10): %#v\n", RepeatAndTrim("Ghost", 10))
Output:
RepeatAndTrim("", 5): ""
RepeatAndTrim("Ghost", 0): ""
RepeatAndTrim("Ghost", 3): "Gho"
RepeatAndTrim("Ghost", 5): "Ghost"
RepeatAndTrim("Ghost", 7): "GhostGh"
RepeatAndTrim("Ghost", 10): "GhostGhost"
func RuneCount(str string) int
RuneCount is similar to utf8.RuneCountInString, except for the fact that it
ignores escape sequences while counting. For ex.:
RuneCount("") == 0
RuneCount("Ghost") == 5
RuneCount("\x1b[33mGhost\x1b[0m") == 5
RuneCount("\x1b[33mGhost\x1b[0") == 5
Deprecated: in favor of RuneWidthWithoutEscSequences
▾ Example
Code:
fmt.Printf("RuneCount(\"\"): %d\n", RuneCount(""))
fmt.Printf("RuneCount(\"Ghost\"): %d\n", RuneCount("Ghost"))
fmt.Printf("RuneCount(\"Ghostツ\"): %d\n", RuneCount("Ghostツ"))
fmt.Printf("RuneCount(\"\\x1b[33mGhost\\x1b[0m\"): %d\n", RuneCount("\x1b[33mGhost\x1b[0m"))
fmt.Printf("RuneCount(\"\\x1b[33mGhost\\x1b[0\"): %d\n", RuneCount("\x1b[33mGhost\x1b[0"))
Output:
RuneCount(""): 0
RuneCount("Ghost"): 5
RuneCount("Ghostツ"): 7
RuneCount("\x1b[33mGhost\x1b[0m"): 5
RuneCount("\x1b[33mGhost\x1b[0"): 5
func RuneWidth(r rune) int
RuneWidth returns the mostly accurate character-width of the rune. This is
not 100% accurate as the character width is usually dependent on the
typeface (font) used in the console/terminal. For ex.:
RuneWidth('A') == 1
RuneWidth('ツ') == 2
RuneWidth('⊙') == 1
RuneWidth('︿') == 2
RuneWidth(0x27) == 0
▾ Example
Code:
fmt.Printf("RuneWidth('A'): %d\n", RuneWidth('A'))
fmt.Printf("RuneWidth('ツ'): %d\n", RuneWidth('ツ'))
fmt.Printf("RuneWidth('⊙'): %d\n", RuneWidth('⊙'))
fmt.Printf("RuneWidth('︿'): %d\n", RuneWidth('︿'))
fmt.Printf("RuneWidth(rune(27)): %d\n", RuneWidth(rune(27)))
Output:
RuneWidth('A'): 1
RuneWidth('ツ'): 2
RuneWidth('⊙'): 1
RuneWidth('︿'): 2
RuneWidth(rune(27)): 0
func RuneWidthWithoutEscSequences(str string) int
RuneWidthWithoutEscSequences is similar to RuneWidth, except for the fact
that it ignores escape sequences while counting. For ex.:
RuneWidthWithoutEscSequences("") == 0
RuneWidthWithoutEscSequences("Ghost") == 5
RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m") == 5
RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0") == 5
▾ Example
Code:
fmt.Printf("RuneWidthWithoutEscSequences(\"\"): %d\n", RuneWidthWithoutEscSequences(""))
fmt.Printf("RuneWidthWithoutEscSequences(\"Ghost\"): %d\n", RuneWidthWithoutEscSequences("Ghost"))
fmt.Printf("RuneWidthWithoutEscSequences(\"Ghostツ\"): %d\n", RuneWidthWithoutEscSequences("Ghostツ"))
fmt.Printf("RuneWidthWithoutEscSequences(\"\\x1b[33mGhost\\x1b[0m\"): %d\n", RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"))
fmt.Printf("RuneWidthWithoutEscSequences(\"\\x1b[33mGhost\\x1b[0\"): %d\n", RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"))
Output:
RuneWidthWithoutEscSequences(""): 0
RuneWidthWithoutEscSequences("Ghost"): 5
RuneWidthWithoutEscSequences("Ghostツ"): 7
RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0m"): 5
RuneWidthWithoutEscSequences("\x1b[33mGhost\x1b[0"): 5
func Snip(str string, length int, snipIndicator string) string
Snip returns the given string with a fixed length. For ex.:
Snip("Ghost", 0, "~") == "Ghost"
Snip("Ghost", 1, "~") == "~"
Snip("Ghost", 3, "~") == "Gh~"
Snip("Ghost", 5, "~") == "Ghost"
Snip("Ghost", 7, "~") == "Ghost "
Snip("\x1b[33mGhost\x1b[0m", 7, "~") == "\x1b[33mGhost\x1b[0m "
▾ Example
Code:
fmt.Printf("Snip(\"Ghost\", 0, \"~\"): %#v\n", Snip("Ghost", 0, "~"))
fmt.Printf("Snip(\"Ghost\", 1, \"~\"): %#v\n", Snip("Ghost", 1, "~"))
fmt.Printf("Snip(\"Ghost\", 3, \"~\"): %#v\n", Snip("Ghost", 3, "~"))
fmt.Printf("Snip(\"Ghost\", 5, \"~\"): %#v\n", Snip("Ghost", 5, "~"))
fmt.Printf("Snip(\"Ghost\", 7, \"~\"): %#v\n", Snip("Ghost", 7, "~"))
fmt.Printf("Snip(\"\\x1b[33mGhost\\x1b[0m\", 7, \"~\"): %#v\n", Snip("\x1b[33mGhost\x1b[0m", 7, "~"))
Output:
Snip("Ghost", 0, "~"): "Ghost"
Snip("Ghost", 1, "~"): "~"
Snip("Ghost", 3, "~"): "Gh~"
Snip("Ghost", 5, "~"): "Ghost"
Snip("Ghost", 7, "~"): "Ghost"
Snip("\x1b[33mGhost\x1b[0m", 7, "~"): "\x1b[33mGhost\x1b[0m"
func StripEscape(str string) string
StripEscape strips all ANSI Escape Sequence from the string.
For ex.:
StripEscape("Ghost") == "Ghost"
StripEscape("\x1b[91mGhost\x1b[0m") == "Ghost"
StripEscape("\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m") == "GhostLady"
StripEscape("\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m") == "NymeriaGhostLady"
StripEscape("\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m") == "Nymeria Ghost Lady"
▾ Example
Code:
fmt.Printf("StripEscape(%#v) == %#v\n", "Ghost", StripEscape("Ghost"))
fmt.Printf("StripEscape(%#v) == %#v\n", "\x1b[91mGhost\x1b[0m", StripEscape("\x1b[91mGhost\x1b[0m"))
fmt.Printf("StripEscape(%#v) == %#v\n", "\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m", StripEscape("\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"))
fmt.Printf("StripEscape(%#v) == %#v\n", "\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m", StripEscape("\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m"))
fmt.Printf("StripEscape(%#v) == %#v\n", "\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m", StripEscape("\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m"))
Output:
StripEscape("Ghost") == "Ghost"
StripEscape("\x1b[91mGhost\x1b[0m") == "Ghost"
StripEscape("\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m") == "GhostLady"
StripEscape("\x1b[91mNymeria\x1b[94mGhost\x1b[0m\x1b[91mLady\x1b[0m") == "NymeriaGhostLady"
StripEscape("\x1b[91mNymeria \x1b[94mGhost\x1b[0m\x1b[91m Lady\x1b[0m") == "Nymeria Ghost Lady"
func Trim(str string, maxLen int) string
Trim trims a string to the given length while ignoring escape sequences. For
ex.:
Trim("Ghost", 3) == "Gho"
Trim("Ghost", 6) == "Ghost"
Trim("\x1b[33mGhost\x1b[0m", 3) == "\x1b[33mGho\x1b[0m"
Trim("\x1b[33mGhost\x1b[0m", 6) == "\x1b[33mGhost\x1b[0m"
▾ Example
Code:
fmt.Printf("Trim(\"Ghost\", 0): %#v\n", Trim("Ghost", 0))
fmt.Printf("Trim(\"Ghost\", 3): %#v\n", Trim("Ghost", 3))
fmt.Printf("Trim(\"Ghost\", 6): %#v\n", Trim("Ghost", 6))
fmt.Printf("Trim(\"\\x1b[33mGhost\\x1b[0m\", 0): %#v\n", Trim("\x1b[33mGhost\x1b[0m", 0))
fmt.Printf("Trim(\"\\x1b[33mGhost\\x1b[0m\", 3): %#v\n", Trim("\x1b[33mGhost\x1b[0m", 3))
fmt.Printf("Trim(\"\\x1b[33mGhost\\x1b[0m\", 6): %#v\n", Trim("\x1b[33mGhost\x1b[0m", 6))
Output:
Trim("Ghost", 0): ""
Trim("Ghost", 3): "Gho"
Trim("Ghost", 6): "Ghost"
Trim("\x1b[33mGhost\x1b[0m", 0): ""
Trim("\x1b[33mGhost\x1b[0m", 3): "\x1b[33mGho\x1b[0m"
Trim("\x1b[33mGhost\x1b[0m", 6): "\x1b[33mGhost\x1b[0m"
func WrapHard(str string, wrapLen int) string
WrapHard wraps a string to the given length using a newline. Handles strings
with ANSI escape sequences (such as text color) without breaking the text
formatting. Breaks all words that go beyond the line boundary.
For examples, refer to the unit-tests or GoDoc examples.
▾ Example
Code:
str := `The quick brown fox jumped over the lazy dog.
A big crocodile died empty-fanged, gulping horribly in jerking kicking little
motions. Nonchalant old Peter Quinn ruthlessly shot the under-water vermin with
Xavier yelling Zap!`
strWrapped := WrapHard(str, 30)
for idx, line := range strings.Split(strWrapped, "\n") {
fmt.Printf("Line #%02d: '%s'\n", idx+1, line)
}
Output:
Line #01: 'The quick brown fox jumped ove'
Line #02: 'r the lazy dog.'
Line #03: ''
Line #04: 'A big crocodile died empty-fan'
Line #05: 'ged, gulping horribly in jerki'
Line #06: 'ng kicking little motions. Non'
Line #07: 'chalant old Peter Quinn ruthle'
Line #08: 'ssly shot the under-water verm'
Line #09: 'in with Xavier yelling Zap!'
func WrapSoft(str string, wrapLen int) string
WrapSoft wraps a string to the given length using a newline. Handles strings
with ANSI escape sequences (such as text color) without breaking the text
formatting. Tries to move words that go beyond the line boundary to the next
line.
For examples, refer to the unit-tests or GoDoc examples.
▾ Example
Code:
str := `The quick brown fox jumped over the lazy dog.
A big crocodile died empty-fanged, gulping horribly in jerking kicking little
motions. Nonchalant old Peter Quinn ruthlessly shot the under-water vermin with
Xavier yelling Zap!`
strWrapped := WrapSoft(str, 30)
for idx, line := range strings.Split(strWrapped, "\n") {
fmt.Printf("Line #%02d: '%s'\n", idx+1, line)
}
Output:
Line #01: 'The quick brown fox jumped '
Line #02: 'over the lazy dog.'
Line #03: ''
Line #04: 'A big crocodile died '
Line #05: 'empty-fanged, gulping horribly'
Line #06: 'in jerking kicking little '
Line #07: 'motions. Nonchalant old Peter '
Line #08: 'Quinn ruthlessly shot the '
Line #09: 'under-water vermin with Xavier'
Line #10: 'yelling Zap!'
func WrapText(str string, wrapLen int) string
WrapText is very similar to WrapHard except for one minor difference. Unlike
WrapHard which discards line-breaks and respects only paragraph-breaks, this
function respects line-breaks too.
For examples, refer to the unit-tests or GoDoc examples.
▾ Example
Code:
str := `The quick brown fox jumped over the lazy dog.
A big crocodile died empty-fanged, gulping horribly in jerking kicking little
motions. Nonchalant old Peter Quinn ruthlessly shot the under-water vermin with
Xavier yelling Zap!`
strWrapped := WrapText(str, 30)
for idx, line := range strings.Split(strWrapped, "\n") {
fmt.Printf("Line #%02d: '%s'\n", idx+1, line)
}
Output:
Line #01: 'The quick brown fox jumped ove'
Line #02: 'r the lazy dog.'
Line #03: ''
Line #04: 'A big crocodile died empty-fan'
Line #05: 'ged, gulping horribly in jerki'
Line #06: 'ng kicking little'
Line #07: 'motions. Nonchalant old Peter '
Line #08: 'Quinn ruthlessly shot the unde'
Line #09: 'r-water vermin with'
Line #10: 'Xavier yelling Zap!'
Align denotes how text is to be aligned horizontally.
type Align int
Align enumerations
const (
AlignDefault Align = iota
AlignLeft
AlignCenter
AlignJustify
AlignRight
)
func (Align) Apply
¶
func (a Align) Apply(text string, maxLength int) string
Apply aligns the text as directed. For ex.:
- AlignDefault.Apply("Jon Snow", 12) returns "Jon Snow "
- AlignLeft.Apply("Jon Snow", 12) returns "Jon Snow "
- AlignCenter.Apply("Jon Snow", 12) returns " Jon Snow "
- AlignJustify.Apply("Jon Snow", 12) returns "Jon Snow"
- AlignRight.Apply("Jon Snow", 12) returns " Jon Snow"
▾ Example
Code:
fmt.Printf("AlignDefault: '%s'\n", AlignDefault.Apply("Jon Snow", 12))
fmt.Printf("AlignLeft : '%s'\n", AlignLeft.Apply("Jon Snow", 12))
fmt.Printf("AlignCenter : '%s'\n", AlignCenter.Apply("Jon Snow", 12))
fmt.Printf("AlignJustify: '%s'\n", AlignJustify.Apply("Jon Snow", 12))
fmt.Printf("AlignRight : '%s'\n", AlignRight.Apply("Jon Snow", 12))
Output:
AlignDefault: 'Jon Snow '
AlignLeft : 'Jon Snow '
AlignCenter : ' Jon Snow '
AlignJustify: 'Jon Snow'
AlignRight : ' Jon Snow'
func (a Align) HTMLProperty() string
HTMLProperty returns the equivalent HTML horizontal-align tag property.
▾ Example
Code:
fmt.Printf("AlignDefault: '%s'\n", AlignDefault.HTMLProperty())
fmt.Printf("AlignLeft : '%s'\n", AlignLeft.HTMLProperty())
fmt.Printf("AlignCenter : '%s'\n", AlignCenter.HTMLProperty())
fmt.Printf("AlignJustify: '%s'\n", AlignJustify.HTMLProperty())
fmt.Printf("AlignRight : '%s'\n", AlignRight.HTMLProperty())
Output:
AlignDefault: ''
AlignLeft : 'align="left"'
AlignCenter : 'align="center"'
AlignJustify: 'align="justify"'
AlignRight : 'align="right"'
func (a Align) MarkdownProperty() string
MarkdownProperty returns the equivalent Markdown horizontal-align separator.
▾ Example
Code:
fmt.Printf("AlignDefault: '%s'\n", AlignDefault.MarkdownProperty())
fmt.Printf("AlignLeft : '%s'\n", AlignLeft.MarkdownProperty())
fmt.Printf("AlignCenter : '%s'\n", AlignCenter.MarkdownProperty())
fmt.Printf("AlignJustify: '%s'\n", AlignJustify.MarkdownProperty())
fmt.Printf("AlignRight : '%s'\n", AlignRight.MarkdownProperty())
Output:
AlignDefault: ' --- '
AlignLeft : ':--- '
AlignCenter : ':---:'
AlignJustify: ' --- '
AlignRight : ' ---:'
Color represents a single color to render with.
type Color int
Base colors -- attributes in reality
const (
Reset Color = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)
Foreground colors
const (
FgBlack Color = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
Foreground Hi-Intensity colors
const (
FgHiBlack Color = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
Background colors
const (
BgBlack Color = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
Background Hi-Intensity colors
const (
BgHiBlack Color = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)
func (c Color) EscapeSeq() string
EscapeSeq returns the ANSI escape sequence for the color.
▾ Example
Code:
fmt.Printf("Black Background: %#v\n", BgBlack.EscapeSeq())
fmt.Printf("Black Foreground: %#v\n", FgBlack.EscapeSeq())
Output:
Black Background: "\x1b[40m"
Black Foreground: "\x1b[30m"
func (c Color) HTMLProperty() string
HTMLProperty returns the "class" attribute for the color.
▾ Example
Code:
fmt.Printf("Bold: %#v\n", Bold.HTMLProperty())
fmt.Printf("Black Background: %#v\n", BgBlack.HTMLProperty())
fmt.Printf("Black Foreground: %#v\n", FgBlack.HTMLProperty())
Output:
Bold: "class=\"bold\""
Black Background: "class=\"bg-black\""
Black Foreground: "class=\"fg-black\""
func (c Color) Sprint(a ...interface{}) string
Sprint colorizes and prints the given string(s).
▾ Example
Code:
fmt.Printf("%#v\n", BgBlack.Sprint("Black Background"))
fmt.Printf("%#v\n", FgBlack.Sprint("Black Foreground"))
Output:
"\x1b[40mBlack Background\x1b[0m"
"\x1b[30mBlack Foreground\x1b[0m"
func (c Color) Sprintf(format string, a ...interface{}) string
Sprintf formats and colorizes and prints the given string(s).
▾ Example
Code:
fmt.Printf("%#v\n", BgBlack.Sprintf("%s %s", "Black", "Background"))
fmt.Printf("%#v\n", FgBlack.Sprintf("%s %s", "Black", "Foreground"))
Output:
"\x1b[40mBlack Background\x1b[0m"
"\x1b[30mBlack Foreground\x1b[0m"
Colors represents an array of Color objects to render with.
Example: Colors{FgCyan, BgBlack}
type Colors []Color
func (c Colors) EscapeSeq() string
EscapeSeq returns the ANSI escape sequence for the colors set.
▾ Example
Code:
fmt.Printf("Black Background: %#v\n", Colors{BgBlack}.EscapeSeq())
fmt.Printf("Black Foreground: %#v\n", Colors{FgBlack}.EscapeSeq())
fmt.Printf("Black Background, White Foreground: %#v\n", Colors{BgBlack, FgWhite}.EscapeSeq())
fmt.Printf("Black Foreground, White Background: %#v\n", Colors{FgBlack, BgWhite}.EscapeSeq())
Output:
Black Background: "\x1b[40m"
Black Foreground: "\x1b[30m"
Black Background, White Foreground: "\x1b[40;37m"
Black Foreground, White Background: "\x1b[30;47m"
func (c Colors) HTMLProperty() string
HTMLProperty returns the "class" attribute for the colors.
▾ Example
Code:
fmt.Printf("Black Background: %#v\n", Colors{BgBlack}.HTMLProperty())
fmt.Printf("Black Foreground: %#v\n", Colors{FgBlack}.HTMLProperty())
fmt.Printf("Black Background, White Foreground: %#v\n", Colors{BgBlack, FgWhite}.HTMLProperty())
fmt.Printf("Black Foreground, White Background: %#v\n", Colors{FgBlack, BgWhite}.HTMLProperty())
fmt.Printf("Bold Italic Underline Red Text: %#v\n", Colors{Bold, Italic, Underline, FgRed}.HTMLProperty())
Output:
Black Background: "class=\"bg-black\""
Black Foreground: "class=\"fg-black\""
Black Background, White Foreground: "class=\"bg-black fg-white\""
Black Foreground, White Background: "class=\"bg-white fg-black\""
Bold Italic Underline Red Text: "class=\"bold fg-red italic underline\""
func (Colors) Sprint
¶
func (c Colors) Sprint(a ...interface{}) string
Sprint colorizes and prints the given string(s).
▾ Example
Code:
fmt.Printf("%#v\n", Colors{BgBlack}.Sprint("Black Background"))
fmt.Printf("%#v\n", Colors{BgBlack, FgWhite}.Sprint("Black Background, White Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack}.Sprint("Black Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack, BgWhite}.Sprint("Black Foreground, White Background"))
Output:
"\x1b[40mBlack Background\x1b[0m"
"\x1b[40;37mBlack Background, White Foreground\x1b[0m"
"\x1b[30mBlack Foreground\x1b[0m"
"\x1b[30;47mBlack Foreground, White Background\x1b[0m"
func (c Colors) Sprintf(format string, a ...interface{}) string
Sprintf formats and colorizes and prints the given string(s).
▾ Example
Code:
fmt.Printf("%#v\n", Colors{BgBlack}.Sprintf("%s %s", "Black", "Background"))
fmt.Printf("%#v\n", Colors{BgBlack, FgWhite}.Sprintf("%s, %s", "Black Background", "White Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack}.Sprintf("%s %s", "Black", "Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack, BgWhite}.Sprintf("%s, %s", "Black Foreground", "White Background"))
Output:
"\x1b[40mBlack Background\x1b[0m"
"\x1b[40;37mBlack Background, White Foreground\x1b[0m"
"\x1b[30mBlack Foreground\x1b[0m"
"\x1b[30;47mBlack Foreground, White Background\x1b[0m"
Cursor helps move the cursor on the console in multiple directions.
type Cursor rune
const (
CursorDown Cursor = 'B'
CursorLeft Cursor = 'D'
CursorRight Cursor = 'C'
CursorUp Cursor = 'A'
EraseLine Cursor = 'K'
)
func (Cursor) Sprint
¶
func (c Cursor) Sprint() string
Sprint prints the Escape Sequence to move the Cursor once.
▾ Example
Code:
fmt.Printf("CursorDown : %#v\n", CursorDown.Sprint())
fmt.Printf("CursorLeft : %#v\n", CursorLeft.Sprint())
fmt.Printf("CursorRight: %#v\n", CursorRight.Sprint())
fmt.Printf("CursorUp : %#v\n", CursorUp.Sprint())
fmt.Printf("EraseLine : %#v\n", EraseLine.Sprint())
Output:
CursorDown : "\x1b[B"
CursorLeft : "\x1b[D"
CursorRight: "\x1b[C"
CursorUp : "\x1b[A"
EraseLine : "\x1b[K"
func (c Cursor) Sprintn(n int) string
Sprintn prints the Escape Sequence to move the Cursor "n" times.
▾ Example
Code:
fmt.Printf("CursorDown : %#v\n", CursorDown.Sprintn(5))
fmt.Printf("CursorLeft : %#v\n", CursorLeft.Sprintn(5))
fmt.Printf("CursorRight: %#v\n", CursorRight.Sprintn(5))
fmt.Printf("CursorUp : %#v\n", CursorUp.Sprintn(5))
fmt.Printf("EraseLine : %#v\n", EraseLine.Sprintn(5))
Output:
CursorDown : "\x1b[5B"
CursorLeft : "\x1b[5D"
CursorRight: "\x1b[5C"
CursorUp : "\x1b[5A"
EraseLine : "\x1b[K"
Direction defines the overall flow of text. Similar to bidi.Direction, but
simplified and specific to this package.
type Direction int
Available Directions.
const (
Default Direction = iota
LeftToRight
RightToLeft
)
func (d Direction) Modifier() string
Modifier returns a character to force the given direction for the text that
follows the modifier.
Format lets you transform the text in supported methods while keeping escape
sequences in the string intact and untouched.
type Format int
Format enumerations
const (
FormatDefault Format = iota
FormatLower
FormatTitle
FormatUpper
)
func (tc Format) Apply(text string) string
Apply converts the text as directed.
Transformer helps format the contents of an object to the user's liking.
type Transformer func(val interface{}) string
func NewJSONTransformer(prefix string, indent string) Transformer
NewJSONTransformer returns a Transformer that can format a JSON string or an
object into pretty-indented JSON-strings.
func NewNumberTransformer(format string) Transformer
NewNumberTransformer returns a number Transformer that:
- transforms the number as directed by 'format' (ex.: %.2f)
- colors negative values Red
- colors positive values Green
func NewTimeTransformer(layout string, location *time.Location) Transformer
NewTimeTransformer returns a Transformer that can format a timestamp (a
time.Time) into a well-defined time format defined using the provided layout
(ex.: time.RFC3339).
If a non-nil location value is provided, the time will be localized to that
location (use time.Local to get localized timestamps).
func NewURLTransformer(colors ...Color) Transformer
NewURLTransformer returns a Transformer that can format and pretty print a string
that contains a URL (the text is underlined and colored Blue).
func NewUnixTimeTransformer(layout string, location *time.Location) Transformer
NewUnixTimeTransformer returns a Transformer that can format a unix-timestamp
into a well-defined time format as defined by 'layout'. This can handle
unix-time in Seconds, MilliSeconds, Microseconds and Nanoseconds.
If a non-nil location value is provided, the time will be localized to that
location (use time.Local to get localized timestamps).
VAlign denotes how text is to be aligned vertically.
type VAlign int
VAlign enumerations
const (
VAlignDefault VAlign = iota
VAlignTop
VAlignMiddle
VAlignBottom
)
func (VAlign) Apply
¶
func (va VAlign) Apply(lines []string, maxLines int) []string
Apply aligns the lines vertically. For ex.:
- VAlignTop.Apply({"Game", "Of", "Thrones"}, 5)
returns {"Game", "Of", "Thrones", "", ""}
- VAlignMiddle.Apply({"Game", "Of", "Thrones"}, 5)
returns {"", "Game", "Of", "Thrones", ""}
- VAlignBottom.Apply({"Game", "Of", "Thrones"}, 5)
returns {"", "", "Game", "Of", "Thrones"}
▾ Example
Code:
lines := []string{"Game", "Of", "Thrones"}
maxLines := 5
fmt.Printf("VAlignDefault: %#v\n", VAlignDefault.Apply(lines, maxLines))
fmt.Printf("VAlignTop : %#v\n", VAlignTop.Apply(lines, maxLines))
fmt.Printf("VAlignMiddle : %#v\n", VAlignMiddle.Apply(lines, maxLines))
fmt.Printf("VAlignBottom : %#v\n", VAlignBottom.Apply(lines, maxLines))
Output:
VAlignDefault: []string{"Game", "Of", "Thrones", "", ""}
VAlignTop : []string{"Game", "Of", "Thrones", "", ""}
VAlignMiddle : []string{"", "Game", "Of", "Thrones", ""}
VAlignBottom : []string{"", "", "Game", "Of", "Thrones"}
func (va VAlign) ApplyStr(text string, maxLines int) []string
ApplyStr aligns the string (of 1 or more lines) vertically. For ex.:
- VAlignTop.ApplyStr("Game\nOf\nThrones", 5)
returns {"Game", "Of", "Thrones", "", ""}
- VAlignMiddle.ApplyStr("Game\nOf\nThrones", 5)
returns {"", "Game", "Of", "Thrones", ""}
- VAlignBottom.ApplyStr("Game\nOf\nThrones", 5)
returns {"", "", "Game", "Of", "Thrones"}
▾ Example
Code:
str := "Game\nOf\nThrones"
maxLines := 5
fmt.Printf("VAlignDefault: %#v\n", VAlignDefault.ApplyStr(str, maxLines))
fmt.Printf("VAlignTop : %#v\n", VAlignTop.ApplyStr(str, maxLines))
fmt.Printf("VAlignMiddle : %#v\n", VAlignMiddle.ApplyStr(str, maxLines))
fmt.Printf("VAlignBottom : %#v\n", VAlignBottom.ApplyStr(str, maxLines))
Output:
VAlignDefault: []string{"Game", "Of", "Thrones", "", ""}
VAlignTop : []string{"Game", "Of", "Thrones", "", ""}
VAlignMiddle : []string{"", "Game", "Of", "Thrones", ""}
VAlignBottom : []string{"", "", "Game", "Of", "Thrones"}
func (va VAlign) HTMLProperty() string
HTMLProperty returns the equivalent HTML vertical-align tag property.
▾ Example
Code:
fmt.Printf("VAlignDefault: '%s'\n", VAlignDefault.HTMLProperty())
fmt.Printf("VAlignTop : '%s'\n", VAlignTop.HTMLProperty())
fmt.Printf("VAlignMiddle : '%s'\n", VAlignMiddle.HTMLProperty())
fmt.Printf("VAlignBottom : '%s'\n", VAlignBottom.HTMLProperty())
Output:
VAlignDefault: ''
VAlignTop : 'valign="top"'
VAlignMiddle : 'valign="middle"'
VAlignBottom : 'valign="bottom"'