-
Notifications
You must be signed in to change notification settings - Fork 457
feat(viewport)!: gutter column, soft wrap, search highlight #697
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
Merged
+646
−56
Merged
Changes from 48 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
98ba87b
horizontal scroll
tty2 fe8d41e
rebase branch
tty2 fd905df
add tests
tty2 11c5170
add tests with 2 cells symbols
tty2 b718451
trimLeft, move to charmbracelete/x/ansi lib
tty2 143ea43
up ansi package
tty2 f1307e5
Update viewport/viewport.go
tty2 4a8cae3
fix: do not navigate out to the right
caarlos0 d7137bb
fix: cache line width on setcontent
caarlos0 5dd53e5
fix tests
tty2 51e92a1
Merge pull request #1 from charmbracelet/feature/i236-viewport-horizo…
tty2 f6b7762
fix viewport tests
tty2 9c0bc5c
add test for preventing right overscroll
tty2 c9d33f9
chore(viewport): increase horizontal step to 6
meowgorithm 4acc392
chore(viewport): make horizontal scroll API better match vertical scr…
meowgorithm d1132a4
fix: nolint
caarlos0 36a216e
fix: use ansi.Cut
caarlos0 fa32384
perf: do not cut anything if not needed
caarlos0 fa28725
feat: expose HorizontalScrollPercent
caarlos0 36be8b6
fix: do not scroll if width is 0
caarlos0 4aff9da
fix: visible lines take frame into account
caarlos0 2f4a36a
feat(viewport): column sign
caarlos0 ea26eb7
feat: gutter, soft wrap
caarlos0 6c10dc2
wip: search
caarlos0 4eebb08
wip: search
caarlos0 eb50edc
wip: search
caarlos0 7784024
fix: perf
caarlos0 303ded7
fix: rename
caarlos0 619bac5
wip
caarlos0 d1ff1ab
wip
caarlos0 fbf76e8
refactor: viewport highlight ranges
caarlos0 5880b3a
fix: ligloss update
caarlos0 8e14bd2
doc: godoc
caarlos0 7f6d0eb
feat: fill height optional
caarlos0 8ddb856
fix: handle no content
caarlos0 0c86665
fix: empty lines
caarlos0 e1944c4
Merge remote-tracking branch 'origin/master' into columnsign
caarlos0 b5f1251
wip
caarlos0 933f181
wip
caarlos0 0e3e31b
Revert "wip"
caarlos0 a7dc5f8
Reapply "wip"
caarlos0 d1928be
fix: wide
caarlos0 28cd0ad
fix: wide, find
caarlos0 067e70a
still not quite there
caarlos0 2a3bb65
fix: grapheme width
caarlos0 7b96ddd
fix: cleanups
caarlos0 912d216
fix: refactors, improves highlight visibility
caarlos0 7d13ae0
docs: godoc
caarlos0 0632e23
Merge remote-tracking branch 'origin/master' into columnsign
caarlos0 06eda29
chore: lipgloss update
caarlos0 b66bc64
chore: x/ansi update
caarlos0 74c65d3
fix: typos, godocs
caarlos0 4ac5ac9
fix: rename
caarlos0 3d06ce4
fix: typo
caarlos0 01cca44
fix: scroll when soft-wrapping
caarlos0 afe305c
fix: soft wrap adjustments
caarlos0 b273cc1
Merge remote-tracking branch 'origin/v2-exp' into columnsign
caarlos0 93de0f7
fix: update
caarlos0 165a74c
fix: deps
caarlos0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.