Fix detecting terminal under Git Bash on Windows

This commit is contained in:
Mislav Marohnić 2019-11-27 15:31:37 +01:00
parent b21b93abe9
commit 7d904fdef7

View file

@ -4,7 +4,11 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"strconv"
"strings"
"github.com/mattn/go-isatty"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@ -17,11 +21,19 @@ type TablePrinter interface {
func NewTablePrinter(w io.Writer) TablePrinter { func NewTablePrinter(w io.Writer) TablePrinter {
if outFile, isFile := w.(*os.File); isFile { if outFile, isFile := w.(*os.File); isFile {
fd := int(outFile.Fd()) isCygwin := isatty.IsCygwinTerminal(outFile.Fd())
if terminal.IsTerminal(fd) { if isatty.IsTerminal(outFile.Fd()) || isCygwin {
ttyWidth := 80 ttyWidth := 80
if w, _, err := terminal.GetSize(fd); err == nil { if w, _, err := terminal.GetSize(int(outFile.Fd())); err == nil {
ttyWidth = w 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{ return &ttyTablePrinter{
out: w, out: w,