Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(viewport): gutter column, soft wrap, search highlight #697

Open
wants to merge 40 commits into
base: feature/i236-viewport-horizontal-scroll
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2f4a36a
feat(viewport): column sign
caarlos0 Jan 7, 2025
ea26eb7
feat: gutter, soft wrap
caarlos0 Jan 8, 2025
6c10dc2
wip: search
caarlos0 Jan 8, 2025
4eebb08
wip: search
caarlos0 Jan 8, 2025
eb50edc
wip: search
caarlos0 Jan 8, 2025
7784024
fix: perf
caarlos0 Jan 8, 2025
303ded7
fix: rename
caarlos0 Jan 8, 2025
619bac5
wip
caarlos0 Jan 8, 2025
d1ff1ab
wip
caarlos0 Jan 9, 2025
fbf76e8
refactor: viewport highlight ranges
caarlos0 Jan 9, 2025
5880b3a
fix: ligloss update
caarlos0 Jan 9, 2025
8e14bd2
doc: godoc
caarlos0 Jan 9, 2025
7f6d0eb
feat: fill height optional
caarlos0 Jan 9, 2025
8ddb856
fix: handle no content
caarlos0 Jan 9, 2025
0c86665
fix: empty lines
caarlos0 Jan 9, 2025
2d53a61
feat(viewport): horizontal scroll (#240)
tty2 Jan 10, 2025
e1944c4
Merge remote-tracking branch 'origin/master' into columnsign
caarlos0 Jan 10, 2025
b5f1251
wip
caarlos0 Jan 10, 2025
933f181
wip
caarlos0 Jan 10, 2025
0e3e31b
Revert "wip"
caarlos0 Jan 10, 2025
a7dc5f8
Reapply "wip"
caarlos0 Jan 10, 2025
d1928be
fix: wide
caarlos0 Jan 10, 2025
28cd0ad
fix: wide, find
caarlos0 Jan 10, 2025
067e70a
still not quite there
caarlos0 Jan 10, 2025
2a3bb65
fix: grapheme width
caarlos0 Jan 10, 2025
7b96ddd
fix: cleanups
caarlos0 Jan 10, 2025
912d216
fix: refactors, improves highlight visibility
caarlos0 Jan 11, 2025
7d13ae0
docs: godoc
caarlos0 Jan 11, 2025
87a4e45
docs: additional bubbles (#583)
caarlos0 Jan 17, 2025
e3ce11a
docs: update charm & friends blurb (#703)
bashbunni Jan 17, 2025
7ab08fb
fix(viewport): scroll to last line when borders (#706)
caarlos0 Jan 21, 2025
1bdd4c6
fix: stopwatch.Start() (#707)
bevicted Jan 22, 2025
0632e23
Merge remote-tracking branch 'origin/master' into columnsign
caarlos0 Jan 23, 2025
06eda29
chore: lipgloss update
caarlos0 Jan 23, 2025
b66bc64
chore: x/ansi update
caarlos0 Jan 23, 2025
74c65d3
fix: typos, godocs
caarlos0 Jan 23, 2025
4ac5ac9
fix: rename
caarlos0 Jan 23, 2025
3d06ce4
fix: typo
caarlos0 Jan 23, 2025
01cca44
fix: scroll when soft-wrapping
caarlos0 Jan 23, 2025
afe305c
fix: soft wrap adjustments
caarlos0 Jan 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ require (
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.3.8 // indirect
)

replace github.com/charmbracelet/lipgloss => ../lipgloss
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should def remember to remove this before merging 😄

2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/charmbracelet/bubbletea v1.1.2 h1:naQXF2laRxyLyil/i7fxdpiz1/k06IKquhm
github.com/charmbracelet/bubbletea v1.1.2/go.mod h1:9HIU/hBV24qKjlehyj8z1r/tR9TYTQEag+cWZnuXo8E=
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
github.com/charmbracelet/x/ansi v0.6.1-0.20250107110353-48b574af22a5 h1:TSjbA80sXnABV/Vxhnb67Ho7p8bEYqz6NIdhLAx+1yg=
github.com/charmbracelet/x/ansi v0.6.1-0.20250107110353-48b574af22a5/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
Expand Down
276 changes: 266 additions & 10 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package viewport

import (
"fmt"
"math"
"regexp"
"strings"

"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -29,6 +31,10 @@ type Model struct {
Height int
KeyMap KeyMap

// Whether or not to wrap text. If false, it'll allow horizontal scrolling
// instead.
SoftWrap bool

// Whether or not to respond to the mouse. The mouse must be enabled in
// Bubble Tea for this to work. For details, see the Bubble Tea docs.
MouseWheelEnabled bool
Expand Down Expand Up @@ -66,9 +72,58 @@ type Model struct {
// Deprecated: high performance rendering is now deprecated in Bubble Tea.
HighPerformanceRendering bool

// LeftGutterFunc allows to define a function that adds a column into the
// left of the viewpart, which is kept when horizontal scrolling.
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
// This should help support things like line numbers, selection indicators,
// and etc.
LeftGutterFunc GutterFunc

initialized bool
lines []string
longestLineWidth int

SearchMatchStyle lipgloss.Style
SearchHighlightMatchStyle lipgloss.Style

searchRE *regexp.Regexp
matches [][][]int
matchIndex int
currentMatch matched
memoizedMatchedLines []string
}

type matched struct {
line, start, end int
}

func (m matched) eq(line int, match []int) bool {
return line == m.line && match[0] == m.start && match[1] == m.end
}

// GutterFunc can be implemented and set into [Model.LeftGutterFunc].
type GutterFunc func(GutterContext) string

// LineNumbersGutter return a [GutterFunc] that shows line numbers.
func LineNumbersGutter(style lipgloss.Style) GutterFunc {
return func(info GutterContext) string {
if info.Soft {
return style.Render(" │ ")
}
if info.Index >= info.TotalLines {
return style.Render(" ~ │ ")
}
return style.Render(fmt.Sprintf("%4d │ ", info.Index+1))
}
}

// NoGutter is the default gutter used.
var NoGutter = func(GutterContext) string { return "" }

// GutterContext provides context to a [GutterFunc].
type GutterContext struct {
Index int
TotalLines int
Soft bool
}

func (m *Model) setInitialValues() {
Expand All @@ -77,6 +132,7 @@ func (m *Model) setInitialValues() {
m.MouseWheelDelta = 3
m.initialized = true
m.horizontalStep = defaultHorizontalStep
m.LeftGutterFunc = NoGutter
}

// Init exists to satisfy the tea.Model interface for composability purposes.
Expand Down Expand Up @@ -143,27 +199,101 @@ func (m Model) maxYOffset() int {
return max(0, len(m.lines)-m.Height)
}

// maxXOffset returns the maximum possible value of the x-offset based on the
// viewport's content and set width.
func (m Model) maxXOffset() int {
return max(0, m.longestLineWidth-m.Width)
}

func (m Model) maxWidth() int {
return m.Width -
m.Style.GetHorizontalFrameSize() -
lipgloss.Width(m.LeftGutterFunc(GutterContext{}))
}

func (m Model) maxHeight() int {
return m.Height - m.Style.GetVerticalFrameSize()
}

func (m Model) makeRanges(lmatches [][]int) []lipgloss.Range {
result := make([]lipgloss.Range, len(lmatches))
for i, match := range lmatches {
result[i] = lipgloss.NewRange(match[0], match[1], m.SearchMatchStyle)
}
return result
}

// visibleLines returns the lines that should currently be visible in the
// viewport.
func (m Model) visibleLines() (lines []string) {
h := m.Height - m.Style.GetVerticalFrameSize()
w := m.Width - m.Style.GetHorizontalFrameSize()
maxHeight := m.maxHeight()
maxWidth := m.maxWidth()

if len(m.lines) > 0 {
top := max(0, m.YOffset)
bottom := clamp(m.YOffset+h, top, len(m.lines))
lines = m.lines[top:bottom]
bottom := clamp(m.YOffset+maxHeight, top, len(m.lines))
lines = make([]string, bottom-top)
copy(lines, m.lines[top:bottom])
if len(m.matches) > 0 {
for i, lmatches := range m.matches[top:bottom] {
if memoized := m.memoizedMatchedLines[i+top]; memoized != "" {
lines[i] = memoized
} else {
lines[i] = lipgloss.StyleRanges(lines[i], m.makeRanges(lmatches))
m.memoizedMatchedLines[i+top] = lines[i]
}
if m.currentMatch.line == i+top {
lines[i] = lipgloss.StyleRange(
lines[i],
m.currentMatch.start,
m.currentMatch.end,
m.SearchHighlightMatchStyle,
)
}
}
}
}

for len(lines) < maxHeight {
lines = append(lines, "")
}

if (m.xOffset == 0 && m.longestLineWidth <= maxWidth) || maxWidth == 0 {
return m.prependColumn(lines)
}

if (m.xOffset == 0 && m.longestLineWidth <= w) || w == 0 {
return lines
if m.SoftWrap {
var wrappedLines []string
for i, line := range lines {
idx := 0
for ansi.StringWidth(line) >= idx {
truncatedLine := ansi.Cut(line, idx, maxWidth+idx)
wrappedLines = append(wrappedLines, m.LeftGutterFunc(GutterContext{
Index: i + m.YOffset,
TotalLines: m.TotalLineCount(),
Soft: idx > 0,
})+truncatedLine)
idx += maxWidth
}
}
return wrappedLines
}

cutLines := make([]string, len(lines))
for i := range lines {
cutLines[i] = ansi.Cut(lines[i], m.xOffset, m.xOffset+w)
lines[i] = ansi.Cut(lines[i], m.xOffset, m.xOffset+maxWidth)
}
return m.prependColumn(lines)
}

func (m Model) prependColumn(lines []string) []string {
result := make([]string, len(lines))
for i, line := range lines {
result[i] = m.LeftGutterFunc(GutterContext{
Index: i + m.YOffset,
TotalLines: m.TotalLineCount(),
}) + line
}
return cutLines
return result
}

// scrollArea returns the scrollable boundaries for high performance rendering.
Expand All @@ -183,6 +313,35 @@ func (m *Model) SetYOffset(n int) {
m.YOffset = clamp(n, 0, m.maxYOffset())
}

// SetXOffset sets the X offset.
// No-op when soft wrap is enabled.
func (m *Model) SetXOffset(n int) {
if m.SoftWrap {
return
}
m.xOffset = clamp(n, 0, m.maxXOffset())
}

func (m *Model) EnsureVisible(line, col int) {
maxHeight := m.maxHeight()
maxWidth := m.maxWidth()

if line >= m.YOffset && line < m.YOffset+maxHeight {
// Line is visible, no nothing
} else if line >= m.YOffset+maxHeight || line < m.YOffset {
m.SetYOffset(line)
}

if col >= m.xOffset && col < m.xOffset+maxWidth {
// Column is visible, do nothing
} else if col >= m.xOffset+maxWidth || col < m.xOffset {
// Column is to the left of visible area
m.SetXOffset(col)
}

m.visibleLines()
}

// ViewDown moves the view down by the number of lines in the viewport.
// Basically, "page down".
func (m *Model) ViewDown() []string {
Expand Down Expand Up @@ -230,6 +389,7 @@ func (m *Model) LineDown(n int) (lines []string) {
// greater than the number of lines we actually have left before we reach
// the bottom.
m.SetYOffset(m.YOffset + n)
m.nearestMatchFromYOffset()

// Gather lines to send off for performance scrolling.
//
Expand All @@ -249,6 +409,7 @@ func (m *Model) LineUp(n int) (lines []string) {
// Make sure the number of lines by which we're going to scroll isn't
// greater than the number of lines we are from the top.
m.SetYOffset(m.YOffset - n)
m.nearestMatchFromYOffset()

// Gather lines to send off for performance scrolling.
//
Expand All @@ -275,12 +436,14 @@ func (m *Model) GotoTop() (lines []string) {
}

m.SetYOffset(0)
m.nearestMatchFromYOffset()
return m.visibleLines()
}

// GotoBottom sets the viewport to the bottom position.
func (m *Model) GotoBottom() (lines []string) {
m.SetYOffset(m.maxYOffset())
m.nearestMatchFromYOffset()
return m.visibleLines()
}

Expand Down Expand Up @@ -352,7 +515,8 @@ func (m *Model) MoveLeft(cols int) {
// 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 {
w := m.maxWidth()
if m.xOffset > m.longestLineWidth-w {
return
}
m.xOffset += cols
Expand All @@ -363,6 +527,98 @@ func (m *Model) ResetIndent() {
m.xOffset = 0
}

func (m *Model) ClearSearch() {
m.searchRE = nil
m.matches = nil
m.memoizedMatchedLines = nil
m.currentMatch = matched{}
m.matchIndex = -1
}

func (m *Model) Search(r *regexp.Regexp) {
m.ClearSearch()
m.searchRE = r
m.matches = make([][][]int, len(m.lines))
m.memoizedMatchedLines = make([]string, len(m.lines))
for i, line := range m.lines {
found := r.FindAllStringIndex(ansi.Strip(line), -1)
m.matches[i] = found
}
m.nearestMatchFromYOffset()
m.EnsureVisible(m.currentMatch.line, m.currentMatch.start)
}

func (m *Model) NextMatch() {
if m.matches == nil {
return
}

got, ok := m.findMatch(m.matchIndex + 1)
if ok {
m.currentMatch = got
m.EnsureVisible(got.line, got.start)
m.matchIndex++
return
}
}

func (m *Model) PreviousMatch() {
if m.matches == nil || m.matchIndex <= 0 {
return
}

got, ok := m.findMatch(m.matchIndex - 1)
if ok {
m.currentMatch = got
m.EnsureVisible(got.line, got.start)
m.matchIndex--
return
}
}

func (m *Model) nearestMatchFromYOffset() {
if m.matches == nil {
return
}

totalMatches := 0
for i, match := range m.matches {
if len(match) == 0 {
continue
}
if i >= m.YOffset {
m.currentMatch = matched{
line: i,
start: match[0][0],
end: match[0][1],
}
m.matchIndex = totalMatches
return
}
totalMatches += len(match)
}
}

func (m *Model) findMatch(idx int) (matched, bool) {
totalMatches := 0
for i, lineMatches := range m.matches {
if len(lineMatches) == 0 {
continue
}
if idx < totalMatches+len(lineMatches) {
matchInLine := idx - totalMatches
match := lineMatches[matchInLine]
return matched{
line: i,
start: match[0],
end: match[1],
}, true
}
totalMatches += len(lineMatches)
}
return matched{}, false
}

// Update handles standard message-based viewport updates.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
Expand Down
Loading