Skip to content
Myr

fix(pager): use help bubble (#748)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Carlos Alexandro Becker · committed by GitHub · parent fb543c3 · Browse files · 3 files+106 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+106 added−25 removed

  • other:+106 added−25 removed

3 files+106 added−25 removed

modified:pager/command.go+3 added-1 removed
2 unmodified lines
3import (4 "fmt"5 "regexp"67 "github.com/charmbracelet/bubbles/help"8 "github.com/charmbracelet/bubbles/viewport"9 tea "github.com/charmbracelet/bubbletea"10 "github.com/charmbracelet/gum/internal/stdin"11 "github.com/charmbracelet/gum/internal/timeout"
20 unmodified lines
32 }3334 m := model{35 viewport: vp,35 helpStyle: o.HelpStyle.ToLipgloss(),36 help: help.New(),37 content: o.Content,38 origContent: o.Content,39 showLineNumbers: o.ShowLineNumbers,40 lineNumberStyle: o.LineNumberStyle.ToLipgloss(),41 softWrap: o.SoftWrap,42 matchStyle: o.MatchStyle.ToLipgloss(),43 matchHighlightStyle: o.MatchHighlightStyle.ToLipgloss(),44 keymap: defaultKeymap(),45 }4647 ctx, cancel := timeout.Context(o.Timeout)48 defer cancel()
13 unmodified lines
modified:pager/options.go+3 added-1 removed
8 unmodified lines
9// Options are the options for the pager.10type Options struct {11 //nolint:staticcheck12 Style style.Styles `embed:"" help:"Style the pager" set:"defaultBorder=rounded" set:"defaultPadding=0 1" set:"defaultBorderForeground=212" envprefix:"GUM_PAGER_"`13 HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_"`13 Content string `arg:"" optional:"" help:"Display content to scroll"`14 ShowLineNumbers bool `help:"Show line numbers" default:"true"`15 LineNumberStyle style.Styles `embed:"" prefix:"line-number." help:"Style the line numbers" set:"defaultForeground=237" envprefix:"GUM_PAGER_LINE_NUMBER_"`16 SoftWrap bool `help:"Soft wrap lines" default:"false"`17 MatchStyle style.Styles `embed:"" prefix:"match." help:"Style the matched text" set:"defaultForeground=212" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_"` //nolint:staticcheck18 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:staticcheck19 Timeout time.Duration `help:"Timeout until command exits" default:"0s" env:"GUM_PAGER_TIMEOUT"`2021 // Deprecated: this has no effect anymore.22 HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_" hidden:""`23}
modified:pager/pager.go+100 added-23 removed
5 unmodified lines
6import (7 "fmt"8 "strings"910 "github.com/charmbracelet/bubbles/help"11 "github.com/charmbracelet/bubbles/key"12 "github.com/charmbracelet/bubbles/textinput"13 "github.com/charmbracelet/bubbles/viewport"14 tea "github.com/charmbracelet/bubbletea"15 "github.com/charmbracelet/lipgloss"16 "github.com/muesli/reflow/truncate"17)1819type keymap struct {20 Home,21 End,22 Search,23 NextMatch,24 PrevMatch,25 Abort,26 Quit,27 ConfirmSearch,28 CancelSearch key.Binding29}3031// FullHelp implements help.KeyMap.32func (k keymap) FullHelp() [][]key.Binding {33 return nil34}3536// ShortHelp implements help.KeyMap.37func (k keymap) ShortHelp() []key.Binding {38 return []key.Binding{39 key.NewBinding(40 key.WithKeys("up", "down"),41 key.WithHelp("↑/↓", "navigate"),42 ),43 k.Quit,44 k.Search,45 k.NextMatch,46 k.PrevMatch,47 }48}4950func defaultKeymap() keymap {51 return keymap{52 Home: key.NewBinding(53 key.WithKeys("g", "home"),54 key.WithHelp("h", "home"),55 ),56 End: key.NewBinding(57 key.WithKeys("G", "end"),58 key.WithHelp("G", "end"),59 ),60 Search: key.NewBinding(61 key.WithKeys("/"),62 key.WithHelp("/", "search"),63 ),64 PrevMatch: key.NewBinding(65 key.WithKeys("p", "N"),66 key.WithHelp("N", "previous match"),67 ),68 NextMatch: key.NewBinding(69 key.WithKeys("n"),70 key.WithHelp("n", "next match"),71 ),72 Abort: key.NewBinding(73 key.WithKeys("ctrl+c"),74 key.WithHelp("ctrl+c", "abort"),75 ),76 Quit: key.NewBinding(77 key.WithKeys("q", "esc"),78 key.WithHelp("esc", "quit"),79 ),80 ConfirmSearch: key.NewBinding(81 key.WithKeys("enter"),82 key.WithHelp("enter", "confirm"),83 ),84 CancelSearch: key.NewBinding(85 key.WithKeys("ctrl+c", "ctrl+d", "esc"),86 key.WithHelp("ctrl+c", "cancel"),87 ),88 }89}9091type model struct {92 content string93 origContent string94 viewport viewport.Model21 helpStyle lipgloss.Style95 help help.Model96 showLineNumbers bool97 lineNumberStyle lipgloss.Style98 softWrap bool99 search search100 matchStyle lipgloss.Style101 matchHighlightStyle lipgloss.Style102 maxWidth int103 keymap keymap104}105106func (m model) Init() tea.Cmd { return nil }107
4 unmodified lines
112 case tea.KeyMsg:113 return m.keyHandler(msg)114 }115116 m.keymap.PrevMatch.SetEnabled(m.search.query != nil)117 m.keymap.NextMatch.SetEnabled(m.search.query != nil)118119 var cmd tea.Cmd120 m.search.input, cmd = m.search.input.Update(msg)121 return m, cmd122}123124func (m *model) helpView() string {125 return "\n" + m.help.View(m.keymap)126}127128func (m *model) processText(msg tea.WindowSizeMsg) {47 m.viewport.Height = msg.Height - lipgloss.Height(m.helpStyle.Render("?")) - 1129 m.viewport.Height = msg.Height - lipgloss.Height(m.helpView())130 m.viewport.Width = msg.Width131 textStyle := lipgloss.NewStyle().Width(m.viewport.Width)132 var text strings.Builder133
34 unmodified lines
168}169170const heightOffset = 217190func (m model) keyHandler(key tea.KeyMsg) (model, tea.Cmd) {172func (m model) keyHandler(msg tea.KeyMsg) (model, tea.Cmd) {173 km := m.keymap174 var cmd tea.Cmd175 if m.search.active {93 switch key.String() {94 case "enter":176 switch {177 case key.Matches(msg, km.ConfirmSearch):178 if m.search.input.Value() != "" {179 m.content = m.origContent180 m.search.Execute(&m)181
2 unmodified lines
184 m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})185 } else {186 m.search.Done()187 }105 case "ctrl+d", "ctrl+c", "esc":188 case key.Matches(msg, km.CancelSearch):189 m.search.Done()190 default:108 m.search.input, cmd = m.search.input.Update(key)191 m.search.input, cmd = m.search.input.Update(msg)192 }193 } else {111 switch key.String() {112 case "g", "home":194 switch {195 case key.Matches(msg, km.Home):196 m.viewport.GotoTop()114 case "G", "end":197 case key.Matches(msg, km.End):198 m.viewport.GotoBottom()116 case "/":199 case key.Matches(msg, km.Search):200 m.search.Begin()201 return m, textinput.Blink119 case "p", "N":202 case key.Matches(msg, km.PrevMatch):203 m.search.PrevMatch(&m)204 m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})122 case "n":205 case key.Matches(msg, km.NextMatch):206 m.search.NextMatch(&m)207 m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})125 case "q", "esc":208 case key.Matches(msg, km.Quit):209 return m, tea.Quit127 case "ctrl+c":210 case key.Matches(msg, km.Abort):211 return m, tea.Interrupt212 }130 m.viewport, cmd = m.viewport.Update(key)213 m.viewport, cmd = m.viewport.Update(msg)214 }215216 return m, cmd217}218219func (m model) View() string {137 // TODO: use help bubble here138 helpMsg := "\n ↑/↓: Navigate • q: Quit • /: Search "139 if m.search.query != nil {140 helpMsg += "• n: Next Match "141 helpMsg += "• N: Prev Match "142 }220 if m.search.active {221 return m.viewport.View() + "\n " + m.search.input.View()222 }223147 return m.viewport.View() + m.helpStyle.Render(helpMsg)224 return m.viewport.View() + m.helpView()225}