forked from mitchellh/colorstring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0 parents
commit 04b0119
Showing
4 changed files
with
189 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Mitchell Hashimoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,23 @@ | ||
# colorstring | ||
|
||
colorstring is a [Go](http://www.golang.org) library for outputting colored | ||
strings to a console using a simple inline syntax in your string to specify | ||
the color to print as. | ||
|
||
For example, the string `[blue]hello [red]world` would output the text | ||
"hello world" in two colors. The API of colorstring allows for easily disabling | ||
colors, adding aliases, etc. | ||
|
||
## Installation | ||
|
||
Standard `go get`: | ||
|
||
``` | ||
$ go get github.com/mitchellh/colorstring | ||
``` | ||
|
||
## Usage & Example | ||
|
||
For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/colorstring). | ||
|
||
The `Decode` function has examples associated with it there. |
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,87 @@ | ||
package colorstring | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// Color colorizes your strings using the default settings. | ||
// | ||
// If you want to customize, use the Colorize struct. | ||
func Color(v string) string { | ||
return def.Color(v) | ||
} | ||
|
||
// Colorize colorizes your strings, giving you the ability to customize | ||
// some of the colorization process. | ||
// | ||
// The options in Colorize can be set to customize colorization. If you're | ||
// only interested in the defaults, just use the top Color function directly, | ||
// which creates a default Colorize. | ||
type Colorize struct { | ||
// Colors maps a color string to the integer code for that color. | ||
Colors map[string]int | ||
|
||
// Reset, if true, will reset the color after each colorization by | ||
// adding a reset code at the end. | ||
Reset bool | ||
} | ||
|
||
func (c *Colorize) Color(v string) string { | ||
matches := parseRe.FindAllStringIndex(v, -1) | ||
if len(matches) == 0 { | ||
return v | ||
} | ||
|
||
result := new(bytes.Buffer) | ||
if matches[0][0] > 0 { | ||
if _, err := result.WriteString(v[:matches[0][0]]); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
colored := false | ||
var m []int | ||
for _, nm := range matches { | ||
if len(m) > 0 { | ||
result.WriteString(v[m[1]:nm[0]]) | ||
} | ||
m = nm | ||
|
||
var replace string | ||
if code, ok := c.Colors[v[m[0]+1:m[1]-1]]; ok { | ||
colored = true | ||
replace = fmt.Sprintf("\033[0;%dm", code) | ||
} else { | ||
replace = v[m[0]:m[1]] | ||
} | ||
|
||
result.WriteString(replace) | ||
} | ||
result.WriteString(v[m[1]:]) | ||
|
||
if colored && c.Reset { | ||
// Write the clear byte at the end | ||
result.WriteString("\033[0m") | ||
} | ||
|
||
return result.String() | ||
} | ||
|
||
// DefaultColors are the default colors used when colorizing. | ||
var DefaultColors map[string]int | ||
|
||
func init() { | ||
DefaultColors = map[string]int{ | ||
"blue": 34, | ||
} | ||
|
||
def = Colorize{ | ||
Colors: DefaultColors, | ||
Reset: true, | ||
} | ||
} | ||
|
||
var def Colorize | ||
var parseRe = regexp.MustCompile(`(?i)\[[a-z0-9_-]+\]`) |
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,58 @@ | ||
package colorstring | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestColor(t *testing.T) { | ||
cases := []struct { | ||
Input, Output string | ||
}{ | ||
{ | ||
Input: "foo", | ||
Output: "foo", | ||
}, | ||
|
||
{ | ||
Input: "[blue]foo", | ||
Output: "\033[0;34mfoo\033[0m", | ||
}, | ||
|
||
{ | ||
Input: "foo[blue]foo", | ||
Output: "foo\033[0;34mfoo\033[0m", | ||
}, | ||
|
||
{ | ||
Input: "foo[what]foo", | ||
Output: "foo[what]foo", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
actual := Color(tc.Input) | ||
if actual != tc.Output { | ||
t.Errorf( | ||
"Input: %#v\n\nOutput: %#v\n\nExpected: %#v", | ||
tc.Input, | ||
actual, | ||
tc.Output) | ||
} | ||
} | ||
} | ||
|
||
func TestColorizeColor_noReset(t *testing.T) { | ||
c := def | ||
c.Reset = false | ||
|
||
input := "[blue]foo" | ||
output := "\033[0;34mfoo" | ||
actual := c.Color(input) | ||
if actual != output { | ||
t.Errorf( | ||
"Input: %#v\n\nOutput: %#v\n\nExpected: %#v", | ||
input, | ||
actual, | ||
output) | ||
} | ||
} |