Move keymap to constants file

This commit is contained in:
The Magician 2024-06-08 16:41:09 +01:00
parent 7ead78327c
commit 017cad1f06
4 changed files with 55 additions and 50 deletions

View File

@ -0,0 +1,50 @@
package tui
import "github.com/charmbracelet/bubbles/key"
type KeyMappings struct {
Update key.Binding
SearchCriteria key.Binding
Search key.Binding
StorageOptions key.Binding
Find key.Binding
Quit key.Binding
}
func (k KeyMappings) ShortHelp() []key.Binding {
return []key.Binding{k.Update, k.SearchCriteria, k.Search, k.StorageOptions, k.Find, k.Quit}
}
func (k KeyMappings) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Update, k.SearchCriteria, k.Search},
{k.StorageOptions, k.Find, k.Quit},
}
}
var KeyMap = KeyMappings{
Update: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("u", "update database"),
),
SearchCriteria: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "card printing search criteria"),
),
Search: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "card printing search"),
),
StorageOptions: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "card storage options"),
),
Find: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "find in storage"),
),
Quit: key.NewBinding(
key.WithKeys("ctrl+c", "q"),
key.WithHelp("q", "quit program"),
),
}

View File

@ -2,56 +2,8 @@ package home
import ( import (
"github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
) )
type KeyMap struct {
Update key.Binding
SearchCriteria key.Binding
Search key.Binding
StorageOptions key.Binding
Find key.Binding
Quit key.Binding
}
func (k KeyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Update, k.SearchCriteria, k.Search, k.StorageOptions, k.Find, k.Quit}
}
func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Update, k.SearchCriteria, k.Search},
{k.StorageOptions, k.Find, k.Quit},
}
}
var DefaultKeyMap = KeyMap{
Update: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("u", "update database"),
),
SearchCriteria: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "card printing search criteria"),
),
Search: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "card printing search"),
),
StorageOptions: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "card storage options"),
),
Find: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "find in storage"),
),
Quit: key.NewBinding(
key.WithKeys("ctrl+c", "q"),
key.WithHelp("q", "quit program"),
),
}
type Model struct { type Model struct {
WindowHeight int WindowHeight int
WindowWidth int WindowWidth int

View File

@ -3,6 +3,8 @@ package home
import ( import (
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"sevenkeys/tui"
) )
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -16,7 +18,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil return m, nil
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
case key.Matches(msg, DefaultKeyMap.Quit): case key.Matches(msg, tui.KeyMap.Quit):
return m, tea.Quit return m, tea.Quit
} }
break break

View File

@ -3,6 +3,7 @@ package home
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"sevenkeys/tui"
"strings" "strings"
"github.com/lukesampson/figlet/figletlib" "github.com/lukesampson/figlet/figletlib"
@ -27,7 +28,7 @@ func (m Model) View() string {
// Display help // Display help
ui += strings.Repeat("\n", m.WindowHeight-8) // TODO: Avoid hardcoding height somehow ui += strings.Repeat("\n", m.WindowHeight-8) // TODO: Avoid hardcoding height somehow
ui += m.Help.View(DefaultKeyMap) ui += m.Help.View(tui.KeyMap)
return ui return ui
} }