ui.go (2478B)
1 package ui 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "time" 8 9 "fes/modules/config" 10 11 "github.com/fatih/color" 12 ) 13 14 const ( 15 hintColor = 0xbda02a 16 ) 17 18 func formatTimestamp() string { 19 return time.Now().Format("02 Jan 2006 15:04") 20 } 21 22 func logMessage(prefix string, msg string, args ...any) { 23 formatted := fmt.Sprintf(msg, args...) 24 if prefix == "" { 25 fmt.Printf("%s * %s\n", formatTimestamp(), formatted) 26 } else { 27 fmt.Printf("%s * %s: %s\n", formatTimestamp(), prefix, formatted) 28 } 29 } 30 31 // Generic log 32 func Log(msg string, args ...any) { 33 logMessage("", msg, args...) 34 } 35 36 // OK message (green) 37 func OK(msg string, args ...any) { 38 formatted := fmt.Sprintf(msg, args...) 39 color.Green("%s * %s\n", formatTimestamp(), formatted) 40 } 41 42 // Warning message (magenta) 43 func WARN(msg string, args ...any) { 44 formatted := fmt.Sprintf(msg, args...) 45 color.Magenta("%s # %s\n", formatTimestamp(), formatted) 46 } 47 48 // Warning message (magenta) 49 func WARNING(msg string, args ...any) { 50 formatted := fmt.Sprintf(msg, args...) 51 color.Magenta("%s # WARNING %s\n", formatTimestamp(), formatted) 52 } 53 54 // Error message (red) 55 func ERROR(msg string, args ...any) { 56 formatted := fmt.Sprintf(msg, args...) 57 color.Red("%s ! %s\n", formatTimestamp(), formatted) 58 } 59 60 // Fatal message and panic 61 func FATAL(msg string, args ...any) { 62 formatted := fmt.Sprintf(msg, args...) 63 color.Red("%s % %s\n", formatTimestamp(), formatted) 64 panic(formatted) 65 } 66 67 // Hint message (custom color) 68 func Hint(msg string, args ...any) { 69 formatted := fmt.Sprintf(msg, args...) 70 color.RGB(func(hex int) (r, g, b int) { 71 r = (hex >> 16) & 0xFF 72 g = (hex >> 8) & 0xFF 73 b = hex & 0xFF 74 return 75 }(hintColor)).Printf("hint: %s\n", formatted) 76 } 77 78 // Path logging: prints route and status 79 func Path(path string, err error) { 80 path = strings.TrimPrefix(path, "/") 81 if path == "" { 82 path = "(null)" 83 } 84 85 if err == nil { 86 OK("Route: %s - ok", path) 87 } else if errors.Is(err, config.ErrRouteMiss) { 88 WARN("Route: %s - %s", path, config.ErrRouteMiss.Error()) 89 } else { 90 ERROR("Route: %s - bad", path) 91 } 92 } 93 94 // System warning with prefix 95 func Warning(msg string, err error) error { 96 WARN("%s: %v", msg, err) 97 return err 98 } 99 100 // System error with prefix 101 func Error(msg string, err error) error { 102 ERROR("%s: %v", msg, err) 103 return err 104 } 105 106 // Fatal system error 107 func Fatal(msg string, err error) error { 108 FATAL("%s: %v", msg, err) 109 return err 110 } 111 112 // Log on Verbose 113 func LogVerbose(msg string, args ...any) { 114 if *config.Verbose { 115 Log(msg, args...) 116 } 117 }