A dependency free package to help with "styling" stdout
text with ANSI escape sequences.
go get github.com/leo-alvarenga/go-easy-style@v1
TextStyle
is a struct as (seen bellow) that acts as the "entrypoint" to use the package.
type TextStyle struct {
Text string
Background string
Format []string
ANSI string
}
func (t *TextStyle) New(txt, background string, styles ...string) {...}
func (t *TextStyle) Style(s string) string {...}
func (t *TextStyle) ShowWithStyle(s string){...}
It also exposes the three (3) methods:
New
: Initializes aTextStyle
instance using defined values. Every time you callNew
for the same instance, the style is overwritten. Invalid values are simply ignoredStyle
: Returns the styled version of thestring
passed as argument, ready to be printed tostdout
without interfering with the rest of the contentShowWithStyle
: A shorthand to style and print thestring
passed as argument tostdout
Other exposed functions:
func ShowAsError(title, msg string) {...}
ShowAsError
: A shorthand to style thestring
passed as argument with a black background, red text and bold letters and print it tostdout
package main
import (
"fmt"
"github.com/leo-alvarenga/go-easy-style"
)
func main() {
var num int
style := new(gostyle.TextStyle)
style.New("black", "white", "bold", "italic", "undescore")
style.ShowWithStyle("Hello, colorfull world!")
style.New("white", "blue", "blink")
println(style.Style("Type in a number:"))
fmt.Scanf("%d", &num)
if num < 0 {
gostyle.ShowAsError("Too low!", "The value you typed is negative")
}
}