aboutsummaryrefslogtreecommitdiff
path: root/colourize.go
diff options
context:
space:
mode:
authorTreyBastian <trey.bastian@gmail.com>2015-05-11 12:54:14 +0100
committerTreyBastian <trey.bastian@gmail.com>2015-05-11 12:54:14 +0100
commite5d4c4e942f0131b01a43069d0e4d24132b99ad4 (patch)
tree411a1f28eca3463078850e80f005bb8baaf751a5 /colourize.go
parente5aea0adc9047b8e01c2045d122d65f5fcc81f68 (diff)
Initial Commit
Diffstat (limited to 'colourize.go')
-rw-r--r--colourize.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/colourize.go b/colourize.go
new file mode 100644
index 0000000..5f6c03d
--- /dev/null
+++ b/colourize.go
@@ -0,0 +1,71 @@
+// 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)
+}