Skip to content

Commit f224b5e

Browse files
committed
One less syscall in most cases
1 parent 4516211 commit f224b5e

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

main.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/alessio/shellescape"
1919
"github.com/fatih/color"
20-
"github.com/mattn/go-isatty"
2120
"github.com/pkg/term/termios"
2221
"golang.design/x/chann"
2322
"golang.org/x/exp/slices"
@@ -35,8 +34,6 @@ var noLongerSpawnChildren = atomic.Bool{}
3534
var bold = color.New(color.Bold).SprintFunc()
3635
var yellow = color.New(color.FgYellow).SprintFunc()
3736

38-
var stdoutIsTty = isatty.IsTerminal(uintptr(syscall.Stdout))
39-
4037
func writeOut(out *Output) {
4138
var clearedOutBytes int64
4239

@@ -175,7 +172,7 @@ func displaySequentially(processes <-chan *ProcessResult) (exitCode int) {
175172
var originalTermState *term.State
176173
var err error
177174

178-
if stdoutIsTty {
175+
if stdoutIsTty() {
179176
originalTermState, err = term.GetState(syscall.Stdout)
180177
if err != nil {
181178
log.Printf("Warning: could get terminal state for stdout: %v\n", err)
@@ -199,7 +196,7 @@ func displaySequentially(processes <-chan *ProcessResult) (exitCode int) {
199196
if *flVerbose {
200197
quotedCommand := shellescape.QuoteCommand(processResult.originalCommand)
201198

202-
if firstProcess || !stdoutIsTty {
199+
if firstProcess || !stdoutIsTty() {
203200
_, _ = fmt.Fprintf(os.Stderr, bold("+ %s")+"\n", quotedCommand)
204201
} else if !processResult.isAlive() {
205202
_, _ = fmt.Fprintf(os.Stderr,

runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,14 @@ func runWithStdin(command []string, stdin io.Reader) (result *ProcessResult) {
313313

314314
recursiveTaskLimitClient().addWait(result)
315315

316-
if stdoutIsTty {
316+
if stdoutIsTty() {
317317
command = append([]string{executable(), "--_execute-and-flush-tty"}, command...)
318318
}
319319

320320
result.cmd = exec.Command(command[0], command[1:]...)
321321
result.cmd.Stdin = stdin
322322

323-
if stdoutIsTty {
323+
if stdoutIsTty() {
324324
result.output = runInteractive(result.cmd)
325325
} else {
326326
result.output = runNonInteractive(result.cmd)

util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"path/filepath"
88
"sync"
99
"syscall"
10+
11+
"github.com/mattn/go-isatty"
1012
)
1113

1214
func max(a, b int) int {
@@ -24,6 +26,10 @@ func min(a, b int) int {
2426
}
2527
}
2628

29+
var stdoutIsTty = sync.OnceValue(func() bool {
30+
return isatty.IsTerminal(uintptr(syscall.Stdout))
31+
})
32+
2733
var dataDir = sync.OnceValue(func() (dir string) {
2834
if _, err := os.Stat("/dev/shm"); !os.IsNotExist(err) {
2935
dir = "/dev/shm"

0 commit comments

Comments
 (0)