From e8361766b36060f5fcae35d9eecebdb4260ccf4c Mon Sep 17 00:00:00 2001 From: Samuel Recker Date: Mon, 16 Jan 2023 00:57:11 +0100 Subject: [PATCH] fix back hint in help views --- tea/scenes/installation.go | 15 +--------- tea/scenes/installations.go | 7 +---- tea/scenes/installed_mods.go | 5 +--- tea/scenes/main_menu.go | 1 - tea/scenes/mod.go | 16 +---------- tea/scenes/mod_version.go | 16 +---------- tea/scenes/mods.go | 9 +----- tea/scenes/new_installation.go | 26 ++++++++---------- tea/scenes/new_profile.go | 47 ++++++++++++++++++++++++++++---- tea/scenes/profile.go | 15 +--------- tea/scenes/profiles.go | 7 +---- tea/scenes/select_mod_version.go | 14 ---------- 12 files changed, 61 insertions(+), 117 deletions(-) diff --git a/tea/scenes/installation.go b/tea/scenes/installation.go index aab0db7..d4331fd 100644 --- a/tea/scenes/installation.go +++ b/tea/scenes/installation.go @@ -4,7 +4,6 @@ import ( "fmt" "time" - "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -66,19 +65,7 @@ func NewInstallation(root components.RootModel, parent tea.Model, installationDa model.list.Styles = utils.ListStyles model.list.SetSize(model.list.Width(), model.list.Height()) model.list.StatusMessageLifetime = time.Second * 3 - model.list.DisableQuitKeybindings() - - model.list.AdditionalShortHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } - - model.list.AdditionalFullHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } + model.list.KeyMap.Quit.SetHelp("q", "back") return model } diff --git a/tea/scenes/installations.go b/tea/scenes/installations.go index 2077b48..1a00802 100644 --- a/tea/scenes/installations.go +++ b/tea/scenes/installations.go @@ -28,12 +28,10 @@ func NewInstallations(root components.RootModel, parent tea.Model) tea.Model { l.Styles = utils.ListStyles l.SetSize(l.Width(), l.Height()) l.KeyMap.Quit.SetHelp("q", "back") - l.DisableQuitKeybindings() l.AdditionalShortHelpKeys = func() []key.Binding { return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - key.NewBinding(key.WithHelp("n", "new installation")), + key.NewBinding(key.WithKeys("n"), key.WithHelp("n", "new installation")), } } @@ -51,9 +49,6 @@ func (m installations) Init() tea.Cmd { } func (m installations) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - // List enables its own keybindings when they were previously disabled - m.list.DisableQuitKeybindings() - switch msg := msg.(type) { case tea.KeyMsg: if m.list.SettingFilter() { diff --git a/tea/scenes/installed_mods.go b/tea/scenes/installed_mods.go index dfe8b48..8331fcd 100644 --- a/tea/scenes/installed_mods.go +++ b/tea/scenes/installed_mods.go @@ -42,8 +42,6 @@ func NewInstalledMods(root components.RootModel, parent tea.Model) tea.Model { l.Title = "Installed Mods" l.Styles = utils.ListStyles l.SetSize(l.Width(), l.Height()) - l.KeyMap.Quit.SetHelp("q", "back") - l.DisableQuitKeybindings() l.AdditionalShortHelpKeys = func() []key.Binding { return []key.Binding{ @@ -160,8 +158,7 @@ func (m installedModsList) LoadModData() { } func (m installedModsList) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - // List enables its own keybindings when they were previously disabled - m.list.DisableQuitKeybindings() + m.list.KeyMap.Quit.SetHelp("q", "back") switch msg := msg.(type) { case tea.KeyMsg: diff --git a/tea/scenes/main_menu.go b/tea/scenes/main_menu.go index 640a6c0..107982f 100644 --- a/tea/scenes/main_menu.go +++ b/tea/scenes/main_menu.go @@ -127,7 +127,6 @@ func NewMainMenu(root components.RootModel) tea.Model { model.list.SetFilteringEnabled(false) model.list.Title = "Main Menu" model.list.Styles = utils.ListStyles - model.list.DisableQuitKeybindings() return model } diff --git a/tea/scenes/mod.go b/tea/scenes/mod.go index b44a017..aa0c18d 100644 --- a/tea/scenes/mod.go +++ b/tea/scenes/mod.go @@ -3,7 +3,6 @@ package scenes import ( "time" - "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -101,21 +100,8 @@ func NewModMenu(root components.RootModel, parent tea.Model, mod utils.Mod) tea. model.list.Title = mod.Name model.list.Styles = utils.ListStyles model.list.SetSize(model.list.Width(), model.list.Height()) - model.list.KeyMap.Quit.SetHelp("q", "back") model.list.StatusMessageLifetime = time.Second * 3 - model.list.DisableQuitKeybindings() - - model.list.AdditionalShortHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } - - model.list.AdditionalFullHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } + model.list.KeyMap.Quit.SetHelp("q", "back") return model } diff --git a/tea/scenes/mod_version.go b/tea/scenes/mod_version.go index df072c5..8025a48 100644 --- a/tea/scenes/mod_version.go +++ b/tea/scenes/mod_version.go @@ -3,7 +3,6 @@ package scenes import ( "time" - "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -67,21 +66,8 @@ func NewModVersion(root components.RootModel, parent tea.Model, mod utils.Mod) t model.list.Title = mod.Name model.list.Styles = utils.ListStyles model.list.SetSize(model.list.Width(), model.list.Height()) - model.list.KeyMap.Quit.SetHelp("q", "back") model.list.StatusMessageLifetime = time.Second * 3 - model.list.DisableQuitKeybindings() - - model.list.AdditionalShortHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } - - model.list.AdditionalFullHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } + model.list.KeyMap.Quit.SetHelp("q", "back") return model } diff --git a/tea/scenes/mods.go b/tea/scenes/mods.go index 4ded8d0..86e4f84 100644 --- a/tea/scenes/mods.go +++ b/tea/scenes/mods.go @@ -63,8 +63,6 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model { l.Title = modsTitle l.Styles = utils.ListStyles l.SetSize(l.Width(), l.Height()) - l.KeyMap.Quit.SetHelp("q", "back") - l.DisableQuitKeybindings() l.AdditionalShortHelpKeys = func() []key.Binding { return []key.Binding{ @@ -167,8 +165,6 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model { sortFieldList.Title = modsTitle sortFieldList.Styles = utils.ListStyles sortFieldList.SetSize(l.Width(), l.Height()) - sortFieldList.KeyMap.Quit.SetHelp("q", "back") - sortFieldList.DisableQuitKeybindings() sortOrderList := list.New([]list.Item{ utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{ @@ -200,8 +196,6 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model { sortOrderList.Title = modsTitle sortOrderList.Styles = utils.ListStyles sortOrderList.SetSize(l.Width(), l.Height()) - sortOrderList.KeyMap.Quit.SetHelp("q", "back") - sortOrderList.DisableQuitKeybindings() m := &modsList{ root: root, @@ -281,8 +275,7 @@ func (m modsList) Init() tea.Cmd { } func (m modsList) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - // List enables its own keybindings when they were previously disabled - m.list.DisableQuitKeybindings() + m.list.KeyMap.Quit.SetHelp("q", "back") switch msg := msg.(type) { case tea.KeyMsg: diff --git a/tea/scenes/new_installation.go b/tea/scenes/new_installation.go index cb2ffe8..679e4df 100644 --- a/tea/scenes/new_installation.go +++ b/tea/scenes/new_installation.go @@ -43,8 +43,6 @@ func NewNewInstallation(root components.RootModel, parent tea.Model) tea.Model { l.Styles = utils.ListStyles l.SetSize(l.Width(), l.Height()) l.KeyMap.Quit.SetHelp("esc", "back") - l.DisableQuitKeybindings() - l.SetShowHelp(false) l.SetShowStatusBar(false) model := newInstallation{ @@ -149,7 +147,8 @@ func (m newInstallation) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m newInstallation) View() string { - inputView := lipgloss.NewStyle().Padding(1, 2).Render(m.input.View()) + style := lipgloss.NewStyle().Padding(1, 2) + inputView := style.Render(m.input.View()) mandatory := lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, inputView) @@ -157,19 +156,18 @@ func (m newInstallation) View() string { return lipgloss.JoinVertical(lipgloss.Left, mandatory, m.error.View()) } - if len(m.dirList.Items()) > 0 { - m.dirList.SetSize(m.dirList.Width(), m.root.Size().Height-lipgloss.Height(mandatory)-1) - return lipgloss.JoinVertical(lipgloss.Left, mandatory, m.dirList.View()) + if len(m.dirList.Items()) == 0 { + infoBox := lipgloss.NewStyle(). + BorderStyle(lipgloss.ThickBorder()). + BorderForeground(lipgloss.Color("39")). + Padding(0, 1). + Margin(0, 0, 0, 2). + Render("Enter the path to the satisfactory installation") + mandatory = lipgloss.JoinVertical(lipgloss.Left, mandatory, infoBox) } - infoBox := lipgloss.NewStyle(). - BorderStyle(lipgloss.ThickBorder()). - BorderForeground(lipgloss.Color("39")). - Padding(0, 1). - Margin(0, 0, 0, 2). - Render("Enter the path to the satisfactory installation") - - return lipgloss.JoinVertical(lipgloss.Left, mandatory, infoBox) + m.dirList.SetSize(m.dirList.Width(), m.root.Size().Height-lipgloss.Height(mandatory)-1) + return lipgloss.JoinVertical(lipgloss.Left, mandatory, style.Render(m.dirList.View())) } // I know this is awful, but beats re-implementing the entire list model diff --git a/tea/scenes/new_profile.go b/tea/scenes/new_profile.go index 458e1d2..945b94e 100644 --- a/tea/scenes/new_profile.go +++ b/tea/scenes/new_profile.go @@ -3,6 +3,8 @@ package scenes import ( "time" + "github.com/charmbracelet/bubbles/help" + "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -13,12 +15,30 @@ import ( var _ tea.Model = (*newProfile)(nil) +type keyMap struct { + Back key.Binding + Quit key.Binding + Enter key.Binding +} + +func (k keyMap) ShortHelp() []key.Binding { + return []key.Binding{k.Enter, k.Back} +} + +func (k keyMap) FullHelp() [][]key.Binding { + return [][]key.Binding{ + {k.Enter, k.Back}, // second column + } +} + type newProfile struct { input textinput.Model root components.RootModel parent tea.Model error *components.ErrorComponent title string + help help.Model + keys keyMap } func NewNewProfile(root components.RootModel, parent tea.Model) tea.Model { @@ -27,6 +47,20 @@ func NewNewProfile(root components.RootModel, parent tea.Model) tea.Model { parent: parent, input: textinput.New(), title: utils.NonListTitleStyle.Render("New Profile"), + help: help.New(), + keys: keyMap{ + Back: key.NewBinding( + key.WithKeys(KeyEscape, KeyControlC), + key.WithHelp(KeyEscape, "back"), + ), + Enter: key.NewBinding( + key.WithKeys(KeyEnter), + key.WithHelp(KeyEnter, "create"), + ), + Quit: key.NewBinding( + key.WithKeys(KeyControlC), + ), + }, } model.input.Focus() @@ -42,12 +76,12 @@ func (m newProfile) Init() tea.Cmd { func (m newProfile) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: - switch keypress := msg.String(); keypress { - case KeyControlC: + switch { + case key.Matches(msg, m.keys.Quit): return m, tea.Quit - case KeyEscape: + case key.Matches(msg, m.keys.Back): return m.parent, nil - case KeyEnter: + case key.Matches(msg, m.keys.Enter): if _, err := m.root.GetGlobal().Profiles.AddProfile(m.input.Value()); err != nil { errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5) m.error = errorComponent @@ -74,7 +108,8 @@ func (m newProfile) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m newProfile) View() string { - inputView := lipgloss.NewStyle().Padding(1, 2).Render(m.input.View()) + style := lipgloss.NewStyle().Padding(1, 2) + inputView := style.Render(m.input.View()) if m.error != nil { return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, m.error.View(), inputView) @@ -87,5 +122,5 @@ func (m newProfile) View() string { Margin(0, 0, 0, 2). Render("Enter the name of the profile") - return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, inputView, infoBox) + return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, inputView, infoBox, style.Render(m.help.View(m.keys))) } diff --git a/tea/scenes/profile.go b/tea/scenes/profile.go index 2e64a1a..f45535c 100644 --- a/tea/scenes/profile.go +++ b/tea/scenes/profile.go @@ -4,7 +4,6 @@ import ( "fmt" "time" - "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -78,19 +77,7 @@ func NewProfile(root components.RootModel, parent tea.Model, profileData *cli.Pr model.list.Styles = utils.ListStyles model.list.SetSize(model.list.Width(), model.list.Height()) model.list.StatusMessageLifetime = time.Second * 3 - model.list.DisableQuitKeybindings() - - model.list.AdditionalShortHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } - - model.list.AdditionalFullHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } + model.list.KeyMap.Quit.SetHelp("q", "back") return model } diff --git a/tea/scenes/profiles.go b/tea/scenes/profiles.go index 9478871..1bd63b0 100644 --- a/tea/scenes/profiles.go +++ b/tea/scenes/profiles.go @@ -28,12 +28,10 @@ func NewProfiles(root components.RootModel, parent tea.Model) tea.Model { l.Styles = utils.ListStyles l.SetSize(l.Width(), l.Height()) l.KeyMap.Quit.SetHelp("q", "back") - l.DisableQuitKeybindings() l.AdditionalShortHelpKeys = func() []key.Binding { return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - key.NewBinding(key.WithHelp("n", "new profile")), + key.NewBinding(key.WithKeys("n"), key.WithHelp("n", "new profile")), } } @@ -51,9 +49,6 @@ func (m profiles) Init() tea.Cmd { } func (m profiles) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - // List enables its own keybindings when they were previously disabled - m.list.DisableQuitKeybindings() - switch msg := msg.(type) { case tea.KeyMsg: if m.list.SettingFilter() { diff --git a/tea/scenes/select_mod_version.go b/tea/scenes/select_mod_version.go index 71ced35..23897c7 100644 --- a/tea/scenes/select_mod_version.go +++ b/tea/scenes/select_mod_version.go @@ -5,7 +5,6 @@ import ( "fmt" "time" - "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" @@ -36,19 +35,6 @@ func NewModVersionList(root components.RootModel, parent tea.Model, mod utils.Mo l.Styles = utils.ListStyles l.SetSize(l.Width(), l.Height()) l.KeyMap.Quit.SetHelp("q", "back") - l.DisableQuitKeybindings() - - l.AdditionalShortHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } - - l.AdditionalFullHelpKeys = func() []key.Binding { - return []key.Binding{ - key.NewBinding(key.WithHelp("q", "back")), - } - } m := &selectModVersionList{ root: root,