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 28 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: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/bubbletea v1.1.2
github.com/charmbracelet/harmonica v0.2.0
github.com/charmbracelet/lipgloss v1.0.0
github.com/charmbracelet/lipgloss v1.0.1-0.20250109182251-99421664af19
github.com/charmbracelet/x/ansi v0.6.1-0.20250107110353-48b574af22a5
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91
github.com/dustin/go-humanize v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ 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/lipgloss v1.0.1-0.20250109182251-99421664af19 h1:um15AqNvVUVrfU+2ENdIc2YtIm83jF+6D1dW8Tm3S+8=
github.com/charmbracelet/lipgloss v1.0.1-0.20250109182251-99421664af19/go.mod h1:QRGthpgH59/perglqXZC8xPHqDGZ9BB45ChJCFEWEMI=
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
151 changes: 151 additions & 0 deletions viewport/highlight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package viewport

import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"github.com/rivo/uniseg"
)

// parseMatches converts the given matches into highlight ranges.
//
// Assumptions:
// - matches are measured in bytes, e.g. what [regex.FindAllStringIndex] would return
// - matches were made against the given content
// - matches are in order
// - matches do not overlap
// - content is line terminated with \n only
//
// We'll then convert the ranges into [highlightInfo]s, which hold the starting
// line and the grapheme positions.
func parseMatches(
content string,
matches [][]int,
) []highlightInfo {
if len(matches) == 0 {
return nil
}

line := 0
graphemePos := 0
previousLinesOffset := 0
bytePos := 0

highlights := make([]highlightInfo, 0, len(matches))
gr := uniseg.NewGraphemes(ansi.Strip(content))

for _, match := range matches {
byteStart, byteEnd := match[0], match[1]

// hilight for this match:
hi := highlightInfo{
lines: map[int][2]int{},
}

// find the beginning of this byte range, setup current line and
// grapheme position.
for byteStart > bytePos {
if !gr.Next() {
break
}
if content[bytePos] == '\n' {
previousLinesOffset = graphemePos + 1
line++
}
graphemePos += max(1, gr.Width())
bytePos += len(gr.Str())
}

hi.lineStart = line
hi.lineEnd = line

graphemeStart := graphemePos

// loop until we find the end
for byteEnd > bytePos {
if !gr.Next() {
break
}

// if it ends with a new line, add the range, increase line, and continue
if content[bytePos] == '\n' {
colstart := max(0, graphemeStart-previousLinesOffset)
colend := max(graphemePos-previousLinesOffset+1, colstart) // +1 its \n itself

// fmt.Printf(
// "nl line=%d linestart=%d lineend=%d colstart=%d colend=%d start=%d end=%d processed=%d width=%d\n",
// line, hi.lineStart, hi.lineEnd, colstart, colend, graphemeStart, graphemeEnd, previousLinesOffset, graphemePos-previousLinesOffset,
// )

if colend > colstart {
hi.lines[line] = [2]int{colstart, colend}
hi.lineEnd = line
}

previousLinesOffset = graphemePos + 1
line++
}

graphemePos += max(1, gr.Width())
bytePos += len(gr.Str())
}

// we found it!, add highlight and continue
if bytePos == byteEnd {
colstart := max(0, graphemeStart-previousLinesOffset)
colend := max(graphemePos-previousLinesOffset, colstart)

// fmt.Printf(
// "no line=%d linestart=%d lineend=%d colstart=%d colend=%d start=%d end=%d processed=%d width=%d\n",
// line, hi.lineStart, hi.lineEnd, colstart, colend, graphemeStart, graphemeEnd, previousLinesOffset, graphemePos-previousLinesOffset,
// )

if colend > colstart {
hi.lines[line] = [2]int{colstart, colend}
hi.lineEnd = line
}
}

highlights = append(highlights, hi)
}

return highlights
}

type highlightInfo struct {
// in which line this highlight starts and ends
lineStart, lineEnd int

// the grapheme highlight ranges for each of these lines
lines map[int][2]int
}

// coords returns the line x column of this highlight.
func (hi highlightInfo) coords() (int, int, int) {
for i := hi.lineStart; i <= hi.lineEnd; i++ {
hl, ok := hi.lines[i]
if !ok {
continue
}
return i, hl[0], hl[1]
}
return hi.lineStart, 0, 0
}

func makeHilightRanges(
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
highlights []highlightInfo,
line int,
style lipgloss.Style,
) []lipgloss.Range {
result := []lipgloss.Range{}
for _, hi := range highlights {
lihi, ok := hi.lines[line]
if !ok {
continue
}
if lihi == [2]int{} {
continue
}
result = append(result, lipgloss.NewRange(lihi[0], lihi[1], style))
}
return result
}
Loading
Loading