Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhevron committed Dec 3, 2015
0 parents commit 938d157
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Thomas Lokshall

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.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ansi - ANSI escape sequence library
===================================

[![wercker status](https://app.wercker.com/status/387e7da9a8e93d6b23892db0ea3ecf51/s/master "wercker status")](https://app.wercker.com/project/bykey/387e7da9a8e93d6b23892db0ea3ecf51)
[![Coverage Status](https://coveralls.io/repos/zhevron/ansi/badge.svg?branch=master)](https://coveralls.io/r/zhevron/ansi?branch=master)
[![GoDoc](https://godoc.org/github.com/zhevron/ansi?status.svg)](https://godoc.org/github.com/zhevron/ansi)

**ansi** is an ANSI escape sequence library for [Google Go](https://golang.org).

## Colored strings

If the output supported ANSI escape codes, you can print colored strings using
the utility functions in this library.

```go
fmt.Println(ansi.Red("This string will be red", None))
```

You can also apply modifiers to the output strings to make the text bold, italic
or underlined.

```go
fmt.Println(ansi.Red("This string will be bold red", Bold))
fmt.Println(ansi.Red("This string will be bold and underlined red", Bold|Underline))
```

## License

**ansi** is licensed under the [MIT license](http://opensource.org/licenses/MIT).
117 changes: 117 additions & 0 deletions ansi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package ansi

const (
csi = "\033["
reset = "0"
bold = ";1"
italic = ";3"
underline = ";4"
blink = ";5"
foreground = "3"
foregroundLight = "9"
background = "4"
backgroundLight = "10"
black = "0"
red = "1"
green = "2"
yellow = "3"
blue = "4"
magenta = "5"
cyan = "6"
white = "7"
def = "9"
sgr = "m"
)

type modifier uint

// Text modifiers.
const (
None = 0
Light modifier = 1 << iota
Bold
Italic
Underline
Blink
)

var enabled = true

// Enable enables the use of ANSI escape sequences in calls to ansi functions.
func Enable() {
enabled = true
}

// Disable disabled the use of ANSI escape sequences in calls to ansi functions.
// In short, no string manipulations will be done by the ansi functions.
func Disable() {
enabled = false
}

// Black returns a string enclosed in the ANSI escape code for black text.
func Black(str string, mod modifier) string {
return colorString(str, black, mod)
}

// Red returns a string enclosed in the ANSI escape code for red text.
func Red(str string, mod modifier) string {
return colorString(str, red, mod)
}

// Green returns a string enclosed in the ANSI escape code for green text.
func Green(str string, mod modifier) string {
return colorString(str, green, mod)
}

// Yellow returns a string enclosed in the ANSI escape code for yellow text.
func Yellow(str string, mod modifier) string {
return colorString(str, yellow, mod)
}

// Blue returns a string enclosed in the ANSI escape code for blue text.
func Blue(str string, mod modifier) string {
return colorString(str, blue, mod)
}

// Magenta returns a string enclosed in the ANSI escape code for magenta text.
func Magenta(str string, mod modifier) string {
return colorString(str, magenta, mod)
}

// Cyan returns a string enclosed in the ANSI escape code for cyan text.
func Cyan(str string, mod modifier) string {
return colorString(str, cyan, mod)
}

// White returns a string enclosed in the ANSI escape code for white text.
func White(str string, mod modifier) string {
return colorString(str, white, mod)
}

func colorString(str, col string, mod modifier) string {
if !enabled {
return str
}
s := csi
if mod&Light != 0 {
s += foregroundLight
} else {
s += foreground
}
s += col
if mod&Bold != 0 {
s += bold
}
if mod&Italic != 0 {
s += italic
}
if mod&Underline != 0 {
s += underline
}
if mod&Blink != 0 {
s += blink
}
s += sgr + str
s += csi + reset + sgr
return s
}
99 changes: 99 additions & 0 deletions ansi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package ansi

import (
"testing"

"github.com/zhevron/match"
)

func TestBlack(t *testing.T) {
str := Black("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[30mtest\033[0m")
}

func TestRed(t *testing.T) {
str := Red("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[31mtest\033[0m")
}

func TestGreen(t *testing.T) {
str := Green("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[32mtest\033[0m")
}

func TestYellow(t *testing.T) {
str := Yellow("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[33mtest\033[0m")
}

func TestBlue(t *testing.T) {
str := Blue("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[34mtest\033[0m")
}

func TestMagenta(t *testing.T) {
str := Magenta("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[35mtest\033[0m")
}

func TestCyan(t *testing.T) {
str := Cyan("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[36mtest\033[0m")
}

func TestWhite(t *testing.T) {
str := White("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[37mtest\033[0m")
}

func TestColorString_Light(t *testing.T) {
str := Black("test", Light)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[90mtest\033[0m")
}

func TestColorString_Bold(t *testing.T) {
str := Black("test", Bold)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[30;1mtest\033[0m")
}

func TestColorString_Italic(t *testing.T) {
str := Black("test", Italic)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[30;3mtest\033[0m")
}

func TestColorString_Underline(t *testing.T) {
str := Black("test", Underline)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[30;4mtest\033[0m")
}

func TestColorString_Blink(t *testing.T) {
str := Black("test", Blink)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[30;5mtest\033[0m")
}

func TestColorString_Combined(t *testing.T) {
str := Black("test", Bold|Underline)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "\033[30;1;4mtest\033[0m")
}

func TestColorString_Disabled(t *testing.T) {
Disable()
str := Black("test", None)
match.GreaterThan(t, len(str), 0)
match.Equals(t, str, "test")
Enable()
}
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package ansi provides utility functions to apply ANSI escape sequences
// to strings.
package ansi
31 changes: 31 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ansi

import "fmt"

func ExampleBlack() {
fmt.Println(Black("This text will be black", None))
}

func ExampleBlack_light() {
fmt.Println(Black("This text will be light black", Light))
}

func ExampleBlack_bold() {
fmt.Println(Black("This text will be bold and black", Bold))
}

func ExampleBlack_italic() {
fmt.Println(Black("This text will be italic and black", Italic))
}

func ExampleBlack_underline() {
fmt.Println(Black("This text will be underlined and black", Underline))
}

func ExampleBlack_blink() {
fmt.Println(Black("This text will be blinking and black", Blink))
}

func ExampleBlack_combined() {
fmt.Println(Black("This text will be bold, underlined and black", Bold|Underline))
}
20 changes: 20 additions & 0 deletions wercker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
box: golang

build:
steps:
- setup-go-workspace
- golint
- script:
name: go get
code: |
go get -t ./...
- script:
name: go build
code: |
go build ./...
- script:
name: go test
code: |
go test ./...
- zhevron/goveralls:
token: $COVERALLS_TOKEN

0 comments on commit 938d157

Please sign in to comment.