Skip to content

Commit

Permalink
minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Apr 29, 2020
1 parent c5311c5 commit 2e3d576
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion color.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func WriteRich(w io.Writer, s string, colorCode int, options ...RichOption) {
for _, output := range p.outputs {
if SupportColors(output) {
if len(richBytes) == 0 {
richBytes = []byte(Rich(s, colorCode, options...))
richBytes = []byte(Rich(s, colorCode, options...)) // no strToBytes; colors are conflicting with --race detector.
}

_, _ = output.Write(richBytes)
Expand Down
8 changes: 4 additions & 4 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Printer struct {
// and write raw text to those that do not (e.g. files).
// Note: we could implementing our own multi writer and skip the use
// of the std package, but let's don't do that unless is requested.
outputs []io.Writer
outputs []*outputWriter

mu sync.Mutex
marshal MarshalerFunc
Expand Down Expand Up @@ -102,7 +102,7 @@ func NewPrinter(name string, output io.Writer) *Printer {
p := &Printer{
Name: name,
Output: output,
outputs: []io.Writer{output},
outputs: wrapWriters(output),
Writer: buf,
Reader: buf,
Closer: NopCloser(),
Expand Down Expand Up @@ -247,7 +247,7 @@ func (p *Printer) AddOutput(writers ...io.Writer) *Printer {
}
}

p.outputs = append(p.outputs, writers...)
p.outputs = append(p.outputs, wrapWriters(writers...)...)

w := io.MultiWriter(append(writers, p.Output)...)

Expand Down Expand Up @@ -287,7 +287,7 @@ func (p *Printer) SetOutput(writers ...io.Writer) *Printer {
}

p.mu.Lock()
p.outputs = writers
p.outputs = wrapWriters(writers...)
p.Output = w
p.IsTerminal = terminal.IsTerminal(w)
p.mu.Unlock()
Expand Down
27 changes: 27 additions & 0 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,35 @@ import (
"github.com/kataras/pio/terminal"
)

// outputWriter just caches the "supportColors"
// in order to reduce syscalls for known printers.
type outputWriter struct {
io.Writer
supportColors bool
}

func wrapWriters(output ...io.Writer) []*outputWriter {
outs := make([]*outputWriter, 0, len(output))
for _, w := range output {
outs = append(outs, &outputWriter{
Writer: w,
supportColors: SupportColors(w),
})
}

return outs
}

// SupportColors reports whether the "w" io.Writer is not a file and it does support colors.
func SupportColors(w io.Writer) bool {
if w == nil {
return false
}

if sc, ok := w.(*outputWriter); ok {
return sc.supportColors
}

isTerminal := !IsNop(w) && terminal.IsTerminal(w)
if isTerminal && runtime.GOOS == "windows" {
// if on windows then return true only when it does support 256-bit colors,
Expand Down

0 comments on commit 2e3d576

Please sign in to comment.