diff --git a/app.go b/app.go index c07f07a..9263319 100644 --- a/app.go +++ b/app.go @@ -9,8 +9,20 @@ import ( ) type App struct { - ui *UI - nav *Nav + ui *UI + nav *Nav + quit chan bool +} + +func newApp() *App { + ui := newUI() + nav := newNav(ui.wins[0].h) + quit := make(chan bool) + return &App{ + ui: ui, + nav: nav, + quit: quit, + } } func waitKey() error { @@ -39,9 +51,8 @@ func waitKey() error { func (app *App) handleInp() { for { - // exit check is done on the top just in case user quits - // before input handling for some reason (e.g. in configuration file) - if gExitFlag { + select { + case <-app.quit: log.Print("bye!") if gLastDirPath != "" { @@ -60,18 +71,16 @@ func (app *App) handleInp() { } return + default: + e, c := app.ui.getExpr(app.nav) + if e == nil { + continue + } + for i := 0; i < c; i++ { + e.eval(app, nil) + } + app.ui.draw(app.nav) } - e, c := app.ui.getExpr(app.nav) - if e == nil { - continue - } - for i := 0; i < c; i++ { - e.eval(app, nil) - } - if gExitFlag { - continue - } - app.ui.draw(app.nav) } } diff --git a/client.go b/client.go index 7969c65..d647dc0 100644 --- a/client.go +++ b/client.go @@ -25,9 +25,7 @@ func client() { } defer termbox.Close() - ui := newUI() - nav := newNav(ui.wins[0].h) - app := &App{ui, nav} + app := newApp() app.ui.loadFile(app.nav) diff --git a/eval.go b/eval.go index 6facc28..f2a2927 100644 --- a/eval.go +++ b/eval.go @@ -193,6 +193,7 @@ func (e *CallExpr) eval(app *App, args []string) { out, err := os.Create(gSelectionPath) if err != nil { log.Printf("opening selection file: %s", err) + return } defer out.Close() @@ -211,7 +212,8 @@ func (e *CallExpr) eval(app *App, args []string) { log.Printf("writing selection file: %s", err) } - gExitFlag = true + go func() { app.quit <- true }() + return } @@ -219,7 +221,7 @@ func (e *CallExpr) eval(app *App, args []string) { cmd.eval(app, e.args) } case "quit": - gExitFlag = true + go func() { app.quit <- true }() case "bot": app.nav.bot() app.ui.loadFile(app.nav) diff --git a/main.go b/main.go index a5946b7..3ccbb18 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,6 @@ var ( ) var ( - gExitFlag bool gLastDirPath string gSelectionPath string gSocketPath string