use a seperate goroutine/channel for quit

This commit is contained in:
Gokcehan 2016-10-27 22:24:42 +03:00
parent c9b4389c65
commit 1e0b558344
4 changed files with 30 additions and 22 deletions

23
app.go
View File

@ -11,6 +11,18 @@ import (
type App struct { type App struct {
ui *UI ui *UI
nav *Nav 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 { func waitKey() error {
@ -39,9 +51,8 @@ func waitKey() error {
func (app *App) handleInp() { func (app *App) handleInp() {
for { for {
// exit check is done on the top just in case user quits select {
// before input handling for some reason (e.g. in configuration file) case <-app.quit:
if gExitFlag {
log.Print("bye!") log.Print("bye!")
if gLastDirPath != "" { if gLastDirPath != "" {
@ -60,7 +71,7 @@ func (app *App) handleInp() {
} }
return return
} default:
e, c := app.ui.getExpr(app.nav) e, c := app.ui.getExpr(app.nav)
if e == nil { if e == nil {
continue continue
@ -68,12 +79,10 @@ func (app *App) handleInp() {
for i := 0; i < c; i++ { for i := 0; i < c; i++ {
e.eval(app, nil) e.eval(app, nil)
} }
if gExitFlag {
continue
}
app.ui.draw(app.nav) app.ui.draw(app.nav)
} }
} }
}
func (app *App) exportVars() { func (app *App) exportVars() {
var envFile string var envFile string

View File

@ -25,9 +25,7 @@ func client() {
} }
defer termbox.Close() defer termbox.Close()
ui := newUI() app := newApp()
nav := newNav(ui.wins[0].h)
app := &App{ui, nav}
app.ui.loadFile(app.nav) app.ui.loadFile(app.nav)

View File

@ -193,6 +193,7 @@ func (e *CallExpr) eval(app *App, args []string) {
out, err := os.Create(gSelectionPath) out, err := os.Create(gSelectionPath)
if err != nil { if err != nil {
log.Printf("opening selection file: %s", err) log.Printf("opening selection file: %s", err)
return
} }
defer out.Close() defer out.Close()
@ -211,7 +212,8 @@ func (e *CallExpr) eval(app *App, args []string) {
log.Printf("writing selection file: %s", err) log.Printf("writing selection file: %s", err)
} }
gExitFlag = true go func() { app.quit <- true }()
return return
} }
@ -219,7 +221,7 @@ func (e *CallExpr) eval(app *App, args []string) {
cmd.eval(app, e.args) cmd.eval(app, e.args)
} }
case "quit": case "quit":
gExitFlag = true go func() { app.quit <- true }()
case "bot": case "bot":
app.nav.bot() app.nav.bot()
app.ui.loadFile(app.nav) app.ui.loadFile(app.nav)

View File

@ -20,7 +20,6 @@ var (
) )
var ( var (
gExitFlag bool
gLastDirPath string gLastDirPath string
gSelectionPath string gSelectionPath string
gSocketPath string gSocketPath string