Skip to content

Commit 95541c2

Browse files
committed
refactor!: viewport: remove deprecated HighPerformanceRendering
This breaks the API and removes high performance rendering and its related methods from the viewport package. It changes the API and makes the methods return nothing instead of lines to render.
1 parent f81fd52 commit 95541c2

File tree

3 files changed

+31
-148
lines changed

3 files changed

+31
-148
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/MakeNowJust/heredoc v1.0.0
77
github.com/atotto/clipboard v0.1.4
8-
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1
8+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20240919172237-265996c29bea
99
github.com/charmbracelet/harmonica v0.2.0
1010
github.com/charmbracelet/lipgloss v0.13.0
1111
github.com/charmbracelet/x/ansi v0.3.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp
88
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
99
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1 h1:OZtpLCsuuPplC+1oyUo+/eAN7e9MC2UyZWKlKrVlUnw=
1010
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1/go.mod h1:j0gn4ft5CE7NDYNZjAA3hBM8t2OPjI8urxuAD0oR4w8=
11+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20240919172237-265996c29bea h1:i32Z8pIQujNjR2BffDviAnai2L9oLMW7jMd7aCD8Jqg=
12+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20240919172237-265996c29bea/go.mod h1:j0gn4ft5CE7NDYNZjAA3hBM8t2OPjI8urxuAD0oR4w8=
1113
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
1214
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
1315
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=

viewport/viewport.go

Lines changed: 28 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ type Model struct {
4242
// useful for setting borders, margins and padding.
4343
Style lipgloss.Style
4444

45-
// HighPerformanceRendering bypasses the normal Bubble Tea renderer to
46-
// provide higher performance rendering. Most of the time the normal Bubble
47-
// Tea rendering methods will suffice, but if you're passing content with
48-
// a lot of ANSI escape codes you may see improved rendering in certain
49-
// terminals with this enabled.
50-
//
51-
// This should only be used in program occupying the entire terminal,
52-
// which is usually via the alternate screen buffer.
53-
//
54-
// Deprecated: high performance rendering is now deprecated in Bubble Tea.
55-
HighPerformanceRendering bool
56-
5745
initialized bool
5846
lines []string
5947
}
@@ -126,96 +114,70 @@ func (m Model) visibleLines() (lines []string) {
126114
return lines
127115
}
128116

129-
// scrollArea returns the scrollable boundaries for high performance rendering.
130-
//
131-
// XXX: high performance rendering is deprecated in Bubble Tea.
132-
func (m Model) scrollArea() (top, bottom int) {
133-
top = max(0, m.YPosition)
134-
bottom = max(top, top+m.Height)
135-
if top > 0 && bottom > top {
136-
bottom--
137-
}
138-
return top, bottom
139-
}
140-
141117
// SetYOffset sets the Y offset.
142118
func (m *Model) SetYOffset(n int) {
143119
m.YOffset = clamp(n, 0, m.maxYOffset())
144120
}
145121

146122
// ViewDown moves the view down by the number of lines in the viewport.
147123
// Basically, "page down".
148-
func (m *Model) ViewDown() []string {
124+
func (m *Model) ViewDown() {
149125
if m.AtBottom() {
150-
return nil
126+
return
151127
}
152128

153-
return m.LineDown(m.Height)
129+
m.LineDown(m.Height)
154130
}
155131

156132
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
157-
func (m *Model) ViewUp() []string {
133+
func (m *Model) ViewUp() {
158134
if m.AtTop() {
159-
return nil
135+
return
160136
}
161137

162-
return m.LineUp(m.Height)
138+
m.LineUp(m.Height)
163139
}
164140

165141
// HalfViewDown moves the view down by half the height of the viewport.
166-
func (m *Model) HalfViewDown() (lines []string) {
142+
func (m *Model) HalfViewDown() {
167143
if m.AtBottom() {
168-
return nil
144+
return
169145
}
170146

171-
return m.LineDown(m.Height / 2)
147+
m.LineDown(m.Height / 2)
172148
}
173149

174150
// HalfViewUp moves the view up by half the height of the viewport.
175-
func (m *Model) HalfViewUp() (lines []string) {
151+
func (m *Model) HalfViewUp() {
176152
if m.AtTop() {
177-
return nil
153+
return
178154
}
179155

180-
return m.LineUp(m.Height / 2)
156+
m.LineUp(m.Height / 2)
181157
}
182158

183159
// LineDown moves the view down by the given number of lines.
184-
func (m *Model) LineDown(n int) (lines []string) {
160+
func (m *Model) LineDown(n int) {
185161
if m.AtBottom() || n == 0 || len(m.lines) == 0 {
186-
return nil
162+
return
187163
}
188164

189165
// Make sure the number of lines by which we're going to scroll isn't
190166
// greater than the number of lines we actually have left before we reach
191167
// the bottom.
192168
m.SetYOffset(m.YOffset + n)
193-
194-
// Gather lines to send off for performance scrolling.
195-
//
196-
// XXX: high performance rendering is deprecated in Bubble Tea.
197-
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines))
198-
top := clamp(m.YOffset+m.Height-n, 0, bottom)
199-
return m.lines[top:bottom]
200169
}
201170

202171
// LineUp moves the view down by the given number of lines. Returns the new
203172
// lines to show.
204-
func (m *Model) LineUp(n int) (lines []string) {
173+
func (m *Model) LineUp(n int) {
205174
if m.AtTop() || n == 0 || len(m.lines) == 0 {
206-
return nil
175+
return
207176
}
208177

209178
// Make sure the number of lines by which we're going to scroll isn't
210179
// greater than the number of lines we are from the top.
211180
m.SetYOffset(m.YOffset - n)
212-
213-
// Gather lines to send off for performance scrolling.
214-
//
215-
// XXX: high performance rendering is deprecated in Bubble Tea.
216-
top := max(0, m.YOffset)
217-
bottom := clamp(m.YOffset+n, 0, m.maxYOffset())
218-
return m.lines[top:bottom]
219181
}
220182

221183
// TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.
@@ -244,106 +206,39 @@ func (m *Model) GotoBottom() (lines []string) {
244206
return m.visibleLines()
245207
}
246208

247-
// Sync tells the renderer where the viewport will be located and requests
248-
// a render of the current state of the viewport. It should be called for the
249-
// first render and after a window resize.
250-
//
251-
// For high performance rendering only.
252-
//
253-
// Deprecated: high performance rendering is deprecated in Bubble Tea.
254-
func Sync(m Model) tea.Cmd {
255-
if len(m.lines) == 0 {
256-
return nil
257-
}
258-
top, bottom := m.scrollArea()
259-
return tea.SyncScrollArea(m.visibleLines(), top, bottom)
260-
}
261-
262-
// ViewDown is a high performance command that moves the viewport up by a given
263-
// number of lines. Use Model.ViewDown to get the lines that should be rendered.
264-
// For example:
265-
//
266-
// lines := model.ViewDown(1)
267-
// cmd := ViewDown(m, lines)
268-
func ViewDown(m Model, lines []string) tea.Cmd {
269-
if len(lines) == 0 {
270-
return nil
271-
}
272-
top, bottom := m.scrollArea()
273-
274-
// XXX: high performance rendering is deprecated in Bubble Tea. In a v2 we
275-
// won't need to return a command here.
276-
return tea.ScrollDown(lines, top, bottom)
277-
}
278-
279-
// ViewUp is a high performance command the moves the viewport down by a given
280-
// number of lines height. Use Model.ViewUp to get the lines that should be
281-
// rendered.
282-
func ViewUp(m Model, lines []string) tea.Cmd {
283-
if len(lines) == 0 {
284-
return nil
285-
}
286-
top, bottom := m.scrollArea()
287-
288-
// XXX: high performance rendering is deprecated in Bubble Tea. In a v2 we
289-
// won't need to return a command here.
290-
return tea.ScrollUp(lines, top, bottom)
291-
}
292-
293209
// Update handles standard message-based viewport updates.
294210
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
295-
var cmd tea.Cmd
296-
m, cmd = m.updateAsModel(msg)
297-
return m, cmd
211+
m = m.updateAsModel(msg)
212+
return m, nil
298213
}
299214

300215
// Author's note: this method has been broken out to make it easier to
301216
// potentially transition Update to satisfy tea.Model.
302-
func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
217+
func (m Model) updateAsModel(msg tea.Msg) Model {
303218
if !m.initialized {
304219
m.setInitialValues()
305220
}
306221

307-
var cmd tea.Cmd
308-
309222
switch msg := msg.(type) {
310223
case tea.KeyPressMsg:
311224
switch {
312225
case key.Matches(msg, m.KeyMap.PageDown):
313-
lines := m.ViewDown()
314-
if m.HighPerformanceRendering {
315-
cmd = ViewDown(m, lines)
316-
}
226+
m.ViewDown()
317227

318228
case key.Matches(msg, m.KeyMap.PageUp):
319-
lines := m.ViewUp()
320-
if m.HighPerformanceRendering {
321-
cmd = ViewUp(m, lines)
322-
}
229+
m.ViewUp()
323230

324231
case key.Matches(msg, m.KeyMap.HalfPageDown):
325-
lines := m.HalfViewDown()
326-
if m.HighPerformanceRendering {
327-
cmd = ViewDown(m, lines)
328-
}
232+
m.HalfViewDown()
329233

330234
case key.Matches(msg, m.KeyMap.HalfPageUp):
331-
lines := m.HalfViewUp()
332-
if m.HighPerformanceRendering {
333-
cmd = ViewUp(m, lines)
334-
}
235+
m.HalfViewUp()
335236

336237
case key.Matches(msg, m.KeyMap.Down):
337-
lines := m.LineDown(1)
338-
if m.HighPerformanceRendering {
339-
cmd = ViewDown(m, lines)
340-
}
238+
m.LineDown(1)
341239

342240
case key.Matches(msg, m.KeyMap.Up):
343-
lines := m.LineUp(1)
344-
if m.HighPerformanceRendering {
345-
cmd = ViewUp(m, lines)
346-
}
241+
m.LineUp(1)
347242
}
348243

349244
case tea.MouseWheelMsg:
@@ -353,32 +248,18 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
353248

354249
switch msg.Button {
355250
case tea.MouseWheelDown:
356-
lines := m.LineDown(m.MouseWheelDelta)
357-
if m.HighPerformanceRendering {
358-
cmd = ViewDown(m, lines)
359-
}
251+
m.LineDown(m.MouseWheelDelta)
360252

361253
case tea.MouseWheelUp:
362-
lines := m.LineUp(m.MouseWheelDelta)
363-
if m.HighPerformanceRendering {
364-
cmd = ViewUp(m, lines)
365-
}
254+
m.LineUp(m.MouseWheelDelta)
366255
}
367256
}
368257

369-
return m, cmd
258+
return m
370259
}
371260

372261
// View renders the viewport into a string.
373262
func (m Model) View() string {
374-
if m.HighPerformanceRendering {
375-
// Just send newlines since we're going to be rendering the actual
376-
// content separately. We still need to send something that equals the
377-
// height of this view so that the Bubble Tea standard renderer can
378-
// position anything below this view properly.
379-
return strings.Repeat("\n", max(0, m.Height-1))
380-
}
381-
382263
w, h := m.Width, m.Height
383264
if sw := m.Style.GetWidth(); sw != 0 {
384265
w = min(w, sw)

0 commit comments

Comments
 (0)