mirror of
https://github.com/Maks1mS/bubbles.git
synced 2024-12-24 22:54:39 +03:00
Fix situation where short viewports would panic (closes #20)
This commit is contained in:
parent
f58fead10d
commit
5dbcf95877
@ -77,7 +77,7 @@ func (m *Model) SetContent(s string) {
|
|||||||
func (m Model) visibleLines() (lines []string) {
|
func (m Model) visibleLines() (lines []string) {
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(0, m.YOffset)
|
top := max(0, m.YOffset)
|
||||||
bottom := min(len(m.lines), m.YOffset+m.Height)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines))
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
return lines
|
return lines
|
||||||
@ -125,7 +125,7 @@ func (m *Model) HalfViewDown() (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(m.YOffset+m.Height/2, 0)
|
top := max(m.YOffset+m.Height/2, 0)
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ func (m *Model) HalfViewUp() (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(m.YOffset, 0)
|
top := max(m.YOffset, 0)
|
||||||
bottom := min(m.YOffset+m.Height/2, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height/2, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ func (m *Model) LineDown(n int) (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(m.YOffset+m.Height-n, 0)
|
top := max(m.YOffset+m.Height-n, 0)
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,7 +193,7 @@ func (m *Model) LineUp(n int) (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(0, m.YOffset)
|
top := max(0, m.YOffset)
|
||||||
bottom := min(m.YOffset+n, len(m.lines)-1)
|
bottom := clamp(m.YOffset+n, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ func (m *Model) GotoTop() (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := m.YOffset
|
top := m.YOffset
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ func Sync(m Model) tea.Cmd {
|
|||||||
// TODO: we should probably use m.visibleLines() rather than these two
|
// TODO: we should probably use m.visibleLines() rather than these two
|
||||||
// expressions.
|
// expressions.
|
||||||
top := max(m.YOffset, 0)
|
top := max(m.YOffset, 0)
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines)-1)
|
||||||
|
|
||||||
return tea.SyncScrollArea(
|
return tea.SyncScrollArea(
|
||||||
m.lines[top:bottom],
|
m.lines[top:bottom],
|
||||||
@ -376,6 +376,10 @@ func View(m Model) string {
|
|||||||
|
|
||||||
// ETC
|
// ETC
|
||||||
|
|
||||||
|
func clamp(v, low, high int) int {
|
||||||
|
return min(high, max(low, v))
|
||||||
|
}
|
||||||
|
|
||||||
func min(a, b int) int {
|
func min(a, b int) int {
|
||||||
if a < b {
|
if a < b {
|
||||||
return a
|
return a
|
||||||
|
Loading…
Reference in New Issue
Block a user