@@ -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.
170172func (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.
188188func (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".
205197func (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.
221206func (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.
237215func (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.
253224func (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.
280244func (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.
330262func (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.
378308func 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.
394322func 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.
406367func (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 }
0 commit comments