v0.0.1 - refact

This commit is contained in:
Mahdi Abu Yasmine 2023-11-04 11:30:17 +01:00
parent 5ce1b15734
commit a15a1b74a4
4 changed files with 131 additions and 47 deletions

2
build Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
go build ./...

88
echo/echo.go Normal file
View File

@ -0,0 +1,88 @@
package echo
import (
"fmt"
"strings"
"gitea.meta-tech.academy/go/core/style"
"gitea.meta-tech.academy/go/core/sys"
)
var curStyles *style.Styles
var hasCurStyle bool = false
func Cstyle(name string) *style.Style {
if !hasCurStyle {
panic("Missing current style, you should call style.SetCurrentStyles(styles *style.Styles) first\n")
}
return curStyles.Get(name)
}
func Ln(a ...any) {
Cstyle("").Ln()
}
func SetCurrentStyles(styles *style.Styles) {
curStyles = styles
hasCurStyle = true
}
func Title(title string, subtitle string) {
Cstyle("").Echof("\n ")
Cstyle("appSep").Echof(" :: ")
Cstyle("appTitle").Echof(title)
if subtitle != "" {
Cstyle("appSubTitle").Echof(" %s", subtitle)
}
Cstyle("appSep").Echof(" :: ")
Ln()
}
func Action(action string, param string) {
Ln()
Cstyle("actionSep").Echo(" * ")
Cstyle("action").Echof("%s ", action)
Cstyle("param").Echo(param)
Ln()
}
func State(done bool) {
c := Cstyle("ok")
label := " OK "
if !done {
c = Cstyle("ko")
label = " KO "
}
fmt.Print(strings.Repeat(" ", 2))
for i := 0; i < sys.TERM_WIDTH-20; i++ {
Cstyle("sep").Echo("-")
// time.Sleep(3 * time.Millisecond)
}
Cstyle("sep").Echo(" ")
c.Echo(label)
Ln()
}
func Msg(msg string) {
fmt.Printf("%s%s\n", strings.Repeat(" ", 4), msg)
}
func LineUp(count int) {
fmt.Printf("\033[%dA", count)
}
func GetArg(msg string) string {
return Cstyle("usageArg").Applyf(" % s", msg)
}
func GetArgOpt(msg string) string {
return Cstyle("usageSep").Apply("[") + Cstyle("usageArg").Apply(msg) + Cstyle("usageSep").Apply("] ")
}
func GetComment(msg string) string {
return Cstyle("usageCom").Apply(msg)
}
func GetApp(msg string, cmd string) string {
return Cstyle("usageApp").Applyf("%s ", msg) + Cstyle("usageCmd").Apply(cmd)
}

View File

@ -2,9 +2,10 @@ package style
import (
"fmt"
"strconv"
"strings"
"gitea.meta-tech.academy/go/core/util"
"github.com/gookit/color"
)
@ -12,6 +13,8 @@ const KEY_STYLE_NAME = 0
const KEY_STYLE_COLOR = 1
const KEY_STYLE_OPTION = 2
var defStyle *Style = &Style{color: color.HEXStyle("#ffffff"), Name: "default"}
type Style struct {
color *color.RGBStyle
Name string
@ -25,28 +28,24 @@ type Styles struct {
DefaultIndent string
}
func (s *Style) Printf(format string, a ...any) {
func (s *Style) Echof(format string, a ...any) {
s.color.Printf(format, a...)
}
func (s *Style) Sprintf(format string, a ...any) string {
func (s *Style) Applyf(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) Echo(data string) {
s.Echof("%s", 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 (s *Style) Apply(data string) string {
return s.Applyf("%s", data)
}
func (s *Style) Ln() {
s.Echo("\n")
}
func NewStyleByDef(def string) *Style {
@ -56,7 +55,7 @@ func NewStyleByDef(def string) *Style {
if len(d) > KEY_STYLE_OPTION {
o = strings.Split(d[KEY_STYLE_OPTION], ",")
}
prependToSliceStr(&c, "#")
util.PrependToSliceStr(&c, "#")
var s *color.RGBStyle
switch len(c) {
case 1:
@ -66,7 +65,7 @@ func NewStyleByDef(def string) *Style {
}
for _, elm := range o {
s.AddOpts(color.Color(str2int(elm, 10, 0)))
s.AddOpts(color.Color(util.Str2int(elm, 10, 0)))
}
// s.Printf(" %-20s\n", d[KEY_STYLE_NAME])
return &Style{s, d[KEY_STYLE_NAME]}
@ -83,46 +82,43 @@ func NewStyles() *Styles {
return l
}
func (s *Styles) Apply(name string, data string) string {
return s.List[name].Apply(data)
func (s *Styles) Get(name string) *Style {
style, ok := s.List[name]
if !ok {
style = defStyle
}
return style
}
func (s *Styles) Echo(name string, data string) {
s.List[name].Echo(data)
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...)
}
func (s *Styles) Keyval(key string, val string, names ...string) {
sk := s.List[s.DefaultKeyStyle]
sv := s.List[s.DefaultValStyle]
sk := s.Get(s.DefaultKeyStyle)
sv := s.Get(s.DefaultValStyle)
if len(names) > 1 {
sk = s.List[names[0]]
sk = s.Get(names[0])
if len(names) > 2 {
sv = s.List[names[1]]
sv = s.Get(names[1])
}
}
fmt.Printf(
"%-"+s.DefaultIndent+"s%s : %s\n",
" ",
sk.Sprintf("%-"+s.DefaultKeyPadding+"s", key),
sk.Applyf("%-"+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(str2int64(str, base, int64(fallback)))
}
func str2int64(str string, base int, fallback int64) int64 {
str = strings.TrimSuffix(str, "\n")
num, err := strconv.ParseInt(str, base, 64)
if err != nil {
num = fallback
}
return num
}

View File

@ -94,10 +94,8 @@ func UpdateTermSize() error {
return nil
}
func HandleTermChange() {
func HandleTermChange(sigwinch chan os.Signal) {
// set signal handler
sigwinch := make(chan os.Signal, 1)
defer close(sigwinch)
signal.Notify(sigwinch, syscall.SIGWINCH)
go func() {
for {