on main help sign in
fix(ui): list: ensure the offset line does not go negative when scrolling up
f22a6f9Ayman Bagabas
internal/ui/list/list.go+10−14
@@ -158,7 +158,7 @@ func (l *List) getItem(idx int) renderedItem {
158
158
159 rendered := item.Render(l.width)
159 rendered := item.Render(l.width)
160 rendered = strings.TrimRight(rendered, "\n")
160 rendered = strings.TrimRight(rendered, "\n")
161− height := countLines(rendered)
161+ height := strings.Count(rendered, "\n") + 1
162 ri := renderedItem{
162 ri := renderedItem{
163 content: rendered,
163 content: rendered,
164 height: height,
164 height: height,
@@ -190,13 +190,18 @@ func (l *List) ScrollBy(lines int) {
190 }
190 }
191
191
192 if lines > 0 {
192 if lines > 0 {
193+ if l.AtBottom() {
194+ // Already at bottom
195+ return
196+ }
197+
193 // Scroll down
198 // Scroll down
194 l.offsetLine += lines
199 l.offsetLine += lines
195 currentItem := l.getItem(l.offsetIdx)
200 currentItem := l.getItem(l.offsetIdx)
196 for l.offsetLine >= currentItem.height {
201 for l.offsetLine >= currentItem.height {
197 l.offsetLine -= currentItem.height
202 l.offsetLine -= currentItem.height
198 if l.gap > 0 {
203 if l.gap > 0 {
199− l.offsetLine -= l.gap
204+ l.offsetLine = max(0, l.offsetLine-l.gap)
200 }
205 }
201
206
202 // Move to next item
207 // Move to next item
@@ -219,14 +224,13 @@ func (l *List) ScrollBy(lines int) {
219 // Scroll up
224 // Scroll up
220 l.offsetLine += lines // lines is negative
225 l.offsetLine += lines // lines is negative
221 for l.offsetLine < 0 {
226 for l.offsetLine < 0 {
227+ // Move to previous item
228+ l.offsetIdx--
222− if l.offsetIdx <= 0 {
229+ if l.offsetIdx < 0 {
223 // Reached top
230 // Reached top
224 l.ScrollToTop()
231 l.ScrollToTop()
225 break
232 break
226 }
233 }
227−
228− // Move to previous item
229− l.offsetIdx--
230 prevItem := l.getItem(l.offsetIdx)
234 prevItem := l.getItem(l.offsetIdx)
231 totalHeight := prevItem.height
235 totalHeight := prevItem.height
232 if l.gap > 0 {
236 if l.gap > 0 {
@@ -642,11 +646,3 @@ func (l *List) findItemAtY(_, y int) (itemIdx int, itemY int) {
642
646
643 return -1, -1
647 return -1, -1
644}
648}
645−
646−// countLines counts the number of lines in a string.
647−func countLines(s string) int {
648− if s == "" {
649− return 1
650− }
651− return strings.Count(s, "\n") + 1
652−}