Skip to content

Commit f2434c3

Browse files
committed
Revert "fix(viewport): normalize method names"
This reverts commit c7f889e. accidental push to master
1 parent c7f889e commit f2434c3

File tree

4 files changed

+66
-105
lines changed

4 files changed

+66
-105
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.ScrollUp(minimum - row)
860+
m.viewport.LineUp(minimum - row)
861861
} else if row > maximum {
862-
m.viewport.ScrollDown(row - maximum)
862+
m.viewport.LineDown(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.ScrollDown(1)
46+
textarea.viewport.LineDown(1)
4747
view = textarea.View()
4848
if !strings.Contains(view, line) {
4949
t.Log(view)

viewport/viewport.go

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

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

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

167169
// scrollArea returns the scrollable boundaries for high performance rendering.
168170
//
169-
// Deprecated: high performance rendering is deprecated in Bubble Tea.
171+
// XXX: high performance rendering is deprecated in Bubble Tea.
170172
func (m Model) scrollArea() (top, bottom int) {
171173
top = max(0, m.YPosition)
172174
bottom = max(top, top+m.Height)
@@ -181,81 +183,45 @@ func (m *Model) SetYOffset(n int) {
181183
m.YOffset = clamp(n, 0, m.maxYOffset())
182184
}
183185

184-
// PageDown moves the view down by the number of lines in the viewport.
186+
// ViewDown moves the view down by the number of lines in the viewport.
185187
// 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 {
194189
if m.AtBottom() {
195190
return nil
196191
}
197192

198-
return m.ScrollDown(m.Height)
193+
return m.LineDown(m.Height)
199194
}
200195

201-
// ViewUp moves the view up by one height of the viewport.
202-
// Basically, "page up".
203-
//
204-
// Deprecated: use [Model.PageUp] instead.
196+
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
205197
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 {
211198
if m.AtTop() {
212199
return nil
213200
}
214201

215-
return m.ScrollUp(m.Height)
202+
return m.LineUp(m.Height)
216203
}
217204

218205
// HalfViewDown moves the view down by half the height of the viewport.
219-
//
220-
// Deprecated: use [Model.HalfPageDown] instead.
221206
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) {
227207
if m.AtBottom() {
228208
return nil
229209
}
230210

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

234214
// HalfViewUp moves the view up by half the height of the viewport.
235-
//
236-
// Deprecated: use [Model.HalfPageUp] instead.
237215
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) {
243216
if m.AtTop() {
244217
return nil
245218
}
246219

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

250223
// LineDown moves the view down by the given number of lines.
251-
//
252-
// Deprecated: use [Model.ScrollDown] instead.
253224
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) {
259225
if m.AtBottom() || n == 0 || len(m.lines) == 0 {
260226
return nil
261227
}
@@ -275,15 +241,7 @@ func (m *Model) ScrollDown(n int) (lines []string) {
275241

276242
// LineUp moves the view down by the given number of lines. Returns the new
277243
// lines to show.
278-
//
279-
// Deprecated: use [Model.ScrollUp] instead.
280244
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) {
287245
if m.AtTop() || n == 0 || len(m.lines) == 0 {
288246
return nil
289247
}
@@ -300,32 +258,6 @@ func (m *Model) ScrollUp(n int) (lines []string) {
300258
return m.lines[top:bottom]
301259
}
302260

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-
329261
// TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.
330262
func (m Model) TotalLineCount() int {
331263
return len(m.lines)
@@ -373,8 +305,6 @@ func Sync(m Model) tea.Cmd {
373305
//
374306
// lines := model.ViewDown(1)
375307
// cmd := ViewDown(m, lines)
376-
//
377-
// Deprecated: high performance rendering is deprecated in Bubble Tea.
378308
func ViewDown(m Model, lines []string) tea.Cmd {
379309
if len(lines) == 0 {
380310
return nil
@@ -389,8 +319,6 @@ func ViewDown(m Model, lines []string) tea.Cmd {
389319
// ViewUp is a high performance command the moves the viewport down by a given
390320
// number of lines height. Use Model.ViewUp to get the lines that should be
391321
// rendered.
392-
//
393-
// Deprecated: high performance rendering is deprecated in Bubble Tea.
394322
func ViewUp(m Model, lines []string) tea.Cmd {
395323
if len(lines) == 0 {
396324
return nil
@@ -402,6 +330,39 @@ func ViewUp(m Model, lines []string) tea.Cmd {
402330
return tea.ScrollUp(lines, top, bottom) //nolint:staticcheck
403331
}
404332

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+
405366
// Update handles standard message-based viewport updates.
406367
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
407368
var cmd tea.Cmd
@@ -422,46 +383,46 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
422383
case tea.KeyMsg:
423384
switch {
424385
case key.Matches(msg, m.KeyMap.PageDown):
425-
lines := m.PageDown()
386+
lines := m.ViewDown()
426387
if m.HighPerformanceRendering {
427388
cmd = ViewDown(m, lines)
428389
}
429390

430391
case key.Matches(msg, m.KeyMap.PageUp):
431-
lines := m.PageUp()
392+
lines := m.ViewUp()
432393
if m.HighPerformanceRendering {
433394
cmd = ViewUp(m, lines)
434395
}
435396

436397
case key.Matches(msg, m.KeyMap.HalfPageDown):
437-
lines := m.HalfPageDown()
398+
lines := m.HalfViewDown()
438399
if m.HighPerformanceRendering {
439400
cmd = ViewDown(m, lines)
440401
}
441402

442403
case key.Matches(msg, m.KeyMap.HalfPageUp):
443-
lines := m.HalfPageUp()
404+
lines := m.HalfViewUp()
444405
if m.HighPerformanceRendering {
445406
cmd = ViewUp(m, lines)
446407
}
447408

448409
case key.Matches(msg, m.KeyMap.Down):
449-
lines := m.ScrollDown(1)
410+
lines := m.LineDown(1)
450411
if m.HighPerformanceRendering {
451412
cmd = ViewDown(m, lines)
452413
}
453414

454415
case key.Matches(msg, m.KeyMap.Up):
455-
lines := m.ScrollUp(1)
416+
lines := m.LineUp(1)
456417
if m.HighPerformanceRendering {
457418
cmd = ViewUp(m, lines)
458419
}
459420

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

463424
case key.Matches(msg, m.KeyMap.Right):
464-
m.ScrollRight(m.horizontalStep)
425+
m.MoveRight(m.horizontalStep)
465426
}
466427

467428
case tea.MouseMsg:
@@ -470,13 +431,13 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) {
470431
}
471432
switch msg.Button { //nolint:exhaustive
472433
case tea.MouseButtonWheelUp:
473-
lines := m.ScrollUp(m.MouseWheelDelta)
434+
lines := m.LineUp(m.MouseWheelDelta)
474435
if m.HighPerformanceRendering {
475436
cmd = ViewUp(m, lines)
476437
}
477438

478439
case tea.MouseButtonWheelDown:
479-
lines := m.ScrollDown(m.MouseWheelDelta)
440+
lines := m.LineDown(m.MouseWheelDelta)
480441
if m.HighPerformanceRendering {
481442
cmd = ViewDown(m, lines)
482443
}

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.ScrollLeft(m.horizontalStep)
98+
m.MoveLeft(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.ScrollLeft(m.horizontalStep)
112+
m.MoveLeft(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.ScrollRight(m.horizontalStep)
134+
m.MoveRight(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.SetXOffset(0)
153+
m.ResetIndent()
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.ScrollRight(m.horizontalStep)
277+
m.MoveRight(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.ScrollLeft(m.horizontalStep)
290+
m.MoveLeft(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.ScrollRight(horizontalStep)
332+
m.MoveRight(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.ScrollLeft(horizontalStep)
343+
m.MoveLeft(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.ScrollLeft(horizontalStep)
353+
m.MoveLeft(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.ScrollRight(m.horizontalStep)
373+
m.MoveRight(m.horizontalStep)
374374
}
375375

376376
visibleLines := m.visibleLines()

0 commit comments

Comments
 (0)