package style import ( "fmt" "strings" "gitea.meta-tech.academy/go/core/util" "github.com/gookit/color" ) // Hello returns a greeting for the named person. func Hello(name string) string { // Return a greeting that embeds the name in a message. message := fmt.Sprintf("Hi, %v. Welcome!", name) return message } const KEY_STYLE_NAME = 0 const KEY_STYLE_COLOR = 1 const KEY_STYLE_OPTION = 2 type Style struct { color *color.RGBStyle Name string } type Styles struct { List map[string]*Style DefaultKeyStyle string DefaultValStyle string DefaultKeyPadding string DefaultIndent string } func (s *Style) Printf(format string, a ...any) { s.color.Printf(format, a...) } func (s *Style) Sprintf(format string, a ...any) string { return s.color.Sprintf(format, a...) } func (s *Style) Echo(data string, format ...string) { fmt := "%s" if len(format) > 1 { fmt = format[0] } s.Printf(fmt, data) } func (s *Style) Apply(data string, format ...string) string { if len(format) > 1 { return s.Sprintf(format[0], data) } else { return s.Sprintf(data) } } 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], ",") } prependToSliceStr(&c, "#") 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 { s.AddOpts(color.Color(str2int(elm, 10, 0))) } // 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 } func (s *Styles) Apply(name string, data string) string { return s.List[name].Apply(data) } func (s *Styles) Echo(name string, data string) { s.List[name].Echo(data) } func (s *Styles) Keyval(key string, val string, names ...string) { sk := s.List[s.DefaultKeyStyle] sv := s.List[s.DefaultValStyle] if len(names) > 1 { sk = s.List[names[0]] if len(names) > 2 { sv = s.List[names[1]] } } fmt.Printf( "%-"+s.DefaultIndent+"s%s : %s\n", " ", sk.Sprintf("%-"+s.DefaultKeyPadding+"s", key), sv.Apply(val), ) } func prependToSliceStr(strs *[]string, prefix string) { for i, elm := range *strs { (*strs)[i] = prefix + elm } } func str2int(str string, base int, fallback int) int { return int(util.Str2int64(str, base, int64(fallback))) }