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 {
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,7 +71,7 @@ func (app *App) handleInp() {
}
return
}
default:
e, c := app.ui.getExpr(app.nav)
if e == nil {
continue
@ -68,11 +79,9 @@ func (app *App) handleInp() {
for i := 0; i < c; i++ {
e.eval(app, nil)
}
if gExitFlag {
continue
}
app.ui.draw(app.nav)
}
}
}
func (app *App) exportVars() {

View File

@ -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)

View File

@ -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)

View File

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