Applied changes from @dawidd6 to check on terminal space at start

This commit is contained in:
Quentin McGaw 2019-07-05 14:39:27 +02:00
parent 29ad43c4a4
commit ee6b990792

View file

@ -4,7 +4,9 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"time" "time"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
@ -66,21 +68,20 @@ func (app *App) Run() error {
func waitForTerminalSpace() error { func waitForTerminalSpace() error {
// before we do anything, we need to check that we have some window space available // before we do anything, we need to check that we have some window space available
tStart := time.Now() width, height, err := terminal.GetSize(int(os.Stdin.Fd()))
count := 0
for {
width, _, err := terminal.GetSize(int(os.Stdin.Fd()))
if err != nil { if err != nil {
return err return err
} }
if width != 0 { if width > 0 && height > 0 {
return nil return nil
} }
count++ winch := make(chan os.Signal)
time.Sleep(time.Millisecond * 50 * time.Duration(count)) signal.Notify(winch, syscall.SIGWINCH)
if count > 1 { select {
fmt.Fprintf(os.Stderr, "waited %s for available terminal space\n", time.Since(tStart)) case <-winch:
} return nil
case <-time.After(time.Second):
return fmt.Errorf("there is no available terminal space")
} }
} }