Skip to content

Commit

Permalink
tasklog: check for connected tty on the actual writer
Browse files Browse the repository at this point in the history
  • Loading branch information
steffengodskesen authored and ttaylorr committed Nov 14, 2018
1 parent 03aa2ef commit b40c5cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
var (
Debugging = false
ErrorBuffer = &bytes.Buffer{}
ErrorWriter = io.MultiWriter(os.Stderr, ErrorBuffer)
OutputWriter = io.MultiWriter(os.Stdout, ErrorBuffer)
ErrorWriter = newMultiWriter(os.Stderr, ErrorBuffer)
OutputWriter = newMultiWriter(os.Stdout, ErrorBuffer)
ManPages = make(map[string]string, 20)
tqManifest = make(map[string]*tq.Manifest)

Expand Down
26 changes: 26 additions & 0 deletions commands/multiwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package commands

import (
"io"
"os"
)

type multiWriter struct {
writer io.Writer
fd uintptr
}

func newMultiWriter(f *os.File, writers ...io.Writer) *multiWriter {
return &multiWriter{
writer: io.MultiWriter(append([]io.Writer{f}, writers...)...),
fd: f.Fd(),
}
}

func (w *multiWriter) Write(p []byte) (n int, err error) {
return w.writer.Write(p)
}

func (w *multiWriter) Fd() uintptr {
return w.fd
}
17 changes: 17 additions & 0 deletions tasklog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Logger struct {
// this logger is running within.
widthFn func() int

// tty is true if sink is connected to a terminal
tty bool

// forceProgress forces progress status even when stdout is not a tty
forceProgress bool

Expand Down Expand Up @@ -81,11 +84,25 @@ func NewLogger(sink io.Writer, options ...Option) *Logger {
option(l)
}

l.tty = tty(sink)

go l.consume()

return l
}

type hasFd interface {
Fd() uintptr
}

// tty returns true if the writer is connected to a tty
func tty(writer io.Writer) bool {
if v, ok := writer.(hasFd); ok {
return isatty.IsTerminal(v.Fd())
}
return false
}

// Close closes the queue and does not allow new Tasks to be `enqueue()`'d. It
// waits until the currently running Task has completed.
func (l *Logger) Close() {
Expand Down

0 comments on commit b40c5cd

Please sign in to comment.