package door import ( "fmt" "strings" ) // Convert an array of int to \x1b[1;2;3m for ANSI Color // output. // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit func Color(arg ...int) string { var result string = "\x1b[" for i := range arg { result += fmt.Sprintf("%d;", arg[i]) } result = result[:len(result)-1] result += "m" return result } // BUG(bugz) INVERSE does not work. // Convert a string like "BRIGHT WHITE ON BLUE" into the expected // ANSI Color codes. You can use just the first 3 letters. // Supports BRIGHT, BOLD, BLINK and ON (to set the background color). // BLACK, RED, GREEN, BROWN / YELLOW, BLUE, MAGENTA, CYAN, WHITE // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit func ColorText(color string) string { // split on spaces, uppercase, match first 3 letter var result []int var bg bool result = append(result, 0) parts := strings.Fields(strings.ToUpper(color)) for _, part := range parts { switch part { case "BLACK", "BLA": if bg { result = append(result, 40) } else { result = append(result, 30) } case "RED": if bg { result = append(result, 41) } else { result = append(result, 31) } case "GREEN", "GRE": if bg { result = append(result, 42) } else { result = append(result, 32) } case "BROWN", "BRO": if bg { result = append(result, 43) } else { result = append(result, 33) } case "YELLOW", "YEL": if bg { result = append(result, 43) } else { result = append(result, 33) } case "BLUE", "BLU": if bg { result = append(result, 44) } else { result = append(result, 34) } case "MAGENTA", "MAG": if bg { result = append(result, 45) } else { result = append(result, 35) } case "CYAN", "CYA": if bg { result = append(result, 46) } else { result = append(result, 36) } case "WHITE", "WHI": if bg { result = append(result, 47) } else { result = append(result, 37) } case "BOLD", "BOL", "BRIGHT", "BRI": result = append(result, 1) case "ON": bg = true case "BLINK", "BLI": result = append(result, 5) case "INVERT", "INVERSE", "INV": result = append(result, 7) default: fmt.Println("ColorText Unknown:", part) } } return Color(result...) }