Skip to content
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
98ba87b
horizontal scroll
tty2 Sep 14, 2022
fe8d41e
rebase branch
tty2 Sep 20, 2022
fd905df
add tests
tty2 Sep 20, 2022
11c5170
add tests with 2 cells symbols
tty2 Sep 21, 2022
b718451
trimLeft, move to charmbracelete/x/ansi lib
tty2 Dec 14, 2024
143ea43
up ansi package
tty2 Dec 14, 2024
f1307e5
Update viewport/viewport.go
tty2 Dec 18, 2024
4a8cae3
fix: do not navigate out to the right
caarlos0 Dec 18, 2024
d7137bb
fix: cache line width on setcontent
caarlos0 Dec 18, 2024
5dd53e5
fix tests
tty2 Dec 18, 2024
51e92a1
Merge pull request #1 from charmbracelet/feature/i236-viewport-horizo…
tty2 Dec 18, 2024
f6b7762
fix viewport tests
tty2 Dec 18, 2024
9c0bc5c
add test for preventing right overscroll
tty2 Dec 18, 2024
c9d33f9
chore(viewport): increase horizontal step to 6
meowgorithm Jan 6, 2025
4acc392
chore(viewport): make horizontal scroll API better match vertical scr…
meowgorithm Jan 6, 2025
d1132a4
fix: nolint
caarlos0 Jan 7, 2025
36a216e
fix: use ansi.Cut
caarlos0 Jan 7, 2025
fa32384
perf: do not cut anything if not needed
caarlos0 Jan 7, 2025
fa28725
feat: expose HorizontalScrollPercent
caarlos0 Jan 7, 2025
36be8b6
fix: do not scroll if width is 0
caarlos0 Jan 7, 2025
4aff9da
fix: visible lines take frame into account
caarlos0 Jan 7, 2025
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
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
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
b273cc1
Merge remote-tracking branch 'origin/v2-exp' into columnsign
caarlos0 Feb 4, 2025
93de0f7
fix: update
caarlos0 Feb 4, 2025
165a74c
fix: deps
caarlos0 Feb 4, 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(
Comment thread
caarlos0 marked this conversation as resolved.
Outdated
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