From 636dfd47b15ae118821fed2d97fd1b53f38adc75 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Mon, 17 Feb 2014 01:12:32 -0800 Subject: [PATCH] Initial commit, WIP --- README.md | 0 color.go | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ color_test.go | 31 ++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 README.md create mode 100644 color.go create mode 100644 color_test.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/color.go b/color.go new file mode 100644 index 0000000..d1a8e45 --- /dev/null +++ b/color.go @@ -0,0 +1,134 @@ +package color + +import ( + "strconv" + "strings" + + "fmt" +) + +type Color struct { + params []Parameter +} + +// Parameter defines a single SGR Code +type Parameter int + +const ( + FgBlack Parameter = iota + 30 + FgRed + FgGreen + FgYellow + FgBlue + FgMagent + FgCyan + FgWhite +) + +const ( + BgBlack Parameter = iota + 40 + BgRed + BgGreen + BgYellow + BgBlue + BgMagent + BgCyan + BgWhite +) + +const ( + Reset Parameter = iota + Bold + Faint + Italic + Underline + BlinkSlow + BlinkRapid + ReverseVideo + Concealed + CrossedOut +) + +var ( + Black = &Color{params: []Parameter{FgBlack}} + Red = &Color{params: []Parameter{FgRed}} + Green = &Color{params: []Parameter{FgGreen}} + Yellow = &Color{params: []Parameter{FgYellow}} + Blue = &Color{params: []Parameter{FgBlue}} + Magent = &Color{params: []Parameter{FgMagent}} + Cyan = &Color{params: []Parameter{FgCyan}} + White = &Color{params: []Parameter{FgWhite}} +) + +func New(value ...Parameter) *Color { + c := &Color{params: make([]Parameter, 0)} + c.Add(value...) + return c +} + +func (c *Color) Bold() *Color { + c.Add(Bold) + return c +} + +func (c *Color) Add(value ...Parameter) *Color { + c.params = append(c.params, value...) + return c +} + +func (c *Color) prepend(value Parameter) { + c.params = append(c.params, 0) + copy(c.params[1:], c.params[0:]) + c.params[0] = value +} + +// Printf formats according to a format specifier and writes to standard output. +// It returns the number of bytes written and any write error encountered. +func (c *Color) Printf(format string, a ...interface{}) (n int, err error) { + c.Set() + defer Unset() + + return fmt.Printf(format, a...) +} + +// Print formats using the default formats for its operands and writes to +// standard output. Spaces are added between operands when neither is a +// string. It returns the number of bytes written and any write error +// encountered. +func (c *Color) Print(a ...interface{}) (n int, err error) { + c.Set() + defer Unset() + + return fmt.Print(a...) +} + +// Println formats using the default formats for its operands and writes to +// standard output. Spaces are always added between operands and a newline is +// appended. It returns the number of bytes written and any write error +// encountered. +func (c *Color) Println(a ...interface{}) (n int, err error) { + c.Set() + defer Unset() + + return fmt.Println(a...) +} + +// sequence returns a formated SGR sequence to be plugged into a "\033[...m" +// an example output might be: "1;36" -> bold cyan +func (c *Color) sequence() string { + format := make([]string, len(c.params)) + for i, v := range c.params { + format[i] = strconv.Itoa(int(v)) + } + + return strings.Join(format, ";") +} + +// Set sets the SGR sequence. +func (c *Color) Set() { + fmt.Printf("\033[%sm", c.sequence()) +} + +func Unset() { + fmt.Print("\033[0m") +} diff --git a/color_test.go b/color_test.go new file mode 100644 index 0000000..c710c63 --- /dev/null +++ b/color_test.go @@ -0,0 +1,31 @@ +package color + +import ( + "bufio" + "fmt" + "os" + "time" + + "testing" +) + +func TestColor(t *testing.T) { + go func() { + scanner := bufio.NewScanner(os.Stdout) + for scanner.Scan() { + fmt.Println(scanner.Text()) // Println will add back the final '\n' + } + }() + + time.Sleep(time.Millisecond * 300) + + c := Cyan.Bold() + c.Println("ankara") + + // New(FgGreen, Bold).Begin() + // fmt.Println("san francisco") + // End() + + // Output: + // ankara +}