github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/colorprint/color.go (about) 1 package colorprint 2 3 import ( 4 "fmt" 5 ) 6 7 // The old escape sequence was FUBAR 8 const escapeSeq = "\x1b[%vm%s\x1b[0m" 9 10 // Text Attributes 11 const ( 12 BOLD = iota + 1 13 DARK 14 ITALIC 15 UNDERLINE 16 BLINK 17 _ 18 REVERSE 19 CONCEALED 20 ) 21 22 // Foreground colors 23 const ( 24 BLACK = iota + 30 25 RED 26 GREEN 27 YELLOW 28 BLUE 29 MAGENTA 30 CYAN 31 WHITE 32 _ 33 DEFAULT 34 ) 35 36 // Background Colors 37 const ( 38 BG_GREY = iota + 40 39 BG_RED 40 BG_GREEN 41 BG_YELLOW 42 BG_BLUE 43 BG_MAGENTA 44 BG_CYAN 45 BG_WHITE 46 _ 47 BG_DEFAULT 48 ) 49 50 type escaper interface { 51 fmt.Formatter 52 fmt.Stringer 53 } 54 55 type Escape struct { 56 code int 57 escapeStr escaper 58 } 59 60 func newColor(code int, escapeOrStr interface{}) *Escape { 61 switch s := escapeOrStr.(type) { 62 case *Escape: 63 return &Escape{code, s} 64 case string: 65 return &Escape{code, rawString(s)} 66 } 67 68 panic(fmt.Sprintf("colorprint: bad value: %v", escapeOrStr)) 69 } 70 71 func (clr *Escape) Format(s fmt.State, c rune) { 72 fmt.Fprintf(s, clr.String()) 73 } 74 75 func (clr *Escape) String() string { 76 return fmt.Sprintf(escapeSeq, clr.code, fmt.Sprintf("%s", clr.escapeStr)) 77 } 78 79 type rawString string 80 81 func (rs rawString) Format(s fmt.State, c rune) { 82 fmt.Fprintf(s, rs.String()) 83 } 84 85 func (rs rawString) String() string { 86 return string(rs) 87 } 88 89 // Terminal color escape sequences 90 func Black(cs interface{}) *Escape { 91 return newColor(BLACK, cs) 92 } 93 94 func Red(cs interface{}) *Escape { 95 return newColor(RED, cs) 96 } 97 98 func Green(cs interface{}) *Escape { 99 return newColor(GREEN, cs) 100 } 101 102 func Yellow(cs interface{}) *Escape { 103 return newColor(YELLOW, cs) 104 } 105 106 func Blue(cs interface{}) *Escape { 107 return newColor(BLUE, cs) 108 } 109 110 func Magenta(cs interface{}) *Escape { 111 return newColor(MAGENTA, cs) 112 } 113 114 func Cyan(cs interface{}) *Escape { 115 return newColor(CYAN, cs) 116 } 117 118 func White(cs interface{}) *Escape { 119 return newColor(WHITE, cs) 120 } 121 122 func Default(cs interface{}) *Escape { 123 return newColor(DEFAULT, cs) 124 } 125 126 // Terminal background color escape sequences 127 func BgGrey(cs interface{}) *Escape { 128 return newColor(BG_GREY, cs) 129 } 130 131 func BgRed(cs interface{}) *Escape { 132 return newColor(BG_RED, cs) 133 } 134 135 func BgGreen(cs interface{}) *Escape { 136 return newColor(BG_GREEN, cs) 137 } 138 139 func BgYellow(cs interface{}) *Escape { 140 return newColor(BG_YELLOW, cs) 141 } 142 143 func BgBlue(cs interface{}) *Escape { 144 return newColor(BG_BLUE, cs) 145 } 146 147 func BgMagenta(cs interface{}) *Escape { 148 return newColor(BG_MAGENTA, cs) 149 } 150 151 func BgCyan(cs interface{}) *Escape { 152 return newColor(BG_CYAN, cs) 153 } 154 155 func BgWhite(cs interface{}) *Escape { 156 return newColor(BG_WHITE, cs) 157 } 158 159 func BgDefault(cs interface{}) *Escape { 160 return newColor(BG_DEFAULT, cs) 161 } 162 163 // Terminal attribute escape sequences 164 func Bold(cs interface{}) *Escape { 165 return newColor(BOLD, cs) 166 } 167 168 func Dark(cs interface{}) *Escape { 169 return newColor(DARK, cs) 170 } 171 172 func Italic(cs interface{}) *Escape { 173 return newColor(ITALIC, cs) 174 } 175 176 func Underline(cs interface{}) *Escape { 177 return newColor(UNDERLINE, cs) 178 } 179 180 func Blink(cs interface{}) *Escape { 181 return newColor(BLINK, cs) 182 } 183 184 func Reverse(cs interface{}) *Escape { 185 return newColor(REVERSE, cs) 186 } 187 188 func Concealed(cs interface{}) *Escape { 189 return newColor(CONCEALED, cs) 190 }