package ccolor
import (
"fmt"
"io"
"strings"
)
type String string
type Style struct {
Fg Color
Bg Color
Opts []Color
}
func NewStyle (fg Color , bg Color , opts ...Color ) *Style {
return &Style {
Fg : fg ,
Bg : bg ,
Opts : opts ,
}
}
func (s *Style ) Print (v ...any ) {
doPrint (s .String (), fmt .Sprint (v ...))
}
func (s *Style ) Println (v ...any ) {
doPrintln (s .String (), v )
}
func (s *Style ) Printf (format string , v ...any ) {
doPrint (s .String (), fmt .Sprintf (format , v ...))
}
func (s *Style ) Sprint (v ...any ) string {
return RenderString (s .String (), fmt .Sprint (v ...))
}
func (s *Style ) Sprintln (v ...any ) string {
return RenderWithSpaces (s .String (), v ...)
}
func (s *Style ) Sprintf (format string , v ...any ) string {
return RenderString (s .String (), fmt .Sprintf (format , v ...))
}
func (s *Style ) Fprint (w io .Writer , v ...any ) {
doPrintTo (w , s .String (), fmt .Sprint (v ...))
}
func (s *Style ) Fprintf (w io .Writer , format string , v ...any ) {
doPrintTo (w , s .String (), fmt .Sprintf (format , v ...))
}
func (s *Style ) Fprintln (w io .Writer , v ...any ) {
doPrintlnTo (w , s .String (), v )
}
func (s *Style ) String () string {
var codes []string
if s .Fg .IsFg () {
codes = append (codes , s .Fg .String ())
}
if s .Bg .IsBg () {
codes = append (codes , s .Bg .String ())
}
if len (s .Opts ) > 0 {
codes = append (codes , ColorsToCode (s .Opts ...))
}
if len (codes ) == 0 {
return ""
}
return strings .Join (codes , ";" )
}
var (
Info = &Style {Fg : FgGreen }
Warn = &Style {Fg : FgYellow }
Error = NewStyle (FgLightWhite , BgRed )
Debug = &Style {Fg : FgCyan }
Success = &Style {Fg : FgGreen , Opts : []Color {OpBold }}
)
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .