pager.go
This file
Path
pager/pager.go
234 lines · 5.7 KiB · Go
Last changed by
chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
History of this fileTake it
The walk stopped after 200 commits: lines older than 6837ed2 point at it.
feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 1–9why, one step back1// Package pager provides a pager (similar to less) for the terminal.2//3// $ cat file.txt | gum pager4package pager56import (7 "fmt"8 "strings"9chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 10–15why, one step back10 "charm.land/bubbles/v2/help"11 "charm.land/bubbles/v2/key"12 "charm.land/bubbles/v2/textinput"13 "charm.land/bubbles/v2/viewport"14 tea "charm.land/bubbletea/v2"15 "charm.land/lipgloss/v2"fix(pager): memory/cpu usage when using soft-wrap (#827)Carlos Alexandro Beckerbefore adoptiond1fc051
line 16why, one step back16 "github.com/charmbracelet/x/ansi"feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 17–18why, one step back17)18fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
lines 19–40why, one step back19type 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"),feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 41why, one step back41 key.WithHelp("↓↑", "navigate"),fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
lines 42–90why, one step back42 ),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}90feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 91–94why, one step back91type model struct {92 content string93 origContent string94 viewport viewport.Modelfix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 95why, one step back95 help help.Modelfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 96–102why, one step back96 showLineNumbers bool97 lineNumberStyle lipgloss.Style98 softWrap bool99 search search100 matchStyle lipgloss.Style101 matchHighlightStyle lipgloss.Style102 maxWidth intfix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 103why, one step back103 keymap keymapfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 104–105why, one step back104}105feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 106why, one step back106func (m model) Init() tea.Cmd { return nil }feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 107–110why, one step back107108func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {109 switch msg := msg.(type) {110 case tea.WindowSizeMsg:feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
line 111why, one step back111 m.processText(msg)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 112why, one step back112 case tea.KeyPressMsg:feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
line 113why, one step back113 return m.keyHandler(msg)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 114–115why, one step back114 }115fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
lines 116–118why, one step back116 m.keymap.PrevMatch.SetEnabled(m.search.query != nil)117 m.keymap.NextMatch.SetEnabled(m.search.query != nil)118feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
lines 119–121why, one step back119 var cmd tea.Cmd120 m.search.input, cmd = m.search.input.Update(msg)121 return m, cmdfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 122–123why, one step back122}123fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 124why, one step back124func (m *model) helpView() string {fix(viewport): remove extra line in viewport help (#816)Carlos Alexandro Beckerbefore adoption7e3216e
line 125why, one step back125 return m.help.View(m.keymap)fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
lines 126–127why, one step back126}127feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
line 128why, one step back128func (m *model) processText(msg tea.WindowSizeMsg) {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 129–131why, one step back129 m.viewport.SetHeight(msg.Height - lipgloss.Height(m.helpView()))130 m.viewport.SetWidth(msg.Width)131 textStyle := lipgloss.NewStyle().Width(m.viewport.Width())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 132–134why, one step back132 var text strings.Builder133134 // Determine max width of a line.chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 135why, one step back135 m.maxWidth = m.viewport.Width()feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 136–148why, one step back136 if m.softWrap {137 vpStyle := m.viewport.Style138 m.maxWidth -= vpStyle.GetHorizontalBorderSize() + vpStyle.GetHorizontalMargins() + vpStyle.GetHorizontalPadding()139 if m.showLineNumbers {140 m.maxWidth -= lipgloss.Width(" │ ")141 }142 }143144 for i, line := range strings.Split(m.content, "\n") {145 line = strings.ReplaceAll(line, "\t", " ")146 if m.showLineNumbers {147 text.WriteString(m.lineNumberStyle.Render(fmt.Sprintf("%4d │ ", i+1)))148 }fix(pager): memory/cpu usage when using soft-wrap (#827)Carlos Alexandro Beckerbefore adoptiond1fc051
lines 149–158why, one step back149 idx := 0150 if w := ansi.StringWidth(line); m.softWrap && w > m.maxWidth {151 for w > idx {152 if m.showLineNumbers && idx != 0 {153 text.WriteString(m.lineNumberStyle.Render(" │ "))154 }155 truncatedLine := ansi.Cut(line, idx, m.maxWidth+idx)156 idx += m.maxWidth157 text.WriteString(textStyle.Render(truncatedLine))158 text.WriteString("\n")feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 159why, one step back159 }fix(pager): memory/cpu usage when using soft-wrap (#827)Carlos Alexandro Beckerbefore adoptiond1fc051
line 160why, one step back160 } else {fix: lint issues (#909)Carlos Alexandro Beckerbefore adoption817c4bd
line 161why, one step back161 text.WriteString(textStyle.Render(line))fix(pager): memory/cpu usage when using soft-wrap (#827)Carlos Alexandro Beckerbefore adoptiond1fc051
line 162why, one step back162 text.WriteString("\n")feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 163–165why, one step back163 }164 }165chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 166why, one step back166 diffHeight := m.viewport.Height() - lipgloss.Height(text.String())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 167–175why, one step back167 if diffHeight > 0 && m.showLineNumbers {168 remainingLines := " ~ │ " + strings.Repeat("\n ~ │ ", diffHeight-1)169 text.WriteString(m.lineNumberStyle.Render(remainingLines))170 }171 m.viewport.SetContent(text.String())172}173174const heightOffset = 2175chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 176why, one step back176func (m model) keyHandler(msg tea.KeyPressMsg) (model, tea.Cmd) {fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 177why, one step back177 km := m.keymapfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 178–179why, one step back178 var cmd tea.Cmd179 if m.search.active {fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
lines 180–181why, one step back180 switch {181 case key.Matches(msg, km.ConfirmSearch):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 182–187why, one step back182 if m.search.input.Value() != "" {183 m.content = m.origContent184 m.search.Execute(&m)185186 // Trigger a view update to highlight the found matches.187 m.search.NextMatch(&m)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 188why, one step back188 m.processText(tea.WindowSizeMsg{Height: m.viewport.Height() + heightOffset, Width: m.viewport.Width()})feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 189–191why, one step back189 } else {190 m.search.Done()191 }fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 192why, one step back192 case key.Matches(msg, km.CancelSearch):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 193–194why, one step back193 m.search.Done()194 default:fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 195why, one step back195 m.search.input, cmd = m.search.input.Update(msg)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 196–197why, one step back196 }197 } else {fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
lines 198–199why, one step back198 switch {199 case key.Matches(msg, km.Home):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 200why, one step back200 m.viewport.GotoTop()fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 201why, one step back201 case key.Matches(msg, km.End):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 202why, one step back202 m.viewport.GotoBottom()fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 203why, one step back203 case key.Matches(msg, km.Search):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 204why, one step back204 m.search.Begin()feat: handle focus/blur events (#749)Carlos Alexandro Beckerbefore adoption71d7e65
line 205why, one step back205 return m, textinput.Blinkfix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 206why, one step back206 case key.Matches(msg, km.PrevMatch):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 207why, one step back207 m.search.PrevMatch(&m)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 208why, one step back208 m.processText(tea.WindowSizeMsg{Height: m.viewport.Height() + heightOffset, Width: m.viewport.Width()})fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 209why, one step back209 case key.Matches(msg, km.NextMatch):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 210why, one step back210 m.search.NextMatch(&m)chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 211why, one step back211 m.processText(tea.WindowSizeMsg{Height: m.viewport.Height() + heightOffset, Width: m.viewport.Width()})fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 212why, one step back212 case key.Matches(msg, km.Quit):feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 213why, one step back213 return m, tea.Quitfix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 214why, one step back214 case key.Matches(msg, km.Abort):feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 215why, one step back215 return m, tea.Interruptfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 216why, one step back216 }fix(pager): use help bubble (#748)Carlos Alexandro Beckerbefore adoption2e2b020
line 217why, one step back217 m.viewport, cmd = m.viewport.Update(msg)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 218–222why, one step back218 }219220 return m, cmd221}222chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 223–226why, one step back223func (m model) View() tea.View {224 v := tea.NewView("")225 v.AltScreen = true226 v.ReportFocus = truefeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 227why, one step back227 if m.search.active {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 228–229why, one step back228 v.SetContent(m.viewport.View() + "\n " + m.search.input.View())229 return vfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 230–231why, one step back230 }231chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 232–233why, one step back232 v.SetContent(m.viewport.View() + "\n" + m.helpView())233 return vfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 234why, one step back234}