Add keybindings and help menu

This commit is contained in:
The Magician 2024-06-05 14:13:29 +01:00
parent 4126b3d963
commit 31d1664619
3 changed files with 84 additions and 13 deletions

View File

@ -1,10 +1,69 @@
package ui package ui
import (
"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
Help help.Model
} }
func NewModel() Model { func NewModel() Model {
return Model{} help := help.New()
help.ShortSeparator = help.FullSeparator
return Model{
Help: help,
}
} }

View File

@ -1,6 +1,9 @@
package ui package ui
import tea "github.com/charmbracelet/bubbletea" import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
@ -9,10 +12,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.WindowHeight = msg.Height m.WindowHeight = msg.Height
m.WindowWidth = msg.Width m.WindowWidth = msg.Width
m.Help.Width = msg.Width
return m, nil return m, nil
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch {
case "ctrl+c": case key.Matches(msg, DefaultKeyMap.Quit):
return m, tea.Quit return m, tea.Quit
} }
break break

View File

@ -3,6 +3,7 @@ package ui
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/lukesampson/figlet/figletlib" "github.com/lukesampson/figlet/figletlib"
) )
@ -10,16 +11,23 @@ import (
func (m Model) View() string { func (m Model) View() string {
var ui string var ui string
if m.WindowWidth > 0 { if m.WindowWidth <= 0 {
cwd, _ := os.Getwd() return ui
fontsdir := filepath.Join(cwd, "fonts")
slantFont, _ := figletlib.GetFontByName(fontsdir, "slant")
termFont, _ := figletlib.GetFontByName(fontsdir, "term")
ui += figletlib.SprintMsg("SEVENKEYS", slantFont, m.WindowWidth, slantFont.Settings(), "center")
ui += figletlib.SprintMsg("the ultimate Magic: the Gathering trading card storage system", termFont, m.WindowWidth, termFont.Settings(), "center")
} }
// Get fonts for figlet
cwd, _ := os.Getwd()
fontsdir := filepath.Join(cwd, "fonts")
slantFont, _ := figletlib.GetFontByName(fontsdir, "slant")
termFont, _ := figletlib.GetFontByName(fontsdir, "term")
// Display splash screen
ui += figletlib.SprintMsg("SEVENKEYS", slantFont, m.WindowWidth, slantFont.Settings(), "center")
ui += figletlib.SprintMsg("the ultimate Magic: the Gathering trading card storage system", termFont, m.WindowWidth, termFont.Settings(), "center")
// Display help
ui += strings.Repeat("\n", m.WindowHeight-8) // TODO: Avoid hardcoding height somehow
ui += m.Help.View(DefaultKeyMap)
return ui return ui
} }