Skip to content
Myr

fix(pager): memory/cpu usage when using soft-wrap (#827)

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

easily reproducible, especially with something like ```sh PAGER="gum pager" man ls ``` Problems here were: - reflow's truncate is not very efficient - useless calls to truncate - bad strings.Replace calls (not necessary, and wouldn't work if text has ansi sequences)

authored by Carlos Alexandro Becker · committed by GitHub · parent e7c916c · Browse files · 3 files+26 added−18 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+26 added−18 removed

  • other:+26 added−18 removed

3 files+26 added−18 removed

modified:pager/pager.go+14 added-10 removed
11 unmodified lines
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"16 "github.com/charmbracelet/x/ansi"17)1819type keymap struct {20 Home,
124 unmodified lines
145 line = strings.ReplaceAll(line, "\t", " ")146 if m.showLineNumbers {147 text.WriteString(m.lineNumberStyle.Render(fmt.Sprintf("%4d │ ", i+1)))148 }149 for m.softWrap && lipgloss.Width(line) > m.maxWidth {150 truncatedLine := truncate.String(line, uint(m.maxWidth)) //nolint: gosec151 text.WriteString(textStyle.Render(truncatedLine))152 text.WriteString("\n")153 if m.showLineNumbers {154 text.WriteString(m.lineNumberStyle.Render(" │ "))149 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")159 }156 line = strings.Replace(line, truncatedLine, "", 1)160 } else {161 text.WriteString(textStyle.Render(line)) //nolint: gosec162 text.WriteString("\n")163 }158 text.WriteString(textStyle.Render(truncate.String(line, uint(m.maxWidth)))) //nolint: gosec159 text.WriteString("\n")164 }165166 diffHeight := m.viewport.Height - lipgloss.Height(text.String())167 if diffHeight > 0 && m.showLineNumbers {
62 unmodified lines
modified:pager/search.go+11 added-7 removed
6 unmodified lines
78 "github.com/charmbracelet/bubbles/textinput"9 "github.com/charmbracelet/gum/internal/utils"10 "github.com/charmbracelet/lipgloss"11 "github.com/muesli/reflow/truncate"11 "github.com/charmbracelet/x/ansi"12)1314type search struct {15 active bool
133 unmodified lines
149150func softWrapEm(str string, maxWidth int, softWrap bool) string {151 var text strings.Builder152 for _, line := range strings.Split(str, "\n") {153 for softWrap && lipgloss.Width(line) > maxWidth {154 truncatedLine := truncate.String(line, uint(maxWidth)) //nolint: gosec155 text.WriteString(truncatedLine)153 idx := 0154 if w := ansi.StringWidth(line); softWrap && w > maxWidth {155 for w > idx {156 truncatedLine := ansi.Cut(line, idx, maxWidth+idx)157 idx += maxWidth158 text.WriteString(truncatedLine)159 text.WriteString("\n")160 }161 } else {162 text.WriteString(line) //nolint: gosec163 text.WriteString("\n")157 line = strings.Replace(line, truncatedLine, "", 1)164 }159 text.WriteString(truncate.String(line, uint(maxWidth))) //nolint: gosec160 text.WriteString("\n")165 }166167 return text.String()168}
modified:go.mod+1 added-1 removed
12 unmodified lines
13 github.com/charmbracelet/log v0.4.014 github.com/charmbracelet/x/ansi v0.8.015 github.com/charmbracelet/x/editor v0.1.016 github.com/charmbracelet/x/term v0.2.117 github.com/muesli/reflow v0.3.017 github.com/muesli/roff v0.1.018 github.com/muesli/termenv v0.15.3-0.20241211131612-0d230cb6eb1519 github.com/rivo/uniseg v0.4.720 github.com/sahilm/fuzzy v0.1.1
17 unmodified lines
38 github.com/microcosm-cc/bluemonday v1.0.27 // indirect39 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect40 github.com/muesli/cancelreader v0.2.2 // indirect41 github.com/muesli/mango v0.2.0 // indirect42 github.com/muesli/reflow v0.3.0 // indirect43 github.com/yuin/goldmark v1.7.4 // indirect44 github.com/yuin/goldmark-emoji v1.0.4 // indirect45 golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect46 golang.org/x/net v0.33.0 // indirect
4 unmodified lines