Files
fes/modules/ui/ui.go
2025-12-31 12:16:51 -05:00

79 lines
1.5 KiB
Go

package ui
import (
"errors"
"fmt"
"strings"
"fes/modules/config"
"fes/modules/version"
"github.com/fatih/color"
)
const (
hint_color = 0xbda02a
)
/* print out the current path (route) and relevant error */
func Path(path string, err error) {
path = strings.TrimPrefix(path, "/")
if path == "" {
path = "(null)"
}
fmt.Printf(" > %s ", path)
if err == nil {
OK("ok")
return
} else if errors.Is(err, config.ErrRouteMiss) {
WARN(config.ErrRouteMiss.Error())
} else {
ERROR("bad")
}
}
/* print general system warning */
func Warning(msg string, err error) error {
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.MagentaString("warning"), err)
return err
}
/* print general system error */
func Error(msg string, err error) error {
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("error"), err)
return err
}
/* print fatality and panic */
func Fatal(msg string, err error) error {
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("fatal"), err)
panic(err)
}
/* print a useful hint to the user */
func Hint(format string, args ...any) {
color.RGB(func(hex int) (r, g, b int) {
r = (hex >> 16) & 0xFF
g = (hex >> 8) & 0xFF
b = hex & 0xFF
return
}(hint_color)).Printf("hint: %s\n", fmt.Sprintf(format, args...))
}
/* print message using the ok status color */
func OK(msg string) {
color.Green(msg)
}
/* print message using the warning status color */
func WARN(msg string) {
color.Magenta(msg)
}
/* print message using the error status color */
func ERROR(msg string) {
color.Red(msg)
}