-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
709 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Buffer-like byte slice | ||
// Copyright (c) 2017 Fadhli Dzil Ikram | ||
|
||
package buffer | ||
|
||
// Buffer type wrap up byte slice built-in type | ||
type Buffer []byte | ||
|
||
// Reset buffer position to start | ||
func (b *Buffer) Reset() { | ||
*b = Buffer([]byte(*b)[:0]) | ||
} | ||
|
||
// Append byte slice to buffer | ||
func (b *Buffer) Append(data []byte) { | ||
*b = append(*b, data...) | ||
} | ||
|
||
// AppendByte to buffer | ||
func (b *Buffer) AppendByte(data byte) { | ||
*b = append(*b, data) | ||
} | ||
|
||
// AppendInt to buffer | ||
func (b *Buffer) AppendInt(val int, width int) { | ||
var repr [8]byte | ||
reprCount := len(repr) - 1 | ||
for val >= 10 || width > 1 { | ||
reminder := val / 10 | ||
repr[reprCount] = byte('0' + val - reminder*10) | ||
val = reminder | ||
reprCount-- | ||
width-- | ||
} | ||
repr[reprCount] = byte('0' + val) | ||
b.Append(repr[reprCount:]) | ||
} | ||
|
||
// Bytes return underlying slice data | ||
func (b Buffer) Bytes() []byte { | ||
return []byte(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Buffer-like byte slice | ||
// Copyright (c) 2017 Fadhli Dzil Ikram | ||
// | ||
// Test file for buffer | ||
|
||
package buffer | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestBufferAllocation(t *testing.T) { | ||
Convey("Given new unallocated buffer", t, func() { | ||
var buf Buffer | ||
|
||
Convey("When appended with data", func() { | ||
data := []byte("Hello") | ||
buf.Append(data) | ||
|
||
Convey("It should have same content as the original data", func() { | ||
So(buf.Bytes(), ShouldResemble, data) | ||
}) | ||
}) | ||
|
||
Convey("When appended with single byte", func() { | ||
data := byte('H') | ||
buf.AppendByte(data) | ||
|
||
Convey("It should have 1 byte length", func() { | ||
So(len(buf), ShouldEqual, 1) | ||
}) | ||
|
||
Convey("It should have same content", func() { | ||
So(buf.Bytes()[0], ShouldEqual, data) | ||
}) | ||
}) | ||
|
||
Convey("When appended with integer", func() { | ||
data := 12345 | ||
repr := []byte("012345") | ||
buf.AppendInt(data, len(repr)) | ||
|
||
Convey("Should have same content with the integer representation", func() { | ||
So(buf.Bytes(), ShouldResemble, repr) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func TestBufferReset(t *testing.T) { | ||
Convey("Given allocated buffer", t, func() { | ||
var buf Buffer | ||
data := []byte("Hello") | ||
replace := []byte("World") | ||
buf.Append(data) | ||
|
||
Convey("When buffer reset", func() { | ||
buf.Reset() | ||
|
||
Convey("It should have zero length", func() { | ||
So(len(buf), ShouldEqual, 0) | ||
}) | ||
}) | ||
|
||
Convey("When buffer reset and replaced with another append", func() { | ||
buf.Reset() | ||
buf.Append(replace) | ||
|
||
Convey("It should have same content with the replaced data", func() { | ||
So(buf.Bytes(), ShouldResemble, replace) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// The color engine for the go-log library | ||
// Copyright (c) 2017 Fadhli Dzil Ikram | ||
|
||
package colorful | ||
|
||
import "github.com/withmandala/go-log/buffer" | ||
|
||
// ColorBuffer add color option to buffer append | ||
type ColorBuffer struct { | ||
buffer.Buffer | ||
} | ||
|
||
// color pallete map | ||
var ( | ||
colorOff = []byte("\033[0m") | ||
colorRed = []byte("\033[0;31m") | ||
colorGreen = []byte("\033[0;32m") | ||
colorOrange = []byte("\033[0;33m") | ||
colorBlue = []byte("\033[0;34m") | ||
colorPurple = []byte("\033[0;35m") | ||
colorCyan = []byte("\033[0;36m") | ||
colorGray = []byte("\033[0;37m") | ||
) | ||
|
||
// Off apply no color to the data | ||
func (cb *ColorBuffer) Off() { | ||
cb.Append(colorOff) | ||
} | ||
|
||
// Red apply red color to the data | ||
func (cb *ColorBuffer) Red() { | ||
cb.Append(colorRed) | ||
} | ||
|
||
// Green apply green color to the data | ||
func (cb *ColorBuffer) Green() { | ||
cb.Append(colorGreen) | ||
} | ||
|
||
// Orange apply orange color to the data | ||
func (cb *ColorBuffer) Orange() { | ||
cb.Append(colorOrange) | ||
} | ||
|
||
// Blue apply blue color to the data | ||
func (cb *ColorBuffer) Blue() { | ||
cb.Append(colorBlue) | ||
} | ||
|
||
// Purple apply purple color to the data | ||
func (cb *ColorBuffer) Purple() { | ||
cb.Append(colorPurple) | ||
} | ||
|
||
// Cyan apply cyan color to the data | ||
func (cb *ColorBuffer) Cyan() { | ||
cb.Append(colorCyan) | ||
} | ||
|
||
// Gray apply gray color to the data | ||
func (cb *ColorBuffer) Gray() { | ||
cb.Append(colorGray) | ||
} | ||
|
||
// mixer mix the color on and off byte with the actual data | ||
func mixer(data []byte, color []byte) []byte { | ||
var result []byte | ||
return append(append(append(result, color...), data...), colorOff...) | ||
} | ||
|
||
// Red apply red color to the data | ||
func Red(data []byte) []byte { | ||
return mixer(data, colorRed) | ||
} | ||
|
||
// Green apply green color to the data | ||
func Green(data []byte) []byte { | ||
return mixer(data, colorGreen) | ||
} | ||
|
||
// Orange apply orange color to the data | ||
func Orange(data []byte) []byte { | ||
return mixer(data, colorOrange) | ||
} | ||
|
||
// Blue apply blue color to the data | ||
func Blue(data []byte) []byte { | ||
return mixer(data, colorBlue) | ||
} | ||
|
||
// Purple apply purple color to the data | ||
func Purple(data []byte) []byte { | ||
return mixer(data, colorPurple) | ||
} | ||
|
||
// Cyan apply cyan color to the data | ||
func Cyan(data []byte) []byte { | ||
return mixer(data, colorCyan) | ||
} | ||
|
||
// Gray apply gray color to the data | ||
func Gray(data []byte) []byte { | ||
return mixer(data, colorGray) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// The color engine for the go-log library | ||
// Copyright (c) 2017 Fadhli Dzil Ikram | ||
// | ||
// Test file | ||
|
||
package colorful | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
"github.com/withmandala/go-log/buffer" | ||
) | ||
|
||
func TestColorBuffer(t *testing.T) { | ||
Convey("Given empty color buffer and test data", t, func() { | ||
var cb ColorBuffer | ||
var result buffer.Buffer | ||
|
||
// Add color to the result buffer | ||
result.Append(colorRed) | ||
result.Append(colorGreen) | ||
result.Append(colorOrange) | ||
result.Append(colorBlue) | ||
result.Append(colorPurple) | ||
result.Append(colorCyan) | ||
result.Append(colorGray) | ||
result.Append(colorOff) | ||
|
||
Convey("When appended with color", func() { | ||
cb.Red() | ||
cb.Green() | ||
cb.Orange() | ||
cb.Blue() | ||
cb.Purple() | ||
cb.Cyan() | ||
cb.Gray() | ||
cb.Off() | ||
|
||
Convey("It should have same content with the test data", func() { | ||
So(result.Bytes(), ShouldResemble, cb.Bytes()) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func TestColorMixer(t *testing.T) { | ||
Convey("Given mixer test result data", t, func() { | ||
var ( | ||
data = []byte("Hello") | ||
resultRed buffer.Buffer | ||
resultGreen buffer.Buffer | ||
resultOrange buffer.Buffer | ||
resultBlue buffer.Buffer | ||
resultPurple buffer.Buffer | ||
resultCyan buffer.Buffer | ||
resultGray buffer.Buffer | ||
) | ||
|
||
// Add result to buffer | ||
resultRed.Append(colorRed) | ||
resultRed.Append(data) | ||
resultRed.Append(colorOff) | ||
|
||
resultGreen.Append(colorGreen) | ||
resultGreen.Append(data) | ||
resultGreen.Append(colorOff) | ||
|
||
resultOrange.Append(colorOrange) | ||
resultOrange.Append(data) | ||
resultOrange.Append(colorOff) | ||
|
||
resultBlue.Append(colorBlue) | ||
resultBlue.Append(data) | ||
resultBlue.Append(colorOff) | ||
|
||
resultPurple.Append(colorPurple) | ||
resultPurple.Append(data) | ||
resultPurple.Append(colorOff) | ||
|
||
resultCyan.Append(colorCyan) | ||
resultCyan.Append(data) | ||
resultCyan.Append(colorOff) | ||
|
||
resultGray.Append(colorGray) | ||
resultGray.Append(data) | ||
resultGray.Append(colorOff) | ||
|
||
Convey("It should have same result when data appended with Red color", func() { | ||
So(Red(data), ShouldResemble, resultRed.Bytes()) | ||
}) | ||
|
||
Convey("It should have same result when data appended with Green color", func() { | ||
So(Green(data), ShouldResemble, resultGreen.Bytes()) | ||
}) | ||
|
||
Convey("It should have same result when data appended with Orange color", func() { | ||
So(Orange(data), ShouldResemble, resultOrange.Bytes()) | ||
}) | ||
|
||
Convey("It should have same result when data appended with Blue color", func() { | ||
So(Blue(data), ShouldResemble, resultBlue.Bytes()) | ||
}) | ||
|
||
Convey("It should have same result when data appended with Purple color", func() { | ||
So(Purple(data), ShouldResemble, resultPurple.Bytes()) | ||
}) | ||
|
||
Convey("It should have same result when data appended with Cyan color", func() { | ||
So(Cyan(data), ShouldResemble, resultCyan.Bytes()) | ||
}) | ||
|
||
Convey("It should have same result when data appended with Gray color", func() { | ||
So(Gray(data), ShouldResemble, resultGray.Bytes()) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.