From 7d904fdef7bae09d9373850346daccfcb7a24447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 27 Nov 2019 15:31:37 +0100 Subject: [PATCH] Fix detecting terminal under Git Bash on Windows --- utils/table_printer.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/utils/table_printer.go b/utils/table_printer.go index 1d763b8ce..177663096 100644 --- a/utils/table_printer.go +++ b/utils/table_printer.go @@ -4,7 +4,11 @@ import ( "fmt" "io" "os" + "os/exec" + "strconv" + "strings" + "github.com/mattn/go-isatty" "golang.org/x/crypto/ssh/terminal" ) @@ -17,11 +21,19 @@ type TablePrinter interface { func NewTablePrinter(w io.Writer) TablePrinter { if outFile, isFile := w.(*os.File); isFile { - fd := int(outFile.Fd()) - if terminal.IsTerminal(fd) { + isCygwin := isatty.IsCygwinTerminal(outFile.Fd()) + if isatty.IsTerminal(outFile.Fd()) || isCygwin { ttyWidth := 80 - if w, _, err := terminal.GetSize(fd); err == nil { + if w, _, err := terminal.GetSize(int(outFile.Fd())); err == nil { ttyWidth = w + } else if isCygwin { + tputCmd := exec.Command("tput", "cols") + tputCmd.Stdin = os.Stdin + if out, err := tputCmd.Output(); err == nil { + if w, err := strconv.Atoi(strings.TrimSpace(string(out))); err == nil { + ttyWidth = w + } + } } return &ttyTablePrinter{ out: w,