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