core/echo/echo.go

117 lines
2.3 KiB
Go
Raw Normal View History

2023-11-04 10:30:17 +00:00
package echo
import (
"fmt"
2023-11-08 23:02:27 +00:00
"os"
2023-11-04 10:30:17 +00:00
"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"
2023-11-17 23:20:41 +00:00
var SYMB_TITLE_SEP = " :: "
var SYMB_ACTION = " * "
2023-11-04 10:30:17 +00:00
func Cstyle(name string) *style.Style {
if !hasCurStyle {
panic(msgUninit)
2023-11-04 10:30:17 +00:00
}
return curStyles.Get(name)
}
func Ln(a ...any) {
Cstyle("").Ln()
}
func SetCurrentStyles(styles *style.Styles) {
curStyles = styles
hasCurStyle = true
}
2023-11-25 14:15:44 +00:00
func InitDefaultStyles() *style.Styles {
styles := style.NewStyles()
styles.LoadDefaultStyle()
SetCurrentStyles(styles)
return styles
}
2023-11-04 10:30:17 +00:00
func Title(title string, subtitle string) {
Cstyle("").Echof("\n ")
2023-11-17 23:20:41 +00:00
Cstyle("appSep").Echof(SYMB_TITLE_SEP)
2023-11-04 10:30:17 +00:00
Cstyle("appTitle").Echof(title)
if subtitle != "" {
Cstyle("appSubTitle").Echof(" %s", subtitle)
}
2023-11-17 23:20:41 +00:00
Cstyle("appSep").Echof(SYMB_TITLE_SEP)
2023-11-04 10:30:17 +00:00
Ln()
}
func Action(action string, param string) {
Ln()
2023-11-17 23:20:41 +00:00
Cstyle("actionSep").Echo(SYMB_ACTION)
2023-11-04 10:30:17 +00:00
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...)
}
2023-11-04 10:30:17 +00:00
func Msg(msg string) {
fmt.Printf("%s%s\n", strings.Repeat(" ", 4), msg)
}
func LineUp(count int) {
fmt.Printf("\033[%dA", count)
}
2023-11-08 23:02:27 +00:00
func Error(msg string, exit ...bool) {
fmt.Printf(" ")
Cstyle("ko").Echof(" %s ", msg)
Ln()
Ln()
if len(exit) > 0 && exit[0] {
os.Exit(1)
}
}
2023-11-04 10:30:17 +00:00
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)
}