terminal.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package pio
  2. import (
  3. "io"
  4. "runtime"
  5. "github.com/kataras/pio/terminal"
  6. )
  7. // outputWriter just caches the "supportColors"
  8. // in order to reduce syscalls for known printers.
  9. type outputWriter struct {
  10. io.Writer
  11. supportColors bool
  12. }
  13. func wrapWriters(output ...io.Writer) []*outputWriter {
  14. outs := make([]*outputWriter, 0, len(output))
  15. for _, w := range output {
  16. outs = append(outs, &outputWriter{
  17. Writer: w,
  18. supportColors: SupportColors(w),
  19. })
  20. }
  21. return outs
  22. }
  23. // SupportColors reports whether the "w" io.Writer is not a file and it does support colors.
  24. func SupportColors(w io.Writer) bool {
  25. if w == nil {
  26. return false
  27. }
  28. if sc, ok := w.(*outputWriter); ok {
  29. return sc.supportColors
  30. }
  31. isTerminal := !IsNop(w) && terminal.IsTerminal(w)
  32. if isTerminal && runtime.GOOS == "windows" {
  33. // if on windows then return true only when it does support 256-bit colors,
  34. // this is why we initially do that terminal check for the "w" writer.
  35. return terminal.SupportColors
  36. }
  37. return isTerminal
  38. }