Skip to content

Commit

Permalink
dont buffer terminal output (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfred-landrum authored Aug 4, 2020
1 parent 14f0513 commit a747329
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions emitter/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/brimsec/zq/pkg/iosrc"
"github.com/brimsec/zq/zio"
"github.com/brimsec/zq/zio/detector"
"golang.org/x/crypto/ssh/terminal"
)

type noClose struct {
Expand All @@ -33,20 +34,37 @@ func NewFile(path string, flags *zio.WriterFlags) (*zio.Writer, error) {
return NewFileWithSource(uri, flags, src)
}

func isTerminal(w io.Writer) bool {
if f, ok := w.(*os.File); ok {
if terminal.IsTerminal(int(f.Fd())) {
return true
}
}
return false
}

func NewFileWithSource(path iosrc.URI, flags *zio.WriterFlags, source iosrc.Source) (*zio.Writer, error) {
f, err := source.NewWriter(path)
if err != nil {
return nil, err
}

var wc io.WriteCloser
if path.Scheme == "stdio" {
// Don't close stdout in case we live inside something
// here that runs multiple instances of this to stdout.
f = &noClose{os.Stdout}
// Don't close stdio in case we live inside something
// that has multiple stdio users.
wc = &noClose{f}
// Don't buffer terminal output.
if !isTerminal(f) {
wc = bufwriter.New(wc)
}
} else {
wc = bufwriter.New(f)
}
// On close, zio.Writer.Close(), the zng WriteFlusher will be flushed
// then the bufwriter will closed (which will flush it's internal buffer
// then close the file)
w := detector.LookupWriter(bufwriter.New(f), flags)
w := detector.LookupWriter(wc, flags)
if w == nil {
return nil, unknownFormat(flags.Format)
}
Expand Down

0 comments on commit a747329

Please sign in to comment.