An ANSI colour terminal package for Go

Initial Commit

+71
colourize.go
···
···
+
// Package colourize implements simple ANSI colour codes to style termianl output text.
+
package colourize
+
+
import (
+
"bytes"
+
"fmt"
+
"strconv"
+
)
+
+
// Colour Styles
+
const (
+
// Text Colour
+
Black = 30
+
Red = 31
+
Green = 32
+
Yellow = 33
+
Blue = 34
+
Magenta = 35
+
Cyan = 36
+
White = 37
+
Grey = 90
+
+
// Background Colour
+
Blackbg = 40
+
Redbg = 41
+
Greenbg = 42
+
Yellowbg = 43
+
Bluebg = 44
+
Magentabg = 45
+
Cyanbg = 46
+
Whitebg = 47
+
+
// Style
+
Bold = 1
+
Dim = 2
+
Italic = 3
+
Underline = 4
+
Blinkslow = 5
+
Blinkfast = 6
+
Inverse = 7
+
Hidden = 8
+
Strikeout = 9
+
)
+
+
/* Colourize is the function that provides colours for values passed to it.
+
package main
+
+
import(
+
c "github.com/TreyBastian/colourize"
+
"fmt"
+
)
+
+
func main() {
+
fmt.Println(c.Colourize("Hello", c.Cyan))
+
}
+
*/
+
+
func Colourize(s interface{}, style ...int) string {
+
b := new(bytes.Buffer)
+
b.WriteString("\x1b[")
+
l := len(style)
+
for k, c := range style {
+
b.WriteString(strconv.Itoa(c))
+
if l > 1 && k < l-1 {
+
b.WriteString(";")
+
}
+
}
+
b.WriteString("m")
+
+
return fmt.Sprintf("%s%v\x1b[0m", b.String(), s)
+
}
+55
colourize_test.go
···
···
+
package colourize
+
+
import (
+
"fmt"
+
"testing"
+
)
+
+
func TestText(t *testing.T) {
+
fmt.Println("~~~* Testing Text Colour *~~~")
+
fmt.Println(Colourize("Black Text", Black))
+
fmt.Println(Colourize("Red Text", Red))
+
fmt.Println(Colourize("Green Text", Green))
+
fmt.Println(Colourize("Yellow Text", Yellow))
+
fmt.Println(Colourize("Blue Text", Blue))
+
fmt.Println(Colourize("Magenta Text", Magenta))
+
fmt.Println(Colourize("Cyan Text", Cyan))
+
fmt.Println(Colourize("White Text", White))
+
fmt.Println(Colourize("Grey Text", Grey))
+
}
+
+
func TestBackground(t *testing.T) {
+
fmt.Println("~~~* Testing Background Colour *~~~")
+
fmt.Println(Colourize("Black Background", Blackbg))
+
fmt.Println(Colourize("Red Background", Redbg))
+
fmt.Println(Colourize("Green Background", Greenbg))
+
fmt.Println(Colourize("Yellow Background", Yellowbg))
+
fmt.Println(Colourize("Blue Background", Bluebg))
+
fmt.Println(Colourize("Magenta Background", Magentabg))
+
fmt.Println(Colourize("Cyan Background", Cyanbg))
+
fmt.Println(Colourize("White Background", Whitebg))
+
}
+
+
func TestStyle(t *testing.T) {
+
fmt.Println("~~~* Testing Style *~~~")
+
fmt.Println(Colourize("Bold Test", Bold))
+
fmt.Println(Colourize("Dim Test", Dim))
+
fmt.Println(Colourize("Italic Test", Italic))
+
fmt.Println(Colourize("Underline Test", Underline))
+
fmt.Println(Colourize("Blinkslow Test", Blinkslow))
+
fmt.Println(Colourize("Blinkfast Test", Blinkfast))
+
fmt.Println(Colourize("Inverse Test", Inverse))
+
fmt.Println(Colourize("Hidden Test", Hidden))
+
fmt.Println(Colourize("Strikeout Test", Strikeout))
+
}
+
+
func TestFunctional(t *testing.T) {
+
fmt.Println("~~~* Testing Functional Uses *~~~")
+
fmt.Println(Colourize("Bold White and Green Background", Bold, White, Greenbg))
+
fmt.Println(Colourize("Dim White with Cyan Background and ", White, Cyanbg, Dim), "Testing Automatic Style Reset")
+
fmt.Println(Colourize("White background with Blue Text and", Whitebg, Blue), Colourize("Green Background and Yellow Text", Greenbg, Yellow))
+
testString := "with a Printf test"
+
fmt.Printf(Colourize("Blue Background and White Text %s", Bluebg, White), testString)
+
fmt.Printf("\n")
+
fmt.Println(Colourize(1234, Green), "Testing with Integer Input")
+
}