Merge pull request #1 from charmbracelet/feature/i236-viewport-horizontal-scroll

Feature/i236 viewport horizontal scroll
This commit is contained in:
Roman
2024-12-18 21:28:34 +01:00
committed by GitHub
+19 -3
View File
@@ -62,9 +62,10 @@ type Model struct {
// horizontal step represents the step of indent we add with one move left or right.
horizontalStep int
indent int
initialized bool
lines []string
indent int
initialized bool
lines []string
longestLineWidth int
}
func (m *Model) setInitialValues() {
@@ -113,6 +114,7 @@ func (m Model) ScrollPercent() float64 {
func (m *Model) SetContent(s string) {
s = strings.ReplaceAll(s, "\r\n", "\n") // normalize line endings
m.lines = strings.Split(s, "\n")
m.longestLineWidth = findLongestLineWidth(m.lines)
if m.YOffset > len(m.lines)-1 {
m.GotoBottom()
@@ -331,6 +333,10 @@ func (m *Model) MoveLeft() {
// MoveRight moves all lines to set runes right.
func (m *Model) MoveRight() {
// prevents over scrolling to the right
if m.indent >= m.longestLineWidth-m.Width {
return
}
m.indent += m.horizontalStep
}
@@ -473,3 +479,13 @@ func max(a, b int) int {
}
return b
}
func findLongestLineWidth(lines []string) int {
w := 0
for _, l := range lines {
if ww := ansi.StringWidth(l); ww > w {
w = ww
}
}
return w
}