From e760917e8531f5438ae184ec0c1599f5a3785d1b Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 5 Sep 2024 20:24:14 +0300 Subject: [PATCH] feat: enable help, add bindings there --- main.go | 37 ++++++++++++++++++++++++++++++++----- types/keymaps.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 types/keymaps.go diff --git a/main.go b/main.go index a1ff471..97a45ad 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "git.src.quest/~skye/tamma/styles" "git.src.quest/~skye/tamma/types" + "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -37,6 +38,7 @@ type model struct { selectedHost types.HostItem actionList list.Model selectedAction types.ActionItem + actionListKeys *types.ActionListKeyMap } type execFinishedMsg struct{ err error } @@ -192,12 +194,23 @@ func main() { if hostListH > 10 { hostListH = 10 } + hostListKeys := types.NewHostListKeyMap() hostList := list.New(hostListItems, types.HostItemDelegate{}, 48, hostListH+4) hostList.Title = "Select host:" - hostList.SetShowHelp(false) + hostList.SetShowHelp(true) hostList.SetFilteringEnabled(true) hostList.SetShowStatusBar(false) hostList.DisableQuitKeybindings() + hostList.AdditionalShortHelpKeys = func() []key.Binding { + return []key.Binding{ + hostListKeys.Quit, + } + } + hostList.AdditionalFullHelpKeys = func() []key.Binding { + return []key.Binding{ + hostListKeys.Quit, + } + } hostList.Styles.Title = styles.ListTitle hostList.Styles.PaginationStyle = styles.ListPaginaton @@ -227,19 +240,33 @@ func main() { if actionListH > 10 { actionListH = 10 } + actionListKeys := types.NewActionListKeyMap() actionList := list.New(actionListItems, types.ActionItemDelegate{}, 48, actionListH+4) actionList.Title = "What shall we do today?" - actionList.SetShowHelp(false) + actionList.SetShowHelp(true) actionList.SetFilteringEnabled(true) actionList.SetShowStatusBar(false) actionList.DisableQuitKeybindings() + actionList.AdditionalShortHelpKeys = func() []key.Binding { + return []key.Binding{ + actionListKeys.Back, + actionListKeys.CopyCommand, + } + } + actionList.AdditionalFullHelpKeys = func() []key.Binding { + return []key.Binding{ + actionListKeys.Back, + actionListKeys.CopyCommand, + } + } actionList.Styles.Title = styles.ListTitle actionList.Styles.PaginationStyle = styles.ListPaginaton p := tea.NewProgram(model{ - state: hostSelect, - hostList: hostList, - actionList: actionList, + state: hostSelect, + hostList: hostList, + actionList: actionList, + actionListKeys: actionListKeys, }, tea.WithAltScreen()) if _, err := p.Run(); err != nil { fmt.Printf("An error occured while running: %v\n", err) diff --git a/types/keymaps.go b/types/keymaps.go new file mode 100644 index 0000000..9f27974 --- /dev/null +++ b/types/keymaps.go @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2024 Jonni Liljamo + * + * This file is licensed under GPL-3.0-only, see NOTICE and LICENSE for + * more information. + */ + +package types + +import ( + "github.com/charmbracelet/bubbles/key" +) + +// HostListKeyMap defines key bindings for the host list. +type HostListKeyMap struct { + Quit key.Binding +} + +// NewHostListKeyMap returns a new HostListKeyMap. +func NewHostListKeyMap() *HostListKeyMap { + return &HostListKeyMap{ + Quit: key.NewBinding( + key.WithKeys("q"), + key.WithHelp("q", "quit"), + ), + } +} + +// ActionListKeyMap defines key bindings for the action list. +type ActionListKeyMap struct { + Back key.Binding + CopyCommand key.Binding +} + +// NewActionListKeyMap returns a new ActionListKeyMap. +func NewActionListKeyMap() *ActionListKeyMap { + return &ActionListKeyMap{ + Back: key.NewBinding( + key.WithKeys("q"), + key.WithHelp("q", "back"), + ), + CopyCommand: key.NewBinding( + key.WithKeys("c"), + key.WithHelp("c", "copy cmd"), + ), + } +} -- 2.44.1