Skip to content

Commit

Permalink
Print functions
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Sep 25, 2016
1 parent f20a0d6 commit f9d5a06
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 66 deletions.
58 changes: 29 additions & 29 deletions color/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,53 @@ import github.com/labstack/gommon/color
### Colored text

```go
fmt.Println(color.Black("black"))
fmt.Println(color.Red("red"))
fmt.Println(color.Green("green"))
fmt.Println(color.Yellow("yellow"))
fmt.Println(color.Blue("blue"))
fmt.Println(color.Magenta("magenta"))
fmt.Println(color.Cyan("cyan"))
fmt.Println(color.White("white"))
fmt.Println(color.Grey("grey"))
color.Println(color.Black("black"))
color.Println(color.Red("red"))
color.Println(color.Green("green"))
color.Println(color.Yellow("yellow"))
color.Println(color.Blue("blue"))
color.Println(color.Magenta("magenta"))
color.Println(color.Cyan("cyan"))
color.Println(color.White("white"))
color.Println(color.Grey("grey"))
```
![Colored Text](http://i.imgur.com/8RtY1QR.png)

### Colored background

```go
fmt.Println(color.BlackBg("black background", color.Wht))
fmt.Println(color.RedBg("red background"))
fmt.Println(color.GreenBg("green background"))
fmt.Println(color.YellowBg("yellow background"))
fmt.Println(color.BlueBg("blue background"))
fmt.Println(color.MagentaBg("magenta background"))
fmt.Println(color.CyanBg("cyan background"))
fmt.Println(color.WhiteBg("white background"))
color.Println(color.BlackBg("black background", color.Wht))
color.Println(color.RedBg("red background"))
color.Println(color.GreenBg("green background"))
color.Println(color.YellowBg("yellow background"))
color.Println(color.BlueBg("blue background"))
color.Println(color.MagentaBg("magenta background"))
color.Println(color.CyanBg("cyan background"))
color.Println(color.WhiteBg("white background"))
```
![Colored Background](http://i.imgur.com/SrrS6lw.png)

### Emphasis

```go
fmt.Println(color.Bold("bold"))
fmt.Println(color.Dim("dim"))
fmt.Println(color.Italic("italic"))
fmt.Println(color.Underline("underline"))
fmt.Println(color.Inverse("inverse"))
fmt.Println(color.Hidden("hidden"))
fmt.Println(color.Strikeout("strikeout"))
color.Println(color.Bold("bold"))
color.Println(color.Dim("dim"))
color.Println(color.Italic("italic"))
color.Println(color.Underline("underline"))
color.Println(color.Inverse("inverse"))
color.Println(color.Hidden("hidden"))
color.Println(color.Strikeout("strikeout"))
```
![Emphasis](http://i.imgur.com/3RSJBbc.png)

### Mix and match

```go
fmt.Println(color.Green("bold green with white background", color.B, color.WhtBg))
fmt.Println(color.Red("underline red", color.U))
fmt.Println(color.Yellow("dim yellow", color.D))
fmt.Println(color.Cyan("inverse cyan", color.In))
fmt.Println(color.Blue("bold underline dim blue", color.B, color.U, color.D))
color.Println(color.Green("bold green with white background", color.B, color.WhtBg))
color.Println(color.Red("underline red", color.U))
color.Println(color.Yellow("dim yellow", color.D))
color.Println(color.Cyan("inverse cyan", color.In))
color.Println(color.Blue("bold underline dim blue", color.B, color.U, color.D))
```
![Mix and match](http://i.imgur.com/jWGq9Ca.png)

Expand Down
65 changes: 63 additions & 2 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package color
import (
"bytes"
"fmt"
"io"
"os"

"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
)

type (
Expand Down Expand Up @@ -118,13 +123,29 @@ func outer(n string) inner {

type (
Color struct {
output io.Writer
disabled bool
}
)

// New creates a Color instance.
func New() *Color {
return &Color{}
func New() (c *Color) {
c = new(Color)
c.SetOutput(colorable.NewColorableStdout())
return
}

// Output returns the output.
func (c *Color) Output() io.Writer {
return c.output
}

// SetOutput sets the output.
func (c *Color) SetOutput(w io.Writer) {
c.output = w
if w, ok := w.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
c.disabled = true
}
}

// Disable disables the colors and styles.
Expand All @@ -137,6 +158,21 @@ func (c *Color) Enable() {
c.disabled = false
}

// Print is analogous to `fmt.Print` with termial detection.
func (c *Color) Print(args ...interface{}) {
fmt.Fprint(c.output, args...)
}

// Println is analogous to `fmt.Println` with termial detection.
func (c *Color) Println(args ...interface{}) {
fmt.Fprintln(c.output, args...)
}

// Printf is analogous to `fmt.Printf` with termial detection.
func (c *Color) Printf(format string, args ...interface{}) {
fmt.Fprintf(c.output, format, args...)
}

func (c *Color) Black(msg interface{}, styles ...string) string {
return black(msg, styles, c)
}
Expand Down Expand Up @@ -237,6 +273,16 @@ func (c *Color) Strikeout(msg interface{}, styles ...string) string {
return strikeout(msg, styles, c)
}

// Output returns the output.
func Output() io.Writer {
return global.output
}

// SetOutput sets the output.
func SetOutput(w io.Writer) {
global.SetOutput(w)
}

func Disable() {
global.Disable()
}
Expand All @@ -245,6 +291,21 @@ func Enable() {
global.Enable()
}

// Print is analogous to `fmt.Print` with termial detection.
func Print(args ...interface{}) {
global.Print(args...)
}

// Println is analogous to `fmt.Println` with termial detection.
func Println(args ...interface{}) {
global.Println(args...)
}

// Printf is analogous to `fmt.Printf` with termial detection.
func Printf(format string, args ...interface{}) {
global.Printf(format, args...)
}

func Black(msg interface{}, styles ...string) string {
return global.Black(msg, styles...)
}
Expand Down
69 changes: 34 additions & 35 deletions color/color_test.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
package color

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestText(t *testing.T) {
fmt.Println("*** colored text ***")
fmt.Println(Black("black"))
fmt.Println(Red("red"))
fmt.Println(Green("green"))
fmt.Println(Yellow("yellow"))
fmt.Println(Blue("blue"))
fmt.Println(Magenta("magenta"))
fmt.Println(Cyan("cyan"))
fmt.Println(White("white"))
fmt.Println(Grey("grey"))
Println("*** colored text ***")
Println(Black("black"))
Println(Red("red"))
Println(Green("green"))
Println(Yellow("yellow"))
Println(Blue("blue"))
Println(Magenta("magenta"))
Println(Cyan("cyan"))
Println(White("white"))
Println(Grey("grey"))
}

func TestBackground(t *testing.T) {
fmt.Println("*** colored background ***")
fmt.Println(BlackBg("black background", Wht))
fmt.Println(RedBg("red background"))
fmt.Println(GreenBg("green background"))
fmt.Println(YellowBg("yellow background"))
fmt.Println(BlueBg("blue background"))
fmt.Println(MagentaBg("magenta background"))
fmt.Println(CyanBg("cyan background"))
fmt.Println(WhiteBg("white background"))
Println("*** colored background ***")
Println(BlackBg("black background", Wht))
Println(RedBg("red background"))
Println(GreenBg("green background"))
Println(YellowBg("yellow background"))
Println(BlueBg("blue background"))
Println(MagentaBg("magenta background"))
Println(CyanBg("cyan background"))
Println(WhiteBg("white background"))
}

func TestEmphasis(t *testing.T) {
fmt.Println("*** emphasis ***")
fmt.Println(Reset("reset"))
fmt.Println(Bold("bold"))
fmt.Println(Dim("dim"))
fmt.Println(Italic("italic"))
fmt.Println(Underline("underline"))
fmt.Println(Inverse("inverse"))
fmt.Println(Hidden("hidden"))
fmt.Println(Strikeout("strikeout"))
Println("*** emphasis ***")
Println(Reset("reset"))
Println(Bold("bold"))
Println(Dim("dim"))
Println(Italic("italic"))
Println(Underline("underline"))
Println(Inverse("inverse"))
Println(Hidden("hidden"))
Println(Strikeout("strikeout"))
}

func TestMixMatch(t *testing.T) {
fmt.Println("*** mix and match ***")
fmt.Println(Green("bold green with white background", B, WhtBg))
fmt.Println(Red("underline red", U))
fmt.Println(Yellow("dim yellow", D))
fmt.Println(Cyan("inverse cyan", In))
fmt.Println(Blue("bold underline dim blue", B, U, D))
Println("*** mix and match ***")
Println(Green("bold green with white background", B, WhtBg))
Println(Red("underline red", U))
Println(Yellow("dim yellow", D))
Println(Cyan("inverse cyan", In))
Println(Blue("bold underline dim blue", B, U, D))
}

func TestEnableDisable(t *testing.T) {
Expand Down

0 comments on commit f9d5a06

Please sign in to comment.