Created
August 18, 2016 18:16
-
-
Save huytd/99d19788e0abc717b25aed897d358147 to your computer and use it in GitHub Desktop.
Color output for Go app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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