Skip to content

Commit

Permalink
In goawk command, don't buffer stdout if it's a terminal (#116)
Browse files Browse the repository at this point in the history
In the `goawk` command (not the `interp` package), don't buffer output if stdout is a terminal.

Fixes #111
  • Loading branch information
benhoyt committed May 18, 2022
1 parent f261190 commit 5f3b124
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions goawk.go
Expand Up @@ -30,6 +30,7 @@ package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -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),
Expand All @@ -258,6 +267,7 @@ func main() {
"INPUTMODE", inputMode,
"OUTPUTMODE", outputMode,
},
Output: stdout,
}
for _, v := range vars {
parts := strings.SplitN(v, "=", 2)
Expand Down

0 comments on commit 5f3b124

Please sign in to comment.