Fix detecting terminal under Git Bash on Windows
This commit is contained in:
parent
b21b93abe9
commit
7d904fdef7
1 changed files with 15 additions and 3 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue