minicolor formater with hex
This commit is contained in:
@@ -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
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user