core/echo/echo.go
2023-11-09 00:02:27 +01:00

108 lines
2.1 KiB
Go

package echo
import (
"fmt"
"os"
"strings"
"gitea.meta-tech.academy/go/core/style"
"gitea.meta-tech.academy/go/core/sys"
)
var curStyles *style.Styles
var hasCurStyle bool = false
var msgUninit = "Missing current *style.Styles, you should call echo.SetCurrentStyles(styles *style.Styles) first"
func Cstyle(name string) *style.Style {
if !hasCurStyle {
panic(msgUninit)
}
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 Keyval(key string, val string, names ...string) {
if !hasCurStyle {
panic(msgUninit)
}
curStyles.Keyval(key, val, names...)
}
func Msg(msg string) {
fmt.Printf("%s%s\n", strings.Repeat(" ", 4), msg)
}
func LineUp(count int) {
fmt.Printf("\033[%dA", count)
}
func Error(msg string, exit ...bool) {
fmt.Printf(" ")
Cstyle("ko").Echof(" %s ", msg)
Ln()
Ln()
if len(exit) > 0 && exit[0] {
os.Exit(1)
}
}
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)
}