123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package door
- import (
- "bytes"
- "log"
- "strconv"
- "strings"
- )
- // Profiling:
- // go build -gcflags=-m=2 . 2>&1 | less
- // What leaks to heap?
- // Possibly make this so it is part of an ANSI package.
- // DRY - Reuse.
- // 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
- }
- */
- func Color(arg []int) []byte {
- var result *bytes.Buffer = &bytes.Buffer{}
- result.WriteString("\x1b[")
- for idx, i := range arg {
- if idx != 0 {
- result.WriteString(";")
- }
- result.WriteString(strconv.Itoa(i))
- }
- result.WriteString("m")
- return result.Bytes()
- }
- func ColorS(arg []int) string {
- var result strings.Builder
- result.WriteString("\x1b[")
- for idx, i := range arg {
- if idx != 0 {
- result.WriteString(";")
- }
- result.WriteString(strconv.Itoa(i))
- }
- result.WriteString("m")
- return result.String()
- }
- // BUG(bugz) INVERSE does not work.
- // Would I want to do something like:
- // BRIGHT YELLOW or ON BLUE, and have it just change the
- // color or the background? Right now, I require the full
- // color (FG & BG).
- // 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) []byte {
- // split on spaces, uppercase, match first 3 letter
- var res [5]int
- var result []int = res[0:0]
- var bg bool
- result = append(result, 0)
- parts := strings.Fields(strings.ToUpper(color))
- for _, part := range parts {
- if len(part) > 3 {
- part = part[:3]
- }
- switch part {
- case "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 "GRE":
- if bg {
- result = append(result, 42)
- } else {
- result = append(result, 32)
- }
- case "BRO", "YEL":
- if bg {
- result = append(result, 43)
- } else {
- result = append(result, 33)
- }
- case "BLU":
- if bg {
- result = append(result, 44)
- } else {
- result = append(result, 34)
- }
- case "MAG":
- if bg {
- result = append(result, 45)
- } else {
- result = append(result, 35)
- }
- case "CYA":
- if bg {
- result = append(result, 46)
- } else {
- result = append(result, 36)
- }
- case "WHI":
- if bg {
- result = append(result, 47)
- } else {
- result = append(result, 37)
- }
- case "BOL", "BRI":
- result = append(result, 1)
- case "ON":
- bg = true
- case "BLI":
- result = append(result, 5)
- // Invert is't well supported in terminals
- // case "INV":
- // result = append(result, 7)
- default:
- log.Panicf("ColorText Unknown: [%s] in %s\n", part, color)
- }
- }
- return Color(result)
- }
- func ColorTextS(color string) string {
- // split on spaces, uppercase, match first 3 letter
- var res [5]int
- var result []int = res[0:0]
- var bg bool
- result = append(result, 0)
- parts := strings.Fields(strings.ToUpper(color))
- for _, part := range parts {
- if len(part) > 3 {
- part = part[:3]
- }
- switch part {
- case "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 "GRE":
- if bg {
- result = append(result, 42)
- } else {
- result = append(result, 32)
- }
- case "BRO", "YEL":
- if bg {
- result = append(result, 43)
- } else {
- result = append(result, 33)
- }
- case "BLU":
- if bg {
- result = append(result, 44)
- } else {
- result = append(result, 34)
- }
- case "MAG":
- if bg {
- result = append(result, 45)
- } else {
- result = append(result, 35)
- }
- case "CYA":
- if bg {
- result = append(result, 46)
- } else {
- result = append(result, 36)
- }
- case "WHI":
- if bg {
- result = append(result, 47)
- } else {
- result = append(result, 37)
- }
- case "BOL", "BRI":
- result = append(result, 1)
- case "ON":
- bg = true
- case "BLI":
- result = append(result, 5)
- // Invert is't well supported in terminals
- // case "INV":
- // result = append(result, 7)
- default:
- log.Panicf("ColorText Unknown: [%s] in %s\n", part, color)
- }
- }
- return ColorS(result)
- }
|