Skip to content

Commit c7f889e

Browse files
committed
fix(viewport): normalize method names
1 parent 9e5365e commit c7f889e

File tree

4 files changed

+105
-66
lines changed

4 files changed

+105
-66
lines changed

textarea/textarea.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,9 +857,9 @@ func (m *Model) repositionView() {
857857
maximum := minimum + m.viewport.Height - 1
858858

859859
if row := m.cursorLineNumber(); row < minimum {
860-
m.viewport.LineUp(minimum - row)
860+
m.viewport.ScrollUp(minimum - row)
861861
} else if row > maximum {
862-
m.viewport.LineDown(row - maximum)
862+
m.viewport.ScrollDown(row - maximum)
863863
}
864864
}
865865

textarea/textarea_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestVerticalScrolling(t *testing.T) {
4343
"the text area.",
4444
}
4545
for _, line := range lines {
46-
textarea.viewport.LineDown(1)
46+
textarea.viewport.ScrollDown(1)
4747
view = textarea.View()
4848
if !strings.Contains(view, line) {
4949
t.Log(view)

viewport/viewport.go

Lines changed: 92 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
"github.com/charmbracelet/x/ansi"
1111
)
1212

13-
const (
14-
defaultHorizontalStep = 6
15-
)
13+
const defaultHorizontalStep = 6
1614

1715
// New returns a new model with the given width and height as well as default
1816
// key mappings.
@@ -168,7 +166,7 @@ func (m Model) visibleLines() (lines []string) {
168166

169167
// scrollArea returns the scrollable boundaries for high performance rendering.
170168
//
171-
// XXX: high performance rendering is deprecated in Bubble Tea.
169+
// Deprecated: high performance rendering is deprecated in Bubble Tea.
172170
func (m Model) scrollArea() (top, bottom int) {
173171
top = max(0, m.YPosition)
174172
bottom = max(top, top+m.Height)
@@ -183,45 +181,81 @@ func (m *Model) SetYOffset(n int) {
183181
m.YOffset = clamp(n, 0, m.maxYOffset())
184182
}
185183

186-
// ViewDown moves the view down by the number of lines in the viewport.
184+
// PageDown moves the view down by the number of lines in the viewport.
187185
// Basically, "page down".
186+
//
187+
// Deprecated: use [Model.PageDown] instead.
188188
func (m *Model) ViewDown() []string {
189+
return m.PageDown()
190+
}
191+
192+
// PageDown moves the view down by the number of lines in the viewport.
193+
func (m *Model) PageDown() []string {
189194
if m.AtBottom() {
190195
return nil
191196
}
192197

193-
return m.LineDown(m.Height)
198+
return m.ScrollDown(m.Height)
194199
}
195200

196-
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
201+
// ViewUp moves the view up by one height of the viewport.
202+
// Basically, "page up".
203+
//
204+
// Deprecated: use [Model.PageUp] instead.
197205
func (m *Model) ViewUp() []string {
206+
return m.PageUp()
207+
}
208+
209+
// PageUp moves the view up by one height of the viewport.
210+
func (m *Model) PageUp() []string {
198211
if m.AtTop() {
199212
return nil
200213
}
201214

202-
return m.LineUp(m.Height)
215+
return m.ScrollUp(m.Height)
203216
}
204217

205218
// HalfViewDown moves the view down by half the height of the viewport.
219+
//
220+
// Deprecated: use [Model.HalfPageDown] instead.
206221
func (m *Model) HalfViewDown() (lines []string) {
222+
return m.HalfPageDown()
223+
}
224+
225+
// HalfPageDown moves the view down by half the height of the viewport.
226+
func (m *Model) HalfPageDown() (lines []string) {
207227
if m.AtBottom() {
208228
return nil
209229
}
210230

211-
return m.LineDown(m.Height / 2) //nolint:mnd
231+
return m.ScrollDown(m.Height / 2) //nolint:mnd
212232
}
213233

214234
// HalfViewUp moves the view up by half the height of the viewport.
235+
//
236+
// Deprecated: use [Model.HalfPageUp] instead.
215237
func (m *Model) HalfViewUp() (lines []string) {
238+
return m.HalfPageUp()
239+
}
240+
241+
// HalfPageUp moves the view up by half the height of the viewport.
242+
func (m *Model) HalfPageUp() (lines []string) {
216243
if m.AtTop() {
217244
return nil
218245
}
219246

220-
return m.LineUp(m.Height / 2) //nolint:mnd
247+
return m.ScrollUp(m.Height / 2) //nolint:mnd
221248
}
222249

223250
// LineDown moves the view down by the given number of lines.
251+
//
252+
// Deprecated: use [Model.ScrollDown] instead.
224253
func (m *Model) LineDown(n int) (lines []string) {
254+
return m.ScrollDown(n)
255+
}
256+
257+
// ScrollDown moves the view down by the given number of lines.
258+
func (m *Model) ScrollDown(n int) (lines []string) {
225259
if m.AtBottom() || n == 0 || len(m.lines) == 0 {
226260
return nil
227261
}
@@ -241,7 +275,15 @@ func (m *Model) LineDown(n int) (lines []string) {
241275

242276
// LineUp moves the view down by the given number of lines. Returns the new
243277
// lines to show.
278+
//
279+
// Deprecated: use [Model.ScrollUp] instead.
244280
func (m *Model) LineUp(n int) (lines []string) {
281+
return m.ScrollUp(n)
282+
}
283+
284+
// ScrollUp moves the view down by the given number of lines. Returns the new
285+
// lines to show.
286+
func (m *Model) ScrollUp(n int) (lines []string) {
245287
if m.AtTop() || n == 0 || len(m.lines) == 0 {
246288
return nil
247289
}
@@ -258,6 +300,32 @@ func (m *Model) LineUp(n int) (lines []string) {
258300
return m.lines[top:bottom]
259301
}
260302

303+
// SetHorizontalStep sets the default amount of columns to scroll left or right
304+
// with the default viewport key map.
305+
// If set to 0 or less, horizontal scrolling is disabled.
306+
func (m *Model) SetHorizontalStep(n int) {
307+
if n < 0 {
308+
n = 0
309+
}
310+
311+
m.horizontalStep = n
312+
}
313+
314+
// SetXOffset sets the X offset.
315+
func (m *Model) SetXOffset(n int) {
316+
m.xOffset = clamp(n, 0, m.longestLineWidth-m.Width)
317+
}
318+
319+
// ScrollLeft moves the viewport to the left by the given number of columns.
320+
func (m *Model) ScrollLeft(n int) {
321+
m.SetXOffset(m.xOffset - n)
322+
}
323+
324+
// ScrollRight moves viewport to the right by the given number of columns.
325+
func (m *Model) ScrollRight(n int) {
326+
m.SetXOffset(m.xOffset + n)
327+
}
328+
261329
// TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.
262330
func (m Model) TotalLineCount() int {
263331
return len(m.lines)
@@ -305,6 +373,8 @@ func Sync(m Model) tea.Cmd {
305373
//
306374
// lines := model.ViewDown(1)
307375
// cmd := ViewDown(m, lines)
376+
//
377+
// Deprecated: high performance rendering is deprecated in Bubble Tea.
308378
func ViewDown(m Model, lines []string) tea.Cmd {
309379
if len(lines) == 0 {
310380
return nil
@@ -319,6 +389,8 @@ func ViewDown(m Model, lines []string) tea.Cmd {
319389
// ViewUp is a high performance command the moves the viewport down by a given
320390
// number of lines height. Use Model.ViewUp to get the lines that should be
321391
// rendered.
392+
//
393+
// Deprecated: high performance rendering is deprecated in Bubble Tea.
322394
func ViewUp(m Model, lines []string) tea.Cmd {
323395
if len(lines) == 0 {
324396
return nil
@@ -330,39 +402,6 @@ func ViewUp(m Model, lines []string) tea.Cmd {
330402
return tea.ScrollUp(lines, top, bottom) //nolint:staticcheck
331403
}
332404

333-
// SetHorizontalStep sets the amount of cells that the viewport moves in the
334-
// default viewport keymapping. If set to 0 or less, horizontal scrolling is
335-
// disabled.
336-
func (m *Model) SetHorizontalStep(n int) {
337-
if n < 0 {
338-
n = 0
339-
}
340-
341-
m.horizontalStep = n
342-
}
343-
344-
// MoveLeft moves the viewport to the left by the given number of columns.
345-
func (m *Model) MoveLeft(cols int) {
346-
m.xOffset -= cols
347-
if m.xOffset < 0 {
348-
m.xOffset = 0
349-
}
350-
}
351-
352-
// MoveRight moves viewport to the right by the given number of columns.
353-
func (m *Model) MoveRight(cols int) {
354-
// prevents over scrolling to the right
355-
if m.xOffset >= m.longestLineWidth-m.Width {
356-
return
357-
}
358-
m.xOffset += cols
359-
}
360-
361-
// Resets lines indent to zero.
362-
func (m *Model) ResetIndent() {
363-
m.xOffset = 0
364-
}
365-
366405
// Update handles standard message-based viewport updates.
367406
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
368407
var cmd tea.Cmd
@@ -383,46 +422,46 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
383422
case tea.KeyMsg:
384423
switch {
385424
case key.Matches(msg, m.KeyMap.PageDown):
386-
lines := m.ViewDown()
425+
lines := m.PageDown()
387426
if m.HighPerformanceRendering {
388427
cmd = ViewDown(m, lines)
389428
}
390429

391430
case key.Matches(msg, m.KeyMap.PageUp):
392-
lines := m.ViewUp()
431+
lines := m.PageUp()
393432
if m.HighPerformanceRendering {
394433
cmd = ViewUp(m, lines)
395434
}
396435

397436
case key.Matches(msg, m.KeyMap.HalfPageDown):
398-
lines := m.HalfViewDown()
437+
lines := m.HalfPageDown()
399438
if m.HighPerformanceRendering {
400439
cmd = ViewDown(m, lines)
401440
}
402441

403442
case key.Matches(msg, m.KeyMap.HalfPageUp):
404-
lines := m.HalfViewUp()
443+
lines := m.HalfPageUp()
405444
if m.HighPerformanceRendering {
406445
cmd = ViewUp(m, lines)
407446
}
408447

409448
case key.Matches(msg, m.KeyMap.Down):
410-
lines := m.LineDown(1)
449+
lines := m.ScrollDown(1)
411450
if m.HighPerformanceRendering {
412451
cmd = ViewDown(m, lines)
413452
}
414453

415454
case key.Matches(msg, m.KeyMap.Up):
416-
lines := m.LineUp(1)
455+
lines := m.ScrollUp(1)
417456
if m.HighPerformanceRendering {
418457
cmd = ViewUp(m, lines)
419458
}
420459

421460
case key.Matches(msg, m.KeyMap.Left):
422-
m.MoveLeft(m.horizontalStep)
461+
m.ScrollLeft(m.horizontalStep)
423462

424463
case key.Matches(msg, m.KeyMap.Right):
425-
m.MoveRight(m.horizontalStep)
464+
m.ScrollRight(m.horizontalStep)
426465
}
427466

428467
case tea.MouseMsg:
@@ -431,13 +470,13 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
431470
}
432471
switch msg.Button { //nolint:exhaustive
433472
case tea.MouseButtonWheelUp:
434-
lines := m.LineUp(m.MouseWheelDelta)
473+
lines := m.ScrollUp(m.MouseWheelDelta)
435474
if m.HighPerformanceRendering {
436475
cmd = ViewUp(m, lines)
437476
}
438477

439478
case tea.MouseButtonWheelDown:
440-
lines := m.LineDown(m.MouseWheelDelta)
479+
lines := m.ScrollDown(m.MouseWheelDelta)
441480
if m.HighPerformanceRendering {
442481
cmd = ViewDown(m, lines)
443482
}

viewport/viewport_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestMoveLeft(t *testing.T) {
9595
t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset)
9696
}
9797

98-
m.MoveLeft(m.horizontalStep)
98+
m.ScrollLeft(m.horizontalStep)
9999
if m.xOffset != zeroPosition {
100100
t.Errorf("indent should be %d, got %d", zeroPosition, m.xOffset)
101101
}
@@ -109,7 +109,7 @@ func TestMoveLeft(t *testing.T) {
109109
}
110110

111111
m.xOffset = defaultHorizontalStep * 2
112-
m.MoveLeft(m.horizontalStep)
112+
m.ScrollLeft(m.horizontalStep)
113113
newIndent := defaultHorizontalStep
114114
if m.xOffset != newIndent {
115115
t.Errorf("indent should be %d, got %d", newIndent, m.xOffset)
@@ -131,7 +131,7 @@ func TestMoveRight(t *testing.T) {
131131
t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset)
132132
}
133133

134-
m.MoveRight(m.horizontalStep)
134+
m.ScrollRight(m.horizontalStep)
135135
newIndent := defaultHorizontalStep
136136
if m.xOffset != newIndent {
137137
t.Errorf("indent should be %d, got %d", newIndent, m.xOffset)
@@ -150,7 +150,7 @@ func TestResetIndent(t *testing.T) {
150150
m := New(10, 10)
151151
m.xOffset = 500
152152

153-
m.ResetIndent()
153+
m.SetXOffset(0)
154154
if m.xOffset != zeroPosition {
155155
t.Errorf("indent should be %d, got %d", zeroPosition, m.xOffset)
156156
}
@@ -274,7 +274,7 @@ func TestVisibleLines(t *testing.T) {
274274
}
275275

276276
// move right
277-
m.MoveRight(m.horizontalStep)
277+
m.ScrollRight(m.horizontalStep)
278278
list = m.visibleLines()
279279

280280
newPrefix := perceptPrefix[m.xOffset:]
@@ -287,7 +287,7 @@ func TestVisibleLines(t *testing.T) {
287287
}
288288

289289
// move left
290-
m.MoveLeft(m.horizontalStep)
290+
m.ScrollLeft(m.horizontalStep)
291291
list = m.visibleLines()
292292
if !strings.HasPrefix(list[0], perceptPrefix) {
293293
t.Errorf("first list item has to have prefix %s", perceptPrefix)
@@ -329,7 +329,7 @@ func TestVisibleLines(t *testing.T) {
329329
}
330330

331331
// move right
332-
m.MoveRight(horizontalStep)
332+
m.ScrollRight(horizontalStep)
333333
list = m.visibleLines()
334334

335335
for i := range list {
@@ -340,7 +340,7 @@ func TestVisibleLines(t *testing.T) {
340340
}
341341

342342
// move left
343-
m.MoveLeft(horizontalStep)
343+
m.ScrollLeft(horizontalStep)
344344
list = m.visibleLines()
345345
for i := range list {
346346
if list[i] != initList[i] {
@@ -350,7 +350,7 @@ func TestVisibleLines(t *testing.T) {
350350

351351
// move left second times do not change lites if indent == 0
352352
m.xOffset = 0
353-
m.MoveLeft(horizontalStep)
353+
m.ScrollLeft(horizontalStep)
354354
list = m.visibleLines()
355355
for i := range list {
356356
if list[i] != initList[i] {
@@ -370,7 +370,7 @@ func TestRightOverscroll(t *testing.T) {
370370
m.SetContent(content)
371371

372372
for i := 0; i < 10; i++ {
373-
m.MoveRight(m.horizontalStep)
373+
m.ScrollRight(m.horizontalStep)
374374
}
375375

376376
visibleLines := m.visibleLines()

0 commit comments

Comments
 (0)