Skip to content

Instantly share code, notes, and snippets.

@huytd
Created August 18, 2016 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/99d19788e0abc717b25aed897d358147 to your computer and use it in GitHub Desktop.
Save huytd/99d19788e0abc717b25aed897d358147 to your computer and use it in GitHub Desktop.
Color output for Go app
package colors
import (
"fmt"
"regexp"
)
var (
TERM_STYLE = []string{
// LIGHT
"\033[1m",
// UNDERLINE
"\033[4m",
// YELLOW
"\033[33m",
// RED
"\033[31m",
// GREEN
"\033[32m",
// CYAN
"\033[36m",
// BLUE
"\033[34m",
}
TERM_RESET = "\033[0m"
INPUT_TAGS = []string{
"light",
"u",
"yellow",
"red",
"green",
"cyan",
"blue",
}
)
func Apply(input string) string {
out := input
for i := 1; i < len(INPUT_TAGS); i++ {
re := regexp.MustCompile(fmt.Sprintf(`\<%s\>(.*)\<\/%s\>`, INPUT_TAGS[i], INPUT_TAGS[i]))
out = re.ReplaceAllString(out, TERM_STYLE[i]+"$1"+TERM_RESET)
}
return out
}
fmt.Println(colors.Apply("<red><u>Attention!:</u></red>\n<yellow>Thank you!</yellow>"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment