From 5f3b12444f510f3cfa574d58df0bdeaa763ed549 Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Wed, 18 May 2022 20:08:23 +1200 Subject: [PATCH] In goawk command, don't buffer stdout if it's a terminal (#116) In the `goawk` command (not the `interp` package), don't buffer output if stdout is a terminal. Fixes https://github.com/benhoyt/goawk/issues/111 --- goawk.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/goawk.go b/goawk.go index 77d2caf6..dfaf1df4 100644 --- a/goawk.go +++ b/goawk.go @@ -30,6 +30,7 @@ package main import ( "bytes" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -250,6 +251,14 @@ func main() { inputMode += " header" } + // Don't buffer output if stdout is a terminal (default output writer when + // Config.Output is nil is a buffered version of os.Stdout). + var stdout io.Writer + stdoutInfo, err := os.Stdout.Stat() + if err == nil && stdoutInfo.Mode()&os.ModeCharDevice != 0 { + stdout = os.Stdout + } + config := &interp.Config{ Argv0: filepath.Base(os.Args[0]), Args: expandWildcardsOnWindows(args), @@ -258,6 +267,7 @@ func main() { "INPUTMODE", inputMode, "OUTPUTMODE", outputMode, }, + Output: stdout, } for _, v := range vars { parts := strings.SplitN(v, "=", 2)