mirror of
https://github.com/anotherhadi/spilltea.git
synced 2026-05-20 09:42:34 +02:00
Change help menus: Only display shortcuts used on the page
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
@@ -5,8 +5,13 @@ import (
|
||||
|
||||
spilltea "github.com/anotherhadi/spilltea"
|
||||
|
||||
"charm.land/bubbles/v2/help"
|
||||
"charm.land/bubbles/v2/key"
|
||||
"charm.land/bubbles/v2/viewport"
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/anotherhadi/spilltea/internal/keys"
|
||||
"github.com/anotherhadi/spilltea/internal/style"
|
||||
)
|
||||
|
||||
func readDoc(name string) string {
|
||||
@@ -23,14 +28,51 @@ var contentMarkdown = strings.Join([]string{
|
||||
|
||||
type Model struct {
|
||||
viewport viewport.Model
|
||||
help help.Model
|
||||
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
func New() Model {
|
||||
return Model{
|
||||
viewport: viewport.New(),
|
||||
help: style.NewHelp(),
|
||||
}
|
||||
}
|
||||
|
||||
func (e Model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Model) SetSize(w, h int) {
|
||||
m.width = w
|
||||
m.height = h
|
||||
m.help.SetWidth(w - 2)
|
||||
|
||||
statusH := strings.Count(m.renderStatusBar(), "\n") + 1
|
||||
frameW := windowStyle().GetHorizontalFrameSize()
|
||||
frameH := windowStyle().GetVerticalFrameSize()
|
||||
|
||||
m.viewport.SetWidth(w - frameW)
|
||||
m.viewport.SetHeight(h - frameH - statusH)
|
||||
m.renderMarkdown()
|
||||
}
|
||||
|
||||
func (m *Model) renderStatusBar() string {
|
||||
return lipgloss.NewStyle().Padding(0, 1).Render(m.help.View(docsKeyMap{width: m.width}))
|
||||
}
|
||||
|
||||
type docsKeyMap struct{ width int }
|
||||
|
||||
func (docsKeyMap) ShortHelp() []key.Binding {
|
||||
g := keys.Keys.Global
|
||||
return []key.Binding{g.Up, g.Down, g.Help}
|
||||
}
|
||||
|
||||
func (m docsKeyMap) FullHelp() [][]key.Binding {
|
||||
g := keys.Keys.Global
|
||||
pageGlobals := []key.Binding{g.Up, g.Down, g.ScrollUp, g.ScrollDown}
|
||||
all := append(pageGlobals, g.CommonBindings()...)
|
||||
return keys.ChunkByWidth(all, m.width)
|
||||
}
|
||||
|
||||
@@ -35,16 +35,10 @@ func (e Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
step = 1
|
||||
}
|
||||
e.viewport.SetYOffset(e.viewport.YOffset() + step)
|
||||
case key.Matches(msg, g.Help):
|
||||
e.help.ShowAll = !e.help.ShowAll
|
||||
e.SetSize(e.width, e.height)
|
||||
}
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (m *Model) SetSize(w, h int) {
|
||||
frameW := windowStyle().GetHorizontalFrameSize()
|
||||
frameH := windowStyle().GetVerticalFrameSize()
|
||||
|
||||
m.viewport.SetWidth(w - frameW)
|
||||
m.viewport.SetHeight(h - frameH)
|
||||
m.renderMarkdown()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package docs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"text/template"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
@@ -20,7 +19,10 @@ func windowStyle() lipgloss.Style {
|
||||
}
|
||||
|
||||
func (e Model) View() tea.View {
|
||||
return tea.NewView(windowStyle().Render(e.viewport.View()))
|
||||
return tea.NewView(lipgloss.JoinVertical(lipgloss.Left,
|
||||
windowStyle().Render(e.viewport.View()),
|
||||
e.renderStatusBar(),
|
||||
))
|
||||
}
|
||||
|
||||
func (m *Model) renderMarkdown() {
|
||||
|
||||
Reference in New Issue
Block a user