ignore input during exec and refresh after

This commit is contained in:
ron 2020-03-18 04:51:25 +01:00
parent 9af9e33bad
commit 5d4716746c
4 changed files with 23 additions and 50 deletions

View File

@ -114,7 +114,7 @@ func main() {
sort.Sort(menu.ByOptions(tags)) sort.Sort(menu.ByOptions(tags))
root.Options = tags root.Options = tags
mt := menu.NewMenu() mt := menu.NewMenu(app)
mt.SetStyle(s) mt.SetStyle(s)
mt.SetSelectedStyle(ss) mt.SetSelectedStyle(ss)
mt.SetCurrent(root) mt.SetCurrent(root)

View File

@ -4,6 +4,7 @@ import (
"sync" "sync"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
) )
type Menu struct { type Menu struct {
@ -12,20 +13,6 @@ type Menu struct {
MenuView MenuView
} }
// func (m *Menu) SetLines(lines []string) {
// m.Init()
// mm := m.model
// mm.width = 0
// mm.height = len(lines)
// mm.lines = lines
// for _, l := range lines {
// if len(l) > mm.width {
// mm.width = len(l)
// }
// }
// m.MenuView.SetModel(mm)
// }
func (m *Menu) SetCurrent(option Option) { func (m *Menu) SetCurrent(option Option) {
m.Init() m.Init()
mm := m.model mm := m.model
@ -34,14 +21,6 @@ func (m *Menu) SetCurrent(option Option) {
m.MenuView.SetModel(mm) m.MenuView.SetModel(mm)
} }
// func (m *Menu) SetOptions(options []Option) {
// m.Init()
// mm := m.model
// mm.SetOptions(options)
// m.MenuView.SetModel(mm)
// }
func (m *Menu) SetStyle(style tcell.Style) { func (m *Menu) SetStyle(style tcell.Style) {
m.model.style = style m.model.style = style
m.MenuView.SetStyle(style) m.MenuView.SetStyle(style)
@ -74,8 +53,9 @@ func (m *Menu) Init() {
}) })
} }
func NewMenu() *Menu { func NewMenu(app *views.Application) *Menu {
m := &Menu{} m := &Menu{}
m.Init() m.Init()
m.app = app
return m return m
} }

View File

@ -7,17 +7,14 @@ import (
type MenuModel interface { type MenuModel interface {
GetBounds() (int, int) GetBounds() (int, int)
GetCurrent() Option GetCurrent() Option
//GetParent() *Option
GetCursor() (int, bool, bool) GetCursor() (int, bool, bool)
GetOption(int) Option GetOption(int) Option
MoveCursor(y int) MoveCursor(y int)
SetCurrent(Option) SetCurrent(Option)
//SetParent(*Option)
SetCursor(int) SetCursor(int)
} }
type menuModel struct { type menuModel struct {
//parent *Option
current Option current Option
width int width int
@ -33,14 +30,6 @@ func (m *menuModel) GetBounds() (int, int) {
return m.width, m.height return m.width, m.height
} }
// func (m *menuModel) GetParent() *Option {
// return m.parent
// }
// func (m *menuModel) SetParent(opt *Option) {
// m.parent = opt
// }
func (m *menuModel) GetCurrent() Option { func (m *menuModel) GetCurrent() Option {
return m.current return m.current
} }

View File

@ -3,6 +3,7 @@ package menu
import ( import (
"os" "os"
"os/exec" "os/exec"
"runtime"
"sync" "sync"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -12,8 +13,10 @@ import (
) )
type MenuView struct { type MenuView struct {
app *views.Application
style tcell.Style style tcell.Style
selectedStyle tcell.Style selectedStyle tcell.Style
handleEvents bool
model MenuModel model MenuModel
view views.View view views.View
@ -24,9 +27,13 @@ type MenuView struct {
} }
func (mv *MenuView) HandleEvent(e tcell.Event) bool { func (mv *MenuView) HandleEvent(e tcell.Event) bool {
if !mv.handleEvents {
return true
}
if mv.model == nil { if mv.model == nil {
return false return false
} }
switch e := e.(type) { switch e := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
switch e.Key() { switch e.Key() {
@ -174,17 +181,10 @@ func puts(s views.View, style tcell.Style, x, y int, str string) {
} }
func (mv *MenuView) keyBack() { func (mv *MenuView) keyBack() {
// p := mv.model.GetParent()
// if p == nil {
// return
// }
cur := mv.model.GetCurrent() cur := mv.model.GetCurrent()
if cur.parent == nil { if cur.parent == nil {
return return
} }
mv.model.SetCurrent(*cur.parent) mv.model.SetCurrent(*cur.parent)
w, h := mv.model.GetBounds() w, h := mv.model.GetBounds()
mv.port.SetContentSize(w, h, true) mv.port.SetContentSize(w, h, true)
@ -201,11 +201,9 @@ func (mv *MenuView) keyEnter() {
} }
y, _, _ = mv.model.GetCursor() y, _, _ = mv.model.GetCursor()
cur := mv.model.GetCurrent()
opt := mv.model.GetOption(y) opt := mv.model.GetOption(y)
opt.y = y opt.y = y
cur := mv.model.GetCurrent()
opt.parent = &cur opt.parent = &cur
switch opt.Action { switch opt.Action {
@ -221,14 +219,19 @@ func (mv *MenuView) keyEnter() {
if len(opt.Args) == 0 { if len(opt.Args) == 0 {
return return
} }
cmd := exec.Command(opt.Args[0], opt.Args[1:]...) cmd := exec.Command(opt.Args[0], opt.Args[1:]...)
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
err := cmd.Run()
mv.Draw() mv.handleEvents = false
if err != nil { runtime.LockOSThread()
return _ = cmd.Run()
}
runtime.UnlockOSThread()
mv.handleEvents = true
mv.port.Clear()
mv.app.Refresh()
} }
} }
@ -363,6 +366,7 @@ func (mv *MenuView) SetSelectedStyle(s tcell.Style) {
func (mv *MenuView) Init() { func (mv *MenuView) Init() {
mv.selectedStyle = mv.style.Reverse(true) mv.selectedStyle = mv.style.Reverse(true)
mv.handleEvents = true
mv.once.Do(func() { mv.once.Do(func() {
mv.port = views.NewViewPort(nil, 0, 0, 0, 0) mv.port = views.NewViewPort(nil, 0, 0, 0, 0)