Fixed infinite loop for checking terminal space as discussed in PR

- All the checks are in the `waitForTerminalSpace` function
- It tries to run the program directly if the width is positive
- It then sleeps 50 milliseconds the first time if the width is zero
- If after the first sleep, the terminal width is still zero, it prints the waiting time to stderr and will keep on doing so with increasing sleep times.
This commit is contained in:
Quentin McGaw 2019-07-05 01:31:28 +02:00
parent e637497fdb
commit 29ad43c4a4

View file

@ -1,6 +1,7 @@
package app
import (
"fmt"
"io"
"os"
"strings"
@ -55,20 +56,34 @@ func NewApp(config *config.AppConfig) (*App, error) {
}
func (app *App) Run() error {
// before we do anything, we need to check that we have some window space available
var err error
width := 0
for width == 0 {
width, _, err = terminal.GetSize(int(os.Stdin.Fd()))
if err != nil {
return err
}
time.Sleep(time.Millisecond * 50)
err := waitForTerminalSpace()
if err != nil {
return err
}
err = app.Gui.RunWithSubprocesses()
return err
}
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))
}
}
}
// Close closes any resources
func (app *App) Close() error {
for _, closer := range app.closers {