Skip to content

Commit

Permalink
Auto-disable color coding when piped
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Oct 12, 2019
1 parent 3520ced commit 834e5f3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/color/coloring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package color

import (
"fmt"
"os"
"strings"
)

Expand Down Expand Up @@ -34,12 +35,21 @@ const (

// Enabled controls whether or not coloring is applied
var Disabled = false
var forceDisable = false

var groupColors = [...]ColorCode{Red, Green, Yellow, Blue, Magenta, Cyan}

func init() {
if fi, err := os.Stdout.Stat(); err == nil {
if (fi.Mode() & os.ModeCharDevice) == 0 {
forceDisable = true
}
}
}

// Wrap surroungs a string with a color (if enabled)
func Wrap(color ColorCode, s string) string {
if Disabled {
if Disabled || forceDisable {
return s
}

Expand All @@ -64,7 +74,7 @@ func Wrapi(color ColorCode, s interface{}) string {
// WrapIndices color-codes by group pairs (regex-style)
// [aStart, aEnd, bStart, bEnd...]
func WrapIndices(s string, groups []int) string {
if Disabled {
if Disabled || forceDisable {
return s
}
if len(groups) == 0 || len(groups)%2 != 0 {
Expand Down

0 comments on commit 834e5f3

Please sign in to comment.