first commit

This commit is contained in:
2020-10-25 10:00:59 +03:00
commit 0bd6f67397
80 changed files with 4741 additions and 0 deletions

51
output/style/color.go Normal file
View File

@@ -0,0 +1,51 @@
package style
const (
Black Color = "0"
Red Color = "1"
Green Color = "2"
Yellow Color = "3"
Blue Color = "4"
Magenta Color = "5"
Cyan Color = "6"
White Color = "7"
Default Color = "9"
)
const (
Bold Option = "122"
Underscore Option = "424"
Blink Option = "525"
Reverse Option = "727"
Conseal Option = "828"
)
const (
ActionSet = 1
ActionUnset = 2
)
type Option string
func (o Option) Apply(action int) string {
v := string(o)
switch action {
case ActionSet:
return v[0:1]
case ActionUnset:
return v[1:]
}
return ""
}
type Color string
func (c Color) Apply(action int) string {
if action == ActionSet {
return string(c)
}
return string(Default)
}

88
output/style/style.go Normal file
View File

@@ -0,0 +1,88 @@
package style
import (
"errors"
"fmt"
"strings"
"sync"
)
//nolint: gochecknoglobals
var (
styles = map[string]Style{
"error": {Foreground: White, Background: Red},
"info": {Foreground: Green},
"comment": {Foreground: Yellow},
"question": {Foreground: Black, Background: Cyan},
}
stylesMu sync.Mutex
empty = Style{}
)
var (
ErrNotFound = errors.New("console: style not found")
ErrDuplicateStyle = errors.New("console: Register called twice")
)
func Empty() Style {
return empty
}
func Find(name string) (Style, error) {
if st, has := styles[name]; has {
return st, nil
}
return empty, ErrNotFound
}
func Register(name string, style Style) error {
stylesMu.Lock()
defer stylesMu.Unlock()
if _, has := styles[name]; has {
return fmt.Errorf("%w for style %s", ErrDuplicateStyle, name)
}
styles[name] = style
return nil
}
func MustRegister(name string, style Style) {
if err := Register(name, style); err != nil {
panic(err)
}
}
type Style struct {
Background Color
Foreground Color
Options []Option
}
func (s Style) Apply(msg string) string {
return s.Set(ActionSet) + msg + s.Set(ActionUnset)
}
func (s Style) Set(action int) string {
style := make([]string, 0, len(s.Options))
if s.Foreground != "" {
style = append(style, "3"+s.Foreground.Apply(action))
}
if s.Background != "" {
style = append(style, "4"+s.Background.Apply(action))
}
for _, opt := range s.Options {
style = append(style, opt.Apply(action))
}
if len(style) == 0 {
return ""
}
return "\033[" + strings.Join(style, ";") + "m"
}