diff --git a/cmd/app/main.go b/cmd/app/main.go index 463a8fa..c49e729 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -16,17 +16,27 @@ var oldStateTerminal *term.State func getColor(color string) prompt.Color { color = strings.ToUpper(color) + switch color { - case "CYAN": - return prompt.Cyan - case "RED": - return prompt.Red - default: - return prompt.Green + case "CYAN": + return prompt.Cyan + case "RED": + return prompt.Red + case "FUCHSIA": + return prompt.Fuchsia + case "TURQUOISE": + return prompt.Turquoise + case "GREEN": + return prompt.Green + case "PURPLE": + return prompt.Purple + default: + return prompt.Green } } func main() { + fd := int(os.Stdin.Fd()) if state, err := term.GetState(fd); err == nil { oldStateTerminal = state diff --git a/tcolor/tcolor.go b/tcolor/tcolor.go new file mode 100644 index 0000000..2bb1005 --- /dev/null +++ b/tcolor/tcolor.go @@ -0,0 +1,39 @@ +// Package tcolor +package tcolor + +import ( + "strconv" + "regexp" + "fmt" +) + +type TColor struct{ + colorRe *regexp.Regexp +} + + +func (t *TColor) Init(){ + colorRe,colorReErr := regexp.Compile(`#[0-9a-fA-F]{6}`) + if colorReErr != nil{ + fmt.Errorf("Regexp color err: %s",colorReErr.Error()) + }else{ + t.colorRe = colorRe + } +} + +func (t *TColor) HexToANSI(hex string) string { + if t.colorRe.MatchString(hex) { + hex = hex[1:] + } + if len(hex) != 6 { + return "" // Возвращаем пустую строку при ошибке + } + r, _ := strconv.ParseUint(hex[0:2], 16, 8) + g, _ := strconv.ParseUint(hex[2:4], 16, 8) + b, _ := strconv.ParseUint(hex[4:6], 16, 8) + + // Формат TrueColor ESC[38;2;R;G;Bm + return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b) +} + +