core/style/style.go

152 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-11-02 22:04:28 +00:00
package style
import (
"fmt"
2023-11-05 13:33:47 +00:00
"sort"
2023-11-02 22:04:28 +00:00
"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-05 13:04:23 +00:00
const COLOR_OFF = "\x1b[0m"
2023-11-02 22:04:28 +00:00
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-05 13:01:24 +00:00
s.color.Printf(format+COLOR_OFF, a...)
2023-11-02 22:04:28 +00:00
}
2023-11-04 10:30:17 +00:00
func (s *Style) Applyf(format string, a ...any) string {
2023-11-05 13:01:24 +00:00
return s.color.Sprintf(format+COLOR_OFF, a...)
2023-11-02 22:04:28 +00:00
}
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{}
2023-11-05 12:49:27 +00:00
var c []string = []string{"FFFFFF"}
2023-11-02 22:04:28 +00:00
d := strings.Fields(def)
2023-11-05 12:49:27 +00:00
if len(d) > KEY_STYLE_COLOR {
c = strings.Split(d[KEY_STYLE_COLOR], ",")
if len(d) > KEY_STYLE_OPTION {
o = strings.Split(d[KEY_STYLE_OPTION], ",")
}
2023-11-02 22:04:28 +00:00
}
2023-11-05 12:49:27 +00:00
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 17:31:36 +00:00
func (s *Styles) HasStyle(name string) bool {
var done bool = false
2023-11-04 17:44:35 +00:00
if s.List != nil {
_, done = s.List[name]
2023-11-04 17:31:36 +00:00
}
return done
}
2023-11-04 10:30:17 +00:00
func (s *Styles) Get(name string) *Style {
2023-11-04 17:31:36 +00:00
style := defStyle
if s.HasStyle(name) {
style = s.List[name]
2023-11-04 10:30:17 +00:00
}
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),
)
}
2023-11-05 12:55:56 +00:00
func (s *Styles) Render() {
2023-11-05 13:33:47 +00:00
keys := make([]string, 0, len(s.List))
for name := range s.List {
keys = append(keys, name)
}
sort.Strings(keys)
for _, name := range keys {
2023-11-05 13:35:19 +00:00
s.Get(name).Echof(" %s ", name)
2023-11-05 13:10:01 +00:00
defStyle.Ln()
2023-11-05 12:55:56 +00:00
}
}