Fix measuring terminal width for output
This commit is contained in:
parent
9109b68344
commit
fae66a62e4
1 changed files with 8 additions and 4 deletions
|
|
@ -20,6 +20,8 @@ type IOStreams struct {
|
|||
Out io.Writer
|
||||
ErrOut io.Writer
|
||||
|
||||
// the original (non-colorable) output stream
|
||||
originalOut io.Writer
|
||||
colorEnabled bool
|
||||
|
||||
stdinTTYOverride bool
|
||||
|
|
@ -81,15 +83,16 @@ func (s *IOStreams) IsStderrTTY() bool {
|
|||
|
||||
func (s *IOStreams) TerminalWidth() int {
|
||||
defaultWidth := 80
|
||||
if s.stdoutTTYOverride {
|
||||
return defaultWidth
|
||||
out := s.Out
|
||||
if s.originalOut != nil {
|
||||
out = s.originalOut
|
||||
}
|
||||
|
||||
if w, _, err := terminalSize(s.Out); err == nil {
|
||||
if w, _, err := terminalSize(out); err == nil {
|
||||
return w
|
||||
}
|
||||
|
||||
if isCygwinTerminal(s.Out) {
|
||||
if isCygwinTerminal(out) {
|
||||
tputCmd := exec.Command("tput", "cols")
|
||||
tputCmd.Stdin = os.Stdin
|
||||
if out, err := tputCmd.Output(); err == nil {
|
||||
|
|
@ -108,6 +111,7 @@ func System() *IOStreams {
|
|||
|
||||
io := &IOStreams{
|
||||
In: os.Stdin,
|
||||
originalOut: os.Stdout,
|
||||
Out: colorable.NewColorable(os.Stdout),
|
||||
ErrOut: colorable.NewColorable(os.Stderr),
|
||||
colorEnabled: os.Getenv("NO_COLOR") == "" && stdoutIsTTY,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue