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 if err != nil {
for { return err
width, _, err := terminal.GetSize(int(os.Stdin.Fd())) }
if err != nil { if width > 0 && height > 0 {
return err return nil
} }
if width != 0 { winch := make(chan os.Signal)
return nil signal.Notify(winch, syscall.SIGWINCH)
} select {
count++ case <-winch:
time.Sleep(time.Millisecond * 50 * time.Duration(count)) return nil
if count > 1 { case <-time.After(time.Second):
fmt.Fprintf(os.Stderr, "waited %s for available terminal space\n", time.Since(tStart)) return fmt.Errorf("there is no available terminal space")
}
} }
} }