An ANSI colour terminal package for Go
1//Package colourize implements simple ANSI colour codes to style terminal output text.
2package colourize
3
4import (
5 "bytes"
6 "fmt"
7 "strconv"
8)
9
10//Colour Styles
11const (
12 //Text Colour
13 Black = 30
14 Red = 31
15 Green = 32
16 Yellow = 33
17 Blue = 34
18 Magenta = 35
19 Cyan = 36
20 White = 37
21 Grey = 90
22
23 //Background Colour
24 Blackbg = 40
25 Redbg = 41
26 Greenbg = 42
27 Yellowbg = 43
28 Bluebg = 44
29 Magentabg = 45
30 Cyanbg = 46
31 Whitebg = 47
32
33 //Style
34 Bold = 1
35 Dim = 2
36 Italic = 3
37 Underline = 4
38 Blinkslow = 5
39 Blinkfast = 6
40 Inverse = 7
41 Hidden = 8
42 Strikeout = 9
43)
44
45/*Colourize is the function that provides colours for values passed to it.
46package main
47
48import(
49 c "tangled.org/treybastian.com/colourize"
50 "fmt"
51)
52
53func main() {
54 fmt.Println(c.Colourize("Hello", c.Cyan))
55}
56*/
57func Colourize(s interface{}, style ...int) string {
58 b := new(bytes.Buffer)
59 b.WriteString("\x1b[")
60 l := len(style)
61 for k, c := range style {
62 b.WriteString(strconv.Itoa(c))
63 if l > 1 && k < l-1 {
64 b.WriteString(";")
65 }
66 }
67 b.WriteString("m")
68
69 return fmt.Sprintf("%s%v\x1b[0m", b.String(), s)
70}