2023-11-02 22:04:28 +00:00
|
|
|
package style
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-11-03 02:10:55 +00:00
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
"gitea.meta-tech.academy/go/core/util"
|
|
|
|
|
2023-11-02 22:04:28 +00:00
|
|
|
"github.com/gookit/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
const KEY_STYLE_NAME = 0
|
|
|
|
const KEY_STYLE_COLOR = 1
|
|
|
|
const KEY_STYLE_OPTION = 2
|
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
var defStyle *Style = &Style{color: color.HEXStyle("#ffffff"), Name: "default"}
|
|
|
|
|
2023-11-02 22:04:28 +00:00
|
|
|
type Style struct {
|
|
|
|
color *color.RGBStyle
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Styles struct {
|
|
|
|
List map[string]*Style
|
|
|
|
DefaultKeyStyle string
|
|
|
|
DefaultValStyle string
|
|
|
|
DefaultKeyPadding string
|
|
|
|
DefaultIndent string
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
func (s *Style) Echof(format string, a ...any) {
|
2023-11-02 22:04:28 +00:00
|
|
|
s.color.Printf(format, a...)
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
func (s *Style) Applyf(format string, a ...any) string {
|
2023-11-02 22:04:28 +00:00
|
|
|
return s.color.Sprintf(format, a...)
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
func (s *Style) Echo(data string) {
|
|
|
|
s.Echof("%s", data)
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
func (s *Style) Apply(data string) string {
|
|
|
|
return s.Applyf("%s", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Style) Ln() {
|
|
|
|
s.Echo("\n")
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStyleByDef(def string) *Style {
|
|
|
|
var o []string = []string{}
|
|
|
|
d := strings.Fields(def)
|
|
|
|
c := strings.Split(d[KEY_STYLE_COLOR], ",")
|
|
|
|
if len(d) > KEY_STYLE_OPTION {
|
|
|
|
o = strings.Split(d[KEY_STYLE_OPTION], ",")
|
|
|
|
}
|
2023-11-04 10:30:17 +00:00
|
|
|
util.PrependToSliceStr(&c, "#")
|
2023-11-02 22:04:28 +00:00
|
|
|
var s *color.RGBStyle
|
|
|
|
switch len(c) {
|
|
|
|
case 1:
|
|
|
|
s = color.HEXStyle(c[0])
|
|
|
|
case 2:
|
|
|
|
s = color.HEXStyle(c[0], c[1])
|
|
|
|
}
|
|
|
|
for _, elm := range o {
|
2023-11-02 23:01:57 +00:00
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
s.AddOpts(color.Color(util.Str2int(elm, 10, 0)))
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
// s.Printf(" %-20s\n", d[KEY_STYLE_NAME])
|
|
|
|
return &Style{s, d[KEY_STYLE_NAME]}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStyles() *Styles {
|
|
|
|
l := &Styles{
|
|
|
|
List: make(map[string]*Style),
|
|
|
|
DefaultKeyStyle: "key",
|
|
|
|
DefaultValStyle: "val",
|
|
|
|
DefaultKeyPadding: "18",
|
|
|
|
DefaultIndent: "4",
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:30:17 +00:00
|
|
|
func (s *Styles) Get(name string) *Style {
|
|
|
|
style, ok := s.List[name]
|
|
|
|
if !ok {
|
|
|
|
style = defStyle
|
|
|
|
}
|
|
|
|
return style
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Styles) Echo(name string, data string) {
|
2023-11-04 10:30:17 +00:00
|
|
|
s.Get(name).Echof("%s", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Styles) Apply(name string, data string) string {
|
|
|
|
return s.List[name].Applyf("%s", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Styles) Echof(name string, format string, a ...any) {
|
|
|
|
s.Get(name).Echof(format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Styles) Applyf(name string, format string, a ...any) string {
|
|
|
|
return s.Get(name).Applyf(format, a...)
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Styles) Keyval(key string, val string, names ...string) {
|
2023-11-04 10:30:17 +00:00
|
|
|
sk := s.Get(s.DefaultKeyStyle)
|
|
|
|
sv := s.Get(s.DefaultValStyle)
|
2023-11-02 22:04:28 +00:00
|
|
|
if len(names) > 1 {
|
2023-11-04 10:30:17 +00:00
|
|
|
sk = s.Get(names[0])
|
2023-11-02 22:04:28 +00:00
|
|
|
if len(names) > 2 {
|
2023-11-04 10:30:17 +00:00
|
|
|
sv = s.Get(names[1])
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf(
|
|
|
|
"%-"+s.DefaultIndent+"s%s : %s\n",
|
|
|
|
" ",
|
2023-11-04 10:30:17 +00:00
|
|
|
sk.Applyf("%-"+s.DefaultKeyPadding+"s", key),
|
2023-11-02 22:04:28 +00:00
|
|
|
sv.Apply(val),
|
|
|
|
)
|
|
|
|
}
|