diff --git a/tui/keymap.go b/tui/keymap.go index d0e8676..fdec422 100644 --- a/tui/keymap.go +++ b/tui/keymap.go @@ -4,19 +4,20 @@ package tui import ( "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/viewport" ) // ListKeyMap is the keymap for the application type ListKeyMap struct { help.KeyMap + ListKeyMap list.KeyMap VpKM viewport.KeyMap Back key.Binding - Enter key.Binding + Select key.Binding Quit key.Binding UpDown key.Binding LeftRight key.Binding - ForwardSlash key.Binding Apply key.Binding Delete key.Binding Print key.Binding @@ -69,14 +70,10 @@ func NewListKeyMap() *ListKeyMap { key.WithKeys("?"), key.WithHelp("?", "help"), ), - Enter: key.NewBinding( + Select: key.NewBinding( key.WithKeys("enter"), key.WithHelp("↲", "select"), ), - ForwardSlash: key.NewBinding( - key.WithKeys("/"), - key.WithHelp("/", "filter"), - ), ShowRessources: key.NewBinding( key.WithKeys("r"), key.WithHelp("r", "show ressources"), @@ -93,114 +90,126 @@ func NewListKeyMap() *ListKeyMap { key.WithKeys("T"), key.WithHelp("T", "generate list tested"), ), - VpKM: viewport.DefaultKeyMap(), + VpKM: viewport.DefaultKeyMap(), + ListKeyMap: list.DefaultKeyMap(), } } // ShortHelp returns the short help func (m ListKeyMap) ShortHelp() []key.Binding { - kb := make([]key.Binding, 0) - kb = append(kb, m.ActiveShortHelp...) - - return kb -} - -// FullHelp returns the full help -func (m ListKeyMap) FullHelp() [][]key.Binding { - return m.ActiveFullHelp -} - -// RootHelp sets the keymap for the root view -func (m *ListKeyMap) RootHelp() { - kb := []key.Binding{ - m.Get, - m.Help, - m.Quit, - } - - m.ActiveShortHelp = kb - - m.ActiveFullHelp = [][]key.Binding{ - {m.UpDown, m.LeftRight, m.Back}, - {m.ForwardSlash, m.Enter}, - {m.Help, m.Quit}, - {m.ShowRessources, m.ShowTested, m.GenerateListTested}, - {m.Get}, - } -} - -// YamlHelp sets the keymap for the yaml view -func (m *ListKeyMap) YamlHelp() { - kb := []key.Binding{ + return []key.Binding{ + m.VpKM.Up, + m.VpKM.Down, + m.VpKM.PageUp, + m.VpKM.PageDown, + m.Select, + // m.ListKeyMap.Filter, m.Apply, m.Delete, m.Get, m.Print, - m.ShowDependanciesFiles, + // m.ShowDependanciesFiles, m.Help, + m.Back, m.Quit, } +} - m.ActiveShortHelp = kb - - m.ActiveFullHelp = [][]key.Binding{ +// FullHelp returns the full help +func (m ListKeyMap) FullHelp() [][]key.Binding { + return [][]key.Binding{ + {m.VpKM.Up, m.VpKM.Down, m.VpKM.PageUp, m.VpKM.PageDown}, {m.UpDown, m.LeftRight, m.Back}, - {m.ForwardSlash, m.Enter}, + {m.ListKeyMap.Filter, m.Select}, {m.Help, m.Quit}, {m.Apply, m.Delete, m.Print}, {m.Get, m.ShowDependanciesFiles}, + {m.ShowRessources, m.ShowTested, m.GenerateListTested}, } } -// GetHelp sets the keymap for the get view -func (m *ListKeyMap) GetHelp() { - m.ActiveShortHelp = []key.Binding{ - m.Apply, - m.Delete, - m.Back, - m.Quit, - } - - m.ActiveFullHelp = [][]key.Binding{} +// EnableViewPortKeys is the set of keys for the viewport +func (m *ListKeyMap) EnableViewPortKeys() { + m.disableK8SKeys() + m.Back.SetEnabled(true) + m.Select.SetEnabled(false) + + m.VpKM.Up.SetEnabled(true) + m.VpKM.Down.SetEnabled(true) + m.VpKM.PageUp.SetEnabled(true) + m.VpKM.PageDown.SetEnabled(true) + m.VpKM.HalfPageUp.SetEnabled(true) + m.VpKM.HalfPageDown.SetEnabled(true) + m.UpDown.SetEnabled(false) + m.LeftRight.SetEnabled(false) + m.Back.SetEnabled(true) + m.ListKeyMap.Filter.SetEnabled(false) } -// YamlActionHelp sets the keymap for the yaml action view -func (m *ListKeyMap) YamlActionHelp() { - - m.ActiveShortHelp = []key.Binding{ - m.Back, - m.Quit, - } +func (m *ListKeyMap) disableViewPortKeys() { + m.VpKM.Up.SetEnabled(false) + m.VpKM.Down.SetEnabled(false) + m.VpKM.PageUp.SetEnabled(false) + m.VpKM.PageDown.SetEnabled(false) + m.VpKM.HalfPageUp.SetEnabled(false) + m.VpKM.HalfPageDown.SetEnabled(false) + m.UpDown.SetEnabled(true) + m.LeftRight.SetEnabled(true) +} - m.ActiveFullHelp = [][]key.Binding{} +func (m *ListKeyMap) enableK8SKeys() { + m.Apply.SetEnabled(true) + m.Delete.SetEnabled(true) + m.Print.SetEnabled(true) + m.Get.SetEnabled(true) + m.ShowDependanciesFiles.SetEnabled(true) + m.ShowRessources.SetEnabled(false) + m.ShowTested.SetEnabled(false) + m.GenerateListTested.SetEnabled(false) } -// ViewPortHelp sets the keymap for the viewport view -func (m *ListKeyMap) ViewPortHelp() { - m.ActiveShortHelp = []key.Binding{ - m.VpKM.Up, - m.VpKM.Down, - m.VpKM.PageUp, - m.VpKM.PageDown, - m.Back, - m.Quit, - } +func (m *ListKeyMap) disableK8SKeys() { + m.Apply.SetEnabled(false) + m.Delete.SetEnabled(false) + m.Print.SetEnabled(false) + m.Get.SetEnabled(false) + m.ShowDependanciesFiles.SetEnabled(false) } -// ErrorHelp sets the keymap for the error view -func (m *ListKeyMap) ErrorHelp() { - m.ActiveShortHelp = []key.Binding{ - m.Back, - m.Quit, - } +// EnableRootKeys is the set of keys for the root +func (m *ListKeyMap) EnableRootKeys() { + m.disableK8SKeys() + m.disableViewPortKeys() + m.Back.SetEnabled(false) + m.Select.SetEnabled(true) + m.Get.SetEnabled(true) + m.Help.SetEnabled(true) + m.ListKeyMap.Filter.SetEnabled(true) + m.ShowRessources.SetEnabled(true) + m.ShowTested.SetEnabled(true) + m.GenerateListTested.SetEnabled(true) } -// OnlyBackQuit sets the keymap for the simple view -func (m *ListKeyMap) OnlyBackQuit() { - m.ActiveShortHelp = []key.Binding{ - m.Back, - m.Quit, - } +// EnableKindListKeys is the set of keys for the kind list +func (m *ListKeyMap) EnableKindListKeys() { + m.enableK8SKeys() + m.Help.SetEnabled(true) + m.ListKeyMap.Filter.SetEnabled(true) + m.Back.SetEnabled(false) + m.Select.SetEnabled(false) + m.UpDown.SetEnabled(true) + m.LeftRight.SetEnabled(true) + m.Back.SetEnabled(true) +} - m.ActiveFullHelp = [][]key.Binding{} +// EnablePrintK8SKeys is the set of keys for the k8s print view +func (m *ListKeyMap) EnablePrintK8SKeys() { + m.disableK8SKeys() + m.disableViewPortKeys() + m.ShowDependanciesFiles.SetEnabled(true) + m.Back.SetEnabled(true) + m.Select.SetEnabled(false) + m.ListKeyMap.Filter.SetEnabled(false) + m.UpDown.SetEnabled(false) + m.LeftRight.SetEnabled(false) } diff --git a/tui/pages/examples/examples.go b/tui/pages/examples/examples.go index eadc496..ed1ad62 100644 --- a/tui/pages/examples/examples.go +++ b/tui/pages/examples/examples.go @@ -121,9 +121,7 @@ func New(e tui.LoadedExamples, width, height int, c config.Provider) model { list.Title = "Choose an example" list.DisableQuitKeybindings() list.SetShowHelp(false) - // list.StatusMessageLifetime = 5 list.SetStatusBarItemName("example", "examples") - // list.Help = help.Model{}` header := header.New( "Bean "+c.Version, @@ -141,9 +139,7 @@ func New(e tui.LoadedExamples, width, height int, c config.Provider) model { k8sCmdList = make(map[string]*k8sCmd) // default activated keys - listKeys.Apply.SetEnabled(false) - listKeys.Delete.SetEnabled(false) - listKeys.Print.SetEnabled(false) + listKeys.EnableRootKeys() return model{ exampleList: e.Examples, @@ -171,7 +167,6 @@ func New(e tui.LoadedExamples, width, height int, c config.Provider) model { // Init initializes the model. func (m model) Init() tea.Cmd { - m.keys.RootHelp() return tea.Batch( tea.EnterAltScreen, m.header.Init(), @@ -218,22 +213,22 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch m.viewName { case pRessources: m, cmd := m.rootView() + m.keys.EnableRootKeys() return m, cmd case pViewPort: m.viewName = pRoot - m.keys.RootHelp() + m.keys.EnableRootKeys() case pPrintActions, pK8S: if m.viewName == pK8S && !m.keys.Apply.Enabled() { - m.keys.Get.SetEnabled(true) + m.keys.EnableRootKeys() m, cmd := m.rootView() return m, cmd } - m.keys.Get.SetEnabled(true) + m.keys.EnableKindListKeys() m.viewName = pRessources - m.keys.YamlHelp() cmd = m.currentList.NewStatusMessage("back to " + m.listName) // m.currentList.ResetSelected() cmds = append(cmds, cmd) @@ -243,7 +238,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, cmd) return m, tea.Batch(cmds...) - case key.Matches(msg, m.keys.Enter): + case key.Matches(msg, m.keys.Select): if m.viewName != pRoot { return m, nil } @@ -252,32 +247,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.currentList.ResetFilter() } - m.keys.Apply.SetEnabled(true) - m.keys.Delete.SetEnabled(true) - m.keys.Print.SetEnabled(true) + m.keys.EnableKindListKeys() m, cmd = m.showYaml(title) m.viewName = pRessources m.previousItemPostion = m.currentList.Index() - m.keys.YamlHelp() return m, cmd case key.Matches(msg, m.keys.Print): - if m.viewName == pRessources { - m.viewName = pK8S - m.viewName = pPrintActions - m.keys.YamlActionHelp() - } + m.keys.EnablePrintK8SKeys() + m.viewName = pPrintActions return m, nil case key.Matches(msg, m.keys.Help): - if m.viewName != pPrintActions && m.viewName != pK8S { + if m.viewName != pK8S { m.footer.Help.ShowAll = !m.footer.Help.ShowAll m.footer.Help.Width = m.width } return m, nil case key.Matches(msg, m.keys.ShowRessources): + m.keys.EnableViewPortKeys() + if m.viewName == pRoot { cmd = tools.RenderMarkdown(m.config.Path+"/list-resources.md", m.width) @@ -285,6 +276,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case key.Matches(msg, m.keys.ShowTested): + m.keys.EnableViewPortKeys() if m.viewName == pRoot { cmd = tools.RenderMarkdown(m.config.Path+"/list-tested.md", m.width) @@ -311,7 +303,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.errorPanel = m.errorPanel.RaiseError("no item selected, empty list ?", errors.New("m.currentList.SelectedItem() == nil")) m.errorRaised = true m.header.NotificationOK = tui.ErrorMark - m.keys.ErrorHelp() return m, cmd } @@ -348,8 +339,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.viewName = pK8S m.k8sCurrentIDView = cmdID verb = "managed" - m.keys.GetHelp() m.keys.Get.SetEnabled(false) + m.keys.Print.SetEnabled(false) + m.keys.ShowDependanciesFiles.SetEnabled(false) + m.keys.Select.SetEnabled(false) + m.keys.Back.SetEnabled(true) + m.keys.Help.SetEnabled(false) k8sCmdList[cmdID].verb = "get" m.k8sCurrentKind = selected.Description() @@ -396,7 +391,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tools.Markdown: m.viewName = pViewPort - m.keys.ViewPortHelp() m.markdown.Viewport.SetContent(msg.Content) m.markdown.Viewport.GotoTop() @@ -408,7 +402,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.errorPanel = m.errorPanel.RaiseError(msg.Reason, msg.Cause) m.errorRaised = true m.header.NotificationOK = tui.ErrorMark - m.keys.ErrorHelp() return m, cmd case tui.ListTestedDone: @@ -676,11 +669,11 @@ func (m model) tickCmd() tea.Cmd { func (m model) rootView() (model, tea.Cmd) { m.viewName = pRoot - m.keys.RootHelp() m.keys.Apply.SetEnabled(false) m.keys.Delete.SetEnabled(false) m.keys.Print.SetEnabled(false) + m.keys.Back.SetEnabled(false) m.currentList.NewStatusMessage("back to home !") diff --git a/tui/pages/footer/footer.go b/tui/pages/footer/footer.go index 76c1f6a..70e9c75 100644 --- a/tui/pages/footer/footer.go +++ b/tui/pages/footer/footer.go @@ -103,6 +103,6 @@ func (m Model) Width() int { } // SetWidth sets the width of the view -func (m Model) SetWidth(w int) { +func (m *Model) SetWidth(w int) { m.width = w }