From e78070ebbe5f2e19c70f12903b86edc47438f04a Mon Sep 17 00:00:00 2001 From: morganamilo Date: Thu, 27 Sep 2018 16:50:59 +0100 Subject: [PATCH] Support color = auto This is done using the test command as the stdlib lacks any way to do this without using syscalls directly --- exec.go | 7 +++++++ main.go | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/exec.go b/exec.go index 4f1b0f24..36c3ccb4 100644 --- a/exec.go +++ b/exec.go @@ -121,3 +121,10 @@ func passToGit(dir string, _args ...string) *exec.Cmd { cmd := exec.Command(config.GitBin, args...) return cmd } + +func isTty() bool { + cmd := exec.Command("test", "-t", "1") + cmd.Stdout = os.Stdout + err := cmd.Run() + return err == nil +} diff --git a/main.go b/main.go index 236d7a0d..a47c989c 100644 --- a/main.go +++ b/main.go @@ -146,12 +146,14 @@ func initAlpm() error { return err } - if value, _, _ := cmdArgs.getArg("color"); value == "always" || value == "auto" { + if value, _, _ := cmdArgs.getArg("color"); value == "always" { useColor = true + } else if value == "auto" { + useColor = isTty() } else if value == "never" { useColor = false } else { - useColor = pacmanConf.Color + useColor = pacmanConf.Color && isTty() } return nil