From 29ad43c4a4fdbc09d8085c68c820fbde8aa473a2 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 5 Jul 2019 01:31:28 +0200 Subject: [PATCH] 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. --- pkg/app/app.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 84034da7..4f845439 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -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 {