diff --git a/textarea/textarea.go b/textarea/textarea.go index 9af8402..88bc979 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -857,9 +857,9 @@ func (m *Model) repositionView() { maximum := minimum + m.viewport.Height - 1 if row := m.cursorLineNumber(); row < minimum { - m.viewport.ScrollUp(minimum - row) + m.viewport.LineUp(minimum - row) } else if row > maximum { - m.viewport.ScrollDown(row - maximum) + m.viewport.LineDown(row - maximum) } } diff --git a/textarea/textarea_test.go b/textarea/textarea_test.go index 9b272c5..41d626c 100644 --- a/textarea/textarea_test.go +++ b/textarea/textarea_test.go @@ -43,7 +43,7 @@ func TestVerticalScrolling(t *testing.T) { "the text area.", } for _, line := range lines { - textarea.viewport.ScrollDown(1) + textarea.viewport.LineDown(1) view = textarea.View() if !strings.Contains(view, line) { t.Log(view) diff --git a/viewport/viewport.go b/viewport/viewport.go index dc61e74..0a818c0 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -10,7 +10,9 @@ import ( "github.com/charmbracelet/x/ansi" ) -const defaultHorizontalStep = 6 +const ( + defaultHorizontalStep = 6 +) // New returns a new model with the given width and height as well as default // key mappings. @@ -166,7 +168,7 @@ func (m Model) visibleLines() (lines []string) { // scrollArea returns the scrollable boundaries for high performance rendering. // -// Deprecated: high performance rendering is deprecated in Bubble Tea. +// XXX: high performance rendering is deprecated in Bubble Tea. func (m Model) scrollArea() (top, bottom int) { top = max(0, m.YPosition) bottom = max(top, top+m.Height) @@ -181,81 +183,45 @@ func (m *Model) SetYOffset(n int) { m.YOffset = clamp(n, 0, m.maxYOffset()) } -// PageDown moves the view down by the number of lines in the viewport. +// ViewDown moves the view down by the number of lines in the viewport. // Basically, "page down". -// -// Deprecated: use [Model.PageDown] instead. func (m *Model) ViewDown() []string { - return m.PageDown() -} - -// PageDown moves the view down by the number of lines in the viewport. -func (m *Model) PageDown() []string { if m.AtBottom() { return nil } - return m.ScrollDown(m.Height) + return m.LineDown(m.Height) } -// ViewUp moves the view up by one height of the viewport. -// Basically, "page up". -// -// Deprecated: use [Model.PageUp] instead. +// ViewUp moves the view up by one height of the viewport. Basically, "page up". func (m *Model) ViewUp() []string { - return m.PageUp() -} - -// PageUp moves the view up by one height of the viewport. -func (m *Model) PageUp() []string { if m.AtTop() { return nil } - return m.ScrollUp(m.Height) + return m.LineUp(m.Height) } // HalfViewDown moves the view down by half the height of the viewport. -// -// Deprecated: use [Model.HalfPageDown] instead. func (m *Model) HalfViewDown() (lines []string) { - return m.HalfPageDown() -} - -// HalfPageDown moves the view down by half the height of the viewport. -func (m *Model) HalfPageDown() (lines []string) { if m.AtBottom() { return nil } - return m.ScrollDown(m.Height / 2) //nolint:mnd + return m.LineDown(m.Height / 2) //nolint:mnd } // HalfViewUp moves the view up by half the height of the viewport. -// -// Deprecated: use [Model.HalfPageUp] instead. func (m *Model) HalfViewUp() (lines []string) { - return m.HalfPageUp() -} - -// HalfPageUp moves the view up by half the height of the viewport. -func (m *Model) HalfPageUp() (lines []string) { if m.AtTop() { return nil } - return m.ScrollUp(m.Height / 2) //nolint:mnd + return m.LineUp(m.Height / 2) //nolint:mnd } // LineDown moves the view down by the given number of lines. -// -// Deprecated: use [Model.ScrollDown] instead. func (m *Model) LineDown(n int) (lines []string) { - return m.ScrollDown(n) -} - -// ScrollDown moves the view down by the given number of lines. -func (m *Model) ScrollDown(n int) (lines []string) { if m.AtBottom() || n == 0 || len(m.lines) == 0 { return nil } @@ -275,15 +241,7 @@ func (m *Model) ScrollDown(n int) (lines []string) { // LineUp moves the view down by the given number of lines. Returns the new // lines to show. -// -// Deprecated: use [Model.ScrollUp] instead. func (m *Model) LineUp(n int) (lines []string) { - return m.ScrollUp(n) -} - -// ScrollUp moves the view down by the given number of lines. Returns the new -// lines to show. -func (m *Model) ScrollUp(n int) (lines []string) { if m.AtTop() || n == 0 || len(m.lines) == 0 { return nil } @@ -300,32 +258,6 @@ func (m *Model) ScrollUp(n int) (lines []string) { return m.lines[top:bottom] } -// SetHorizontalStep sets the default amount of columns to scroll left or right -// with the default viewport key map. -// If set to 0 or less, horizontal scrolling is disabled. -func (m *Model) SetHorizontalStep(n int) { - if n < 0 { - n = 0 - } - - m.horizontalStep = n -} - -// SetXOffset sets the X offset. -func (m *Model) SetXOffset(n int) { - m.xOffset = clamp(n, 0, m.longestLineWidth-m.Width) -} - -// ScrollLeft moves the viewport to the left by the given number of columns. -func (m *Model) ScrollLeft(n int) { - m.SetXOffset(m.xOffset - n) -} - -// ScrollRight moves viewport to the right by the given number of columns. -func (m *Model) ScrollRight(n int) { - m.SetXOffset(m.xOffset + n) -} - // TotalLineCount returns the total number of lines (both hidden and visible) within the viewport. func (m Model) TotalLineCount() int { return len(m.lines) @@ -373,8 +305,6 @@ func Sync(m Model) tea.Cmd { // // lines := model.ViewDown(1) // cmd := ViewDown(m, lines) -// -// Deprecated: high performance rendering is deprecated in Bubble Tea. func ViewDown(m Model, lines []string) tea.Cmd { if len(lines) == 0 { return nil @@ -389,8 +319,6 @@ func ViewDown(m Model, lines []string) tea.Cmd { // ViewUp is a high performance command the moves the viewport down by a given // number of lines height. Use Model.ViewUp to get the lines that should be // rendered. -// -// Deprecated: high performance rendering is deprecated in Bubble Tea. func ViewUp(m Model, lines []string) tea.Cmd { if len(lines) == 0 { return nil @@ -402,6 +330,39 @@ func ViewUp(m Model, lines []string) tea.Cmd { return tea.ScrollUp(lines, top, bottom) //nolint:staticcheck } +// SetHorizontalStep sets the amount of cells that the viewport moves in the +// default viewport keymapping. If set to 0 or less, horizontal scrolling is +// disabled. +func (m *Model) SetHorizontalStep(n int) { + if n < 0 { + n = 0 + } + + m.horizontalStep = n +} + +// MoveLeft moves the viewport to the left by the given number of columns. +func (m *Model) MoveLeft(cols int) { + m.xOffset -= cols + if m.xOffset < 0 { + m.xOffset = 0 + } +} + +// MoveRight moves viewport to the right by the given number of columns. +func (m *Model) MoveRight(cols int) { + // prevents over scrolling to the right + if m.xOffset >= m.longestLineWidth-m.Width { + return + } + m.xOffset += cols +} + +// Resets lines indent to zero. +func (m *Model) ResetIndent() { + m.xOffset = 0 +} + // Update handles standard message-based viewport updates. func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { var cmd tea.Cmd @@ -422,46 +383,46 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) { case tea.KeyMsg: switch { case key.Matches(msg, m.KeyMap.PageDown): - lines := m.PageDown() + lines := m.ViewDown() if m.HighPerformanceRendering { cmd = ViewDown(m, lines) } case key.Matches(msg, m.KeyMap.PageUp): - lines := m.PageUp() + lines := m.ViewUp() if m.HighPerformanceRendering { cmd = ViewUp(m, lines) } case key.Matches(msg, m.KeyMap.HalfPageDown): - lines := m.HalfPageDown() + lines := m.HalfViewDown() if m.HighPerformanceRendering { cmd = ViewDown(m, lines) } case key.Matches(msg, m.KeyMap.HalfPageUp): - lines := m.HalfPageUp() + lines := m.HalfViewUp() if m.HighPerformanceRendering { cmd = ViewUp(m, lines) } case key.Matches(msg, m.KeyMap.Down): - lines := m.ScrollDown(1) + lines := m.LineDown(1) if m.HighPerformanceRendering { cmd = ViewDown(m, lines) } case key.Matches(msg, m.KeyMap.Up): - lines := m.ScrollUp(1) + lines := m.LineUp(1) if m.HighPerformanceRendering { cmd = ViewUp(m, lines) } case key.Matches(msg, m.KeyMap.Left): - m.ScrollLeft(m.horizontalStep) + m.MoveLeft(m.horizontalStep) case key.Matches(msg, m.KeyMap.Right): - m.ScrollRight(m.horizontalStep) + m.MoveRight(m.horizontalStep) } case tea.MouseMsg: @@ -470,13 +431,13 @@ func (m Model) updateAsModel(msg tea.Msg) (Model, tea.Cmd) { } switch msg.Button { //nolint:exhaustive case tea.MouseButtonWheelUp: - lines := m.ScrollUp(m.MouseWheelDelta) + lines := m.LineUp(m.MouseWheelDelta) if m.HighPerformanceRendering { cmd = ViewUp(m, lines) } case tea.MouseButtonWheelDown: - lines := m.ScrollDown(m.MouseWheelDelta) + lines := m.LineDown(m.MouseWheelDelta) if m.HighPerformanceRendering { cmd = ViewDown(m, lines) } diff --git a/viewport/viewport_test.go b/viewport/viewport_test.go index b29da88..ef70a54 100644 --- a/viewport/viewport_test.go +++ b/viewport/viewport_test.go @@ -95,7 +95,7 @@ func TestMoveLeft(t *testing.T) { t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset) } - m.ScrollLeft(m.horizontalStep) + m.MoveLeft(m.horizontalStep) if m.xOffset != zeroPosition { t.Errorf("indent should be %d, got %d", zeroPosition, m.xOffset) } @@ -109,7 +109,7 @@ func TestMoveLeft(t *testing.T) { } m.xOffset = defaultHorizontalStep * 2 - m.ScrollLeft(m.horizontalStep) + m.MoveLeft(m.horizontalStep) newIndent := defaultHorizontalStep if m.xOffset != newIndent { t.Errorf("indent should be %d, got %d", newIndent, m.xOffset) @@ -131,7 +131,7 @@ func TestMoveRight(t *testing.T) { t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset) } - m.ScrollRight(m.horizontalStep) + m.MoveRight(m.horizontalStep) newIndent := defaultHorizontalStep if m.xOffset != newIndent { t.Errorf("indent should be %d, got %d", newIndent, m.xOffset) @@ -150,7 +150,7 @@ func TestResetIndent(t *testing.T) { m := New(10, 10) m.xOffset = 500 - m.SetXOffset(0) + m.ResetIndent() if m.xOffset != zeroPosition { t.Errorf("indent should be %d, got %d", zeroPosition, m.xOffset) } @@ -274,7 +274,7 @@ func TestVisibleLines(t *testing.T) { } // move right - m.ScrollRight(m.horizontalStep) + m.MoveRight(m.horizontalStep) list = m.visibleLines() newPrefix := perceptPrefix[m.xOffset:] @@ -287,7 +287,7 @@ func TestVisibleLines(t *testing.T) { } // move left - m.ScrollLeft(m.horizontalStep) + m.MoveLeft(m.horizontalStep) list = m.visibleLines() if !strings.HasPrefix(list[0], perceptPrefix) { t.Errorf("first list item has to have prefix %s", perceptPrefix) @@ -329,7 +329,7 @@ func TestVisibleLines(t *testing.T) { } // move right - m.ScrollRight(horizontalStep) + m.MoveRight(horizontalStep) list = m.visibleLines() for i := range list { @@ -340,7 +340,7 @@ func TestVisibleLines(t *testing.T) { } // move left - m.ScrollLeft(horizontalStep) + m.MoveLeft(horizontalStep) list = m.visibleLines() for i := range list { if list[i] != initList[i] { @@ -350,7 +350,7 @@ func TestVisibleLines(t *testing.T) { // move left second times do not change lites if indent == 0 m.xOffset = 0 - m.ScrollLeft(horizontalStep) + m.MoveLeft(horizontalStep) list = m.visibleLines() for i := range list { if list[i] != initList[i] { @@ -370,7 +370,7 @@ func TestRightOverscroll(t *testing.T) { m.SetContent(content) for i := 0; i < 10; i++ { - m.ScrollRight(m.horizontalStep) + m.MoveRight(m.horizontalStep) } visibleLines := m.visibleLines()