diff --git a/internal/keys/global.go b/internal/keys/global.go index b6ad90e..2d704c5 100644 --- a/internal/keys/global.go +++ b/internal/keys/global.go @@ -54,3 +54,8 @@ func (g GlobalKeyMap) Bindings() []key.Binding { g.ScrollUp, g.ScrollDown, } } + +// CommonBindings returns keys available on every page. +func (g GlobalKeyMap) CommonBindings() []key.Binding { + return []key.Binding{g.Quit, g.Help, g.OpenLogs, g.ToggleSidebar} +} diff --git a/internal/ui/app/update.go b/internal/ui/app/update.go index d98b934..376b417 100644 --- a/internal/ui/app/update.go +++ b/internal/ui/app/update.go @@ -176,22 +176,24 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if !m.activeIsEditing() { switch { case key.Matches(msg, keys.Keys.Global.CopyAs): - if m.page == pageDiff { - if raw := m.diff.CurrentRaw(); raw != "" { - m.copyAs.SetSize(m.width, m.height) - m.copyAs.Open(copyasUI.OpenMsg{ - RawRequest: raw, - Scheme: "https", - }) - } - } else if m.page == pageIntercept { - if raw := m.intercept.CurrentRaw(); raw != "" { - m.copyAs.SetSize(m.width, m.height) - m.copyAs.Open(copyasUI.OpenMsg{ - RawRequest: raw, - Scheme: m.intercept.CurrentScheme(), - }) - } + var raw, scheme string + switch m.page { + case pageDiff: + raw = m.diff.CurrentRaw() + scheme = "https" + case pageIntercept: + raw = m.intercept.CurrentRaw() + scheme = m.intercept.CurrentScheme() + case pageHistory: + raw = m.history.CurrentRaw() + scheme = m.history.CurrentScheme() + case pageReplay: + raw = m.replay.CurrentRaw() + scheme = m.replay.CurrentScheme() + } + if raw != "" { + m.copyAs.SetSize(m.width, m.height) + m.copyAs.Open(copyasUI.OpenMsg{RawRequest: raw, Scheme: scheme}) } return m, nil diff --git a/internal/ui/diff/model.go b/internal/ui/diff/model.go index cdfa55e..95f93d7 100644 --- a/internal/ui/diff/model.go +++ b/internal/ui/diff/model.go @@ -243,14 +243,6 @@ func lcsAlignedDiff(a, b, aHL, bHL []string) (left, right []diffLine) { return left, right } -func diffBindings() []key.Binding { - g := keys.Keys.Global - return []key.Binding{ - g.Up, g.Down, g.ScrollUp, g.ScrollDown, - g.CycleFocus, keys.Keys.Diff.Clear, - } -} - type diffKeyMap struct{ width int } func (diffKeyMap) ShortHelp() []key.Binding { @@ -259,6 +251,9 @@ func (diffKeyMap) ShortHelp() []key.Binding { } func (m diffKeyMap) FullHelp() [][]key.Binding { - all := append(diffBindings(), keys.Keys.Global.Bindings()...) + g := keys.Keys.Global + pageGlobals := []key.Binding{g.Up, g.Down, g.CycleFocus, g.ScrollUp, g.ScrollDown, g.Left, g.Right, g.Copy, g.CopyAs} + all := append(keys.Keys.Diff.Bindings(), pageGlobals...) + all = append(all, g.CommonBindings()...) return keys.ChunkByWidth(all, m.width) } diff --git a/internal/ui/docs/model.go b/internal/ui/docs/model.go index ea4dca4..b1d83a0 100644 --- a/internal/ui/docs/model.go +++ b/internal/ui/docs/model.go @@ -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) +} diff --git a/internal/ui/docs/update.go b/internal/ui/docs/update.go index ba04ca4..cdbe6cb 100644 --- a/internal/ui/docs/update.go +++ b/internal/ui/docs/update.go @@ -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() -} diff --git a/internal/ui/docs/view.go b/internal/ui/docs/view.go index d1f2e76..0895b87 100644 --- a/internal/ui/docs/view.go +++ b/internal/ui/docs/view.go @@ -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() { diff --git a/internal/ui/findings/model.go b/internal/ui/findings/model.go index 4f0498b..764a56c 100644 --- a/internal/ui/findings/model.go +++ b/internal/ui/findings/model.go @@ -81,7 +81,7 @@ func (m *Model) recalcSizes() { } func (m *Model) renderStatusBar() string { - return lipgloss.NewStyle().Padding(0, 1).Render(m.help.View(findingsKeyMap{})) + return lipgloss.NewStyle().Padding(0, 1).Render(m.help.View(findingsKeyMap{width: m.width})) } // RefreshCmd loads findings from the database. @@ -143,14 +143,18 @@ func renderMarkdown(src string, width int) string { return out } -type findingsKeyMap struct{} +type findingsKeyMap struct{ width int } func (findingsKeyMap) ShortHelp() []key.Binding { g := keys.Keys.Global f := keys.Keys.Findings - return []key.Binding{g.Up, g.Down, f.Dismiss} + return []key.Binding{g.Up, g.Down, f.Dismiss, g.Help} } -func (findingsKeyMap) FullHelp() [][]key.Binding { - return [][]key.Binding{findingsKeyMap{}.ShortHelp()} +func (m findingsKeyMap) FullHelp() [][]key.Binding { + g := keys.Keys.Global + pageGlobals := []key.Binding{g.Up, g.Down, g.ScrollUp, g.ScrollDown} + all := append(keys.Keys.Findings.Bindings(), pageGlobals...) + all = append(all, g.CommonBindings()...) + return keys.ChunkByWidth(all, m.width) } diff --git a/internal/ui/findings/update.go b/internal/ui/findings/update.go index f20920c..e44538a 100644 --- a/internal/ui/findings/update.go +++ b/internal/ui/findings/update.go @@ -76,6 +76,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { step = 1 } m.bodyViewport.SetYOffset(m.bodyViewport.YOffset() + step) + case key.Matches(msg, g.Help): + m.help.ShowAll = !m.help.ShowAll + m.recalcSizes() } } return m, nil diff --git a/internal/ui/history/model.go b/internal/ui/history/model.go index fb924ea..e793760 100644 --- a/internal/ui/history/model.go +++ b/internal/ui/history/model.go @@ -152,7 +152,10 @@ func (historyKeyMap) ShortHelp() []key.Binding { func (m historyKeyMap) FullHelp() [][]key.Binding { h := keys.Keys.History + g := keys.Keys.Global + pageGlobals := []key.Binding{g.Up, g.Down, g.CycleFocus, g.ScrollUp, g.ScrollDown, g.Left, g.Right, g.Escape, g.SendToReplay, g.SendToDiff, g.Copy, g.CopyAs} all := []key.Binding{h.DeleteEntry, h.DeleteAll, h.Filter, h.SqlQuery} - all = append(all, keys.Keys.Global.Bindings()...) + all = append(all, pageGlobals...) + all = append(all, g.CommonBindings()...) return keys.ChunkByWidth(all, m.width) } diff --git a/internal/ui/intercept/keymap.go b/internal/ui/intercept/keymap.go index ca7513d..2684220 100644 --- a/internal/ui/intercept/keymap.go +++ b/internal/ui/intercept/keymap.go @@ -29,6 +29,9 @@ func (interceptKeyMap) ShortHelp() []key.Binding { } func (m interceptKeyMap) FullHelp() [][]key.Binding { - all := append(keys.Keys.Intercept.Bindings(), keys.Keys.Global.Bindings()...) + g := keys.Keys.Global + pageGlobals := []key.Binding{g.Up, g.Down, g.CycleFocus, g.ScrollUp, g.ScrollDown, g.Left, g.Right, g.Escape, g.SendToReplay, g.SendToDiff, g.Copy, g.CopyAs} + all := append(keys.Keys.Intercept.Bindings(), pageGlobals...) + all = append(all, g.CommonBindings()...) return keys.ChunkByWidth(all, m.width) } diff --git a/internal/ui/plugins/model.go b/internal/ui/plugins/model.go index 5eeed46..aff98aa 100644 --- a/internal/ui/plugins/model.go +++ b/internal/ui/plugins/model.go @@ -196,6 +196,7 @@ func (m *Model) refreshListViewport() { type pluginsKeyMap struct { editing bool hasConfig bool + width int } func (k pluginsKeyMap) ShortHelp() []key.Binding { @@ -210,11 +211,20 @@ func (k pluginsKeyMap) ShortHelp() []key.Binding { key.WithHelp(g.ScrollUp.Help().Key+"/"+g.ScrollDown.Help().Key, "scroll detail"), ) if k.hasConfig { - return []key.Binding{pk.Toggle, pk.EditConfig, pk.Filter, scrollHint} + return []key.Binding{pk.Toggle, pk.EditConfig, pk.Filter, scrollHint, g.Help} } - return []key.Binding{pk.Toggle, pk.Filter, scrollHint} + return []key.Binding{pk.Toggle, pk.Filter, scrollHint, g.Help} } func (k pluginsKeyMap) FullHelp() [][]key.Binding { - return [][]key.Binding{k.ShortHelp()} + g := keys.Keys.Global + if k.editing { + return [][]key.Binding{k.ShortHelp()} + } + pk := keys.Keys.Plugins + pageGlobals := []key.Binding{g.Up, g.Down, g.ScrollUp, g.ScrollDown, g.Escape} + all := []key.Binding{pk.Toggle, pk.EditConfig, pk.Filter} + all = append(all, pageGlobals...) + all = append(all, g.CommonBindings()...) + return keys.ChunkByWidth(all, k.width) } diff --git a/internal/ui/plugins/view.go b/internal/ui/plugins/view.go index e022760..505759f 100644 --- a/internal/ui/plugins/view.go +++ b/internal/ui/plugins/view.go @@ -120,9 +120,9 @@ func (m *Model) renderStatusBar() string { escKey := keys.Keys.Global.Escape.Help().Key accent := lipgloss.NewStyle().Foreground(s.Primary) filterLine := pad.Render(accent.Render(filterKey) + " " + s.Bold.Render(m.filter) + s.Faint.Render(" "+escKey+" to clear")) - return lipgloss.JoinVertical(lipgloss.Left, filterLine, pad.Render(m.help.View(pluginsKeyMap{editing: m.editing, hasConfig: m.hasConfig()}))) + return lipgloss.JoinVertical(lipgloss.Left, filterLine, pad.Render(m.help.View(pluginsKeyMap{editing: m.editing, hasConfig: m.hasConfig(), width: m.width}))) } - return pad.Render(m.help.View(pluginsKeyMap{editing: m.editing, hasConfig: m.hasConfig()})) + return pad.Render(m.help.View(pluginsKeyMap{editing: m.editing, hasConfig: m.hasConfig(), width: m.width})) } func (m *Model) renderList() string { diff --git a/internal/ui/replay/model.go b/internal/ui/replay/model.go index 21cbaef..62ec9a8 100644 --- a/internal/ui/replay/model.go +++ b/internal/ui/replay/model.go @@ -187,6 +187,9 @@ func (replayKeyMap) ShortHelp() []key.Binding { } func (m replayKeyMap) FullHelp() [][]key.Binding { - all := append(keys.Keys.Replay.Bindings(), keys.Keys.Global.Bindings()...) + g := keys.Keys.Global + pageGlobals := []key.Binding{g.Up, g.Down, g.ScrollUp, g.ScrollDown, g.Left, g.Right, g.Escape, g.Copy, g.CopyAs} + all := append(keys.Keys.Replay.Bindings(), pageGlobals...) + all = append(all, g.CommonBindings()...) return keys.ChunkByWidth(all, m.width) }