diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-09-17 21:02:30 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-09-17 21:02:30 +1000 |
commit | c00c834b359bc0ebcd6e940e5cb5ef6f7247a6c7 (patch) | |
tree | 67ca05580afab38e8a743d6c3fcf91caa1cf82ee /pkg/gui/menu_panel.go | |
parent | 3b765e5417501a39bca5c2f0038488dbbeb6b200 (diff) |
standardise rendering of lists in panels
Diffstat (limited to 'pkg/gui/menu_panel.go')
-rw-r--r-- | pkg/gui/menu_panel.go | 51 |
1 files changed, 11 insertions, 40 deletions
diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index 983543c94..ee0773a51 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -49,50 +49,20 @@ func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error { return gui.returnFocus(g, v) } -func (gui *Gui) GetKey(binding Binding) string { - r, ok := binding.Key.(rune) - key := "" - - if ok { - key = string(r) - } else if binding.KeyReadable != "" { - key = binding.KeyReadable - } - - return key -} - -func (gui *Gui) GetMaxKeyLength(bindings []Binding) int { - max := 0 - for _, binding := range bindings { - keyLength := len(gui.GetKey(binding)) - if keyLength > max { - max = keyLength - } - } - return max -} - func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error { var ( - contentGlobal, contentPanel []string - bindingsGlobal, bindingsPanel []Binding + bindingsGlobal, bindingsPanel []*Binding ) // clear keys slice, so we don't have ghost elements gui.State.Keys = gui.State.Keys[:0] bindings := gui.GetKeybindings() - padWidth := gui.GetMaxKeyLength(bindings) for _, binding := range bindings { - key := gui.GetKey(binding) - if key != "" && binding.Description != "" { - content := fmt.Sprintf("%s %s", utils.WithPadding(key, padWidth), binding.Description) + if binding.GetKey() != "" && binding.Description != "" { switch binding.ViewName { case "": - contentGlobal = append(contentGlobal, content) bindingsGlobal = append(bindingsGlobal, binding) case v.Name(): - contentPanel = append(contentPanel, content) bindingsPanel = append(bindingsPanel, binding) } } @@ -100,24 +70,25 @@ func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error { // append dummy element to have a separator between // panel and global keybindings - contentPanel = append(contentPanel, "") - bindingsPanel = append(bindingsPanel, Binding{}) - - content := append(contentPanel, contentGlobal...) + bindingsPanel = append(bindingsPanel, &Binding{}) gui.State.Keys = append(bindingsPanel, bindingsGlobal...) - contentJoined := strings.Join(content, "\n") - x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, contentJoined) + list, err := utils.RenderList(gui.State.Keys) + if err != nil { + return err + } + + x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, list) menuView, _ := g.SetView("menu", x0, y0, x1, y1, 0) menuView.Title = strings.Title(gui.Tr.SLocalize("menu")) menuView.FgColor = gocui.ColorWhite + menuView.Clear() + fmt.Fprint(menuView, list) if err := gui.renderMenuOptions(g); err != nil { return err } - fmt.Fprint(menuView, contentJoined) - g.Update(func(g *gocui.Gui) error { _, err := g.SetViewOnTop("menu") if err != nil { |