diff --git a/main.go b/main.go index 5fce2f5..06871a9 100644 --- a/main.go +++ b/main.go @@ -114,7 +114,7 @@ func main() { sort.Sort(menu.ByOptions(tags)) root.Options = tags - mt := menu.NewMenu() + mt := menu.NewMenu(app) mt.SetStyle(s) mt.SetSelectedStyle(ss) mt.SetCurrent(root) diff --git a/pkg/menu/menu.go b/pkg/menu/menu.go index 01f9c8e..22fd054 100644 --- a/pkg/menu/menu.go +++ b/pkg/menu/menu.go @@ -4,6 +4,7 @@ import ( "sync" "github.com/gdamore/tcell" + "github.com/gdamore/tcell/views" ) type Menu struct { @@ -12,20 +13,6 @@ type Menu struct { 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) { m.Init() mm := m.model @@ -34,14 +21,6 @@ func (m *Menu) SetCurrent(option Option) { 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) { m.model.style = 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.Init() + m.app = app return m } diff --git a/pkg/menu/menu_model.go b/pkg/menu/menu_model.go index ceb7d2c..951a754 100644 --- a/pkg/menu/menu_model.go +++ b/pkg/menu/menu_model.go @@ -7,17 +7,14 @@ import ( type MenuModel interface { GetBounds() (int, int) GetCurrent() Option - //GetParent() *Option GetCursor() (int, bool, bool) GetOption(int) Option MoveCursor(y int) SetCurrent(Option) - //SetParent(*Option) SetCursor(int) } type menuModel struct { - //parent *Option current Option width int @@ -33,14 +30,6 @@ func (m *menuModel) GetBounds() (int, int) { 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 { return m.current } diff --git a/pkg/menu/menu_view.go b/pkg/menu/menu_view.go index c2b1bf1..2cb42ca 100644 --- a/pkg/menu/menu_view.go +++ b/pkg/menu/menu_view.go @@ -3,6 +3,7 @@ package menu import ( "os" "os/exec" + "runtime" "sync" "github.com/gdamore/tcell" @@ -12,8 +13,10 @@ import ( ) type MenuView struct { + app *views.Application style tcell.Style selectedStyle tcell.Style + handleEvents bool model MenuModel view views.View @@ -24,9 +27,13 @@ type MenuView struct { } func (mv *MenuView) HandleEvent(e tcell.Event) bool { + if !mv.handleEvents { + return true + } if mv.model == nil { return false } + switch e := e.(type) { case *tcell.EventKey: switch e.Key() { @@ -174,17 +181,10 @@ func puts(s views.View, style tcell.Style, x, y int, str string) { } func (mv *MenuView) keyBack() { - // p := mv.model.GetParent() - // if p == nil { - // return - // } - cur := mv.model.GetCurrent() - if cur.parent == nil { return } - mv.model.SetCurrent(*cur.parent) w, h := mv.model.GetBounds() mv.port.SetContentSize(w, h, true) @@ -201,11 +201,9 @@ func (mv *MenuView) keyEnter() { } y, _, _ = mv.model.GetCursor() + cur := mv.model.GetCurrent() opt := mv.model.GetOption(y) opt.y = y - - cur := mv.model.GetCurrent() - opt.parent = &cur switch opt.Action { @@ -221,14 +219,19 @@ func (mv *MenuView) keyEnter() { if len(opt.Args) == 0 { return } + cmd := exec.Command(opt.Args[0], opt.Args[1:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout - err := cmd.Run() - mv.Draw() - if err != nil { - return - } + + mv.handleEvents = false + runtime.LockOSThread() + _ = 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() { mv.selectedStyle = mv.style.Reverse(true) + mv.handleEvents = true mv.once.Do(func() { mv.port = views.NewViewPort(nil, 0, 0, 0, 0)