Skip to content

Commit

Permalink
Add functions to color text on a terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
moncho committed Sep 3, 2017
1 parent bc73104 commit e6184d9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions terminal/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package terminal

import "fmt"

//Yellow add ANSI escape codes to the given string so when printed on a terminal
//the text foreground is yellow.
func Yellow(s string) string {
return colorize(s, 220)
}

//Red add ANSI escape codes to the given string so when printed on a terminal
//the text foreground is red.
func Red(s string) string {
return colorize(s, 88)
}

//White add ANSI escape codes to the given string so when printed on a terminal
//the text foreground is white.
func White(s string) string {
return colorize(s, 244)
}

//colorize applies the given color to the given text using the "extended set foreground color" code
func colorize(s string, color uint8) string {
return fmt.Sprintf("\033[38;5;%03dm%s\033[0m", color, s)
}

0 comments on commit e6184d9

Please sign in to comment.