Skip to content

Commit

Permalink
fix(pager): use help bubble (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 authored Dec 9, 2024
1 parent fb543c3 commit 2e2b020
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 25 deletions.
4 changes: 3 additions & 1 deletion pager/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/stdin"
Expand Down Expand Up @@ -32,14 +33,15 @@ func (o Options) Run() error {

m := model{
viewport: vp,
helpStyle: o.HelpStyle.ToLipgloss(),
help: help.New(),
content: o.Content,
origContent: o.Content,
showLineNumbers: o.ShowLineNumbers,
lineNumberStyle: o.LineNumberStyle.ToLipgloss(),
softWrap: o.SoftWrap,
matchStyle: o.MatchStyle.ToLipgloss(),
matchHighlightStyle: o.MatchHighlightStyle.ToLipgloss(),
keymap: defaultKeymap(),
}

ctx, cancel := timeout.Context(o.Timeout)
Expand Down
4 changes: 3 additions & 1 deletion pager/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
type Options struct {
//nolint:staticcheck
Style style.Styles `embed:"" help:"Style the pager" set:"defaultBorder=rounded" set:"defaultPadding=0 1" set:"defaultBorderForeground=212" envprefix:"GUM_PAGER_"`
HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_"`
Content string `arg:"" optional:"" help:"Display content to scroll"`
ShowLineNumbers bool `help:"Show line numbers" default:"true"`
LineNumberStyle style.Styles `embed:"" prefix:"line-number." help:"Style the line numbers" set:"defaultForeground=237" envprefix:"GUM_PAGER_LINE_NUMBER_"`
SoftWrap bool `help:"Soft wrap lines" default:"false"`
MatchStyle style.Styles `embed:"" prefix:"match." help:"Style the matched text" set:"defaultForeground=212" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_"` //nolint:staticcheck
MatchHighlightStyle style.Styles `embed:"" prefix:"match-highlight." help:"Style the matched highlight text" set:"defaultForeground=235" set:"defaultBackground=225" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_HIGH_"` //nolint:staticcheck
Timeout time.Duration `help:"Timeout until command exits" default:"0s" env:"GUM_PAGER_TIMEOUT"`

// Deprecated: this has no effect anymore.
HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_" hidden:""`
}
123 changes: 100 additions & 23 deletions pager/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,100 @@ import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/truncate"
)

type keymap struct {
Home,
End,
Search,
NextMatch,
PrevMatch,
Abort,
Quit,
ConfirmSearch,
CancelSearch key.Binding
}

// FullHelp implements help.KeyMap.
func (k keymap) FullHelp() [][]key.Binding {
return nil
}

// ShortHelp implements help.KeyMap.
func (k keymap) ShortHelp() []key.Binding {
return []key.Binding{
key.NewBinding(
key.WithKeys("up", "down"),
key.WithHelp("↑/↓", "navigate"),
),
k.Quit,
k.Search,
k.NextMatch,
k.PrevMatch,
}
}

func defaultKeymap() keymap {
return keymap{
Home: key.NewBinding(
key.WithKeys("g", "home"),
key.WithHelp("h", "home"),
),
End: key.NewBinding(
key.WithKeys("G", "end"),
key.WithHelp("G", "end"),
),
Search: key.NewBinding(
key.WithKeys("/"),
key.WithHelp("/", "search"),
),
PrevMatch: key.NewBinding(
key.WithKeys("p", "N"),
key.WithHelp("N", "previous match"),
),
NextMatch: key.NewBinding(
key.WithKeys("n"),
key.WithHelp("n", "next match"),
),
Abort: key.NewBinding(
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "abort"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc"),
key.WithHelp("esc", "quit"),
),
ConfirmSearch: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "confirm"),
),
CancelSearch: key.NewBinding(
key.WithKeys("ctrl+c", "ctrl+d", "esc"),
key.WithHelp("ctrl+c", "cancel"),
),
}
}

type model struct {
content string
origContent string
viewport viewport.Model
helpStyle lipgloss.Style
help help.Model
showLineNumbers bool
lineNumberStyle lipgloss.Style
softWrap bool
search search
matchStyle lipgloss.Style
matchHighlightStyle lipgloss.Style
maxWidth int
keymap keymap
}

func (m model) Init() tea.Cmd { return nil }
Expand All @@ -38,13 +113,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.keyHandler(msg)
}

m.keymap.PrevMatch.SetEnabled(m.search.query != nil)
m.keymap.NextMatch.SetEnabled(m.search.query != nil)

var cmd tea.Cmd
m.search.input, cmd = m.search.input.Update(msg)
return m, cmd
}

func (m *model) helpView() string {
return "\n" + m.help.View(m.keymap)
}

func (m *model) processText(msg tea.WindowSizeMsg) {
m.viewport.Height = msg.Height - lipgloss.Height(m.helpStyle.Render("?")) - 1
m.viewport.Height = msg.Height - lipgloss.Height(m.helpView())
m.viewport.Width = msg.Width
textStyle := lipgloss.NewStyle().Width(m.viewport.Width)
var text strings.Builder
Expand Down Expand Up @@ -87,11 +169,12 @@ func (m *model) processText(msg tea.WindowSizeMsg) {

const heightOffset = 2

func (m model) keyHandler(key tea.KeyMsg) (model, tea.Cmd) {
func (m model) keyHandler(msg tea.KeyMsg) (model, tea.Cmd) {
km := m.keymap
var cmd tea.Cmd
if m.search.active {
switch key.String() {
case "enter":
switch {
case key.Matches(msg, km.ConfirmSearch):
if m.search.input.Value() != "" {
m.content = m.origContent
m.search.Execute(&m)
Expand All @@ -102,47 +185,41 @@ func (m model) keyHandler(key tea.KeyMsg) (model, tea.Cmd) {
} else {
m.search.Done()
}
case "ctrl+d", "ctrl+c", "esc":
case key.Matches(msg, km.CancelSearch):
m.search.Done()
default:
m.search.input, cmd = m.search.input.Update(key)
m.search.input, cmd = m.search.input.Update(msg)
}
} else {
switch key.String() {
case "g", "home":
switch {
case key.Matches(msg, km.Home):
m.viewport.GotoTop()
case "G", "end":
case key.Matches(msg, km.End):
m.viewport.GotoBottom()
case "/":
case key.Matches(msg, km.Search):
m.search.Begin()
return m, textinput.Blink
case "p", "N":
case key.Matches(msg, km.PrevMatch):
m.search.PrevMatch(&m)
m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
case "n":
case key.Matches(msg, km.NextMatch):
m.search.NextMatch(&m)
m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
case "q", "esc":
case key.Matches(msg, km.Quit):
return m, tea.Quit
case "ctrl+c":
case key.Matches(msg, km.Abort):
return m, tea.Interrupt
}
m.viewport, cmd = m.viewport.Update(key)
m.viewport, cmd = m.viewport.Update(msg)
}

return m, cmd
}

func (m model) View() string {
// TODO: use help bubble here
helpMsg := "\n ↑/↓: Navigate • q: Quit • /: Search "
if m.search.query != nil {
helpMsg += "• n: Next Match "
helpMsg += "• N: Prev Match "
}
if m.search.active {
return m.viewport.View() + "\n " + m.search.input.View()
}

return m.viewport.View() + m.helpStyle.Render(helpMsg)
return m.viewport.View() + m.helpView()
}

0 comments on commit 2e2b020

Please sign in to comment.