Skip to content

Commit

Permalink
fix: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jan 10, 2025
1 parent 2a3bb65 commit 7b96ddd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
10 changes: 3 additions & 7 deletions viewport/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func parseMatches(
hi.lineEnd = line

graphemeStart := graphemePos
graphemeEnd := graphemePos

// loop until we find the end
for byteEnd > bytePos {
Expand All @@ -69,9 +68,8 @@ func parseMatches(

// if it ends with a new line, add the range, increase line, and continue
if content[bytePos] == '\n' {
graphemeEnd = graphemePos
colstart := max(0, graphemeStart-previousLinesOffset)
colend := max(graphemeEnd-previousLinesOffset+1, colstart) // +1 its \n itself
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",
Expand All @@ -93,9 +91,8 @@ func parseMatches(

// we found it!, add highlight and continue
if bytePos == byteEnd {
graphemeEnd = graphemePos
colstart := max(0, graphemeStart-previousLinesOffset)
colend := max(graphemeEnd-previousLinesOffset, colstart)
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",
Expand All @@ -106,7 +103,6 @@ func parseMatches(
hi.lines[line] = [2]int{colstart, colend}
hi.lineEnd = line
}

}

highlights = append(highlights, hi)
Expand All @@ -125,7 +121,7 @@ type highlightInfo struct {

// coords returns the line x column of this highlight.
func (hi highlightInfo) coords() (line int, col int) {
for i := hi.lineStart; i <= hi.lineEnd; i++ {
for i := hi.lineStart; i < hi.lineEnd; i++ {
hl, ok := hi.lines[i]
if !ok {
continue
Expand Down
9 changes: 3 additions & 6 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type Model struct {

initialized bool
lines []string
lineWidths []int
longestLineWidth int

// HighlightStyle highlights the ranges set with [SetHighligths].
Expand Down Expand Up @@ -190,7 +189,7 @@ func (m *Model) SetContent(s string) {
if len(m.lines) == 1 && ansi.StringWidth(m.lines[0]) == 0 {
m.lines = nil
}
m.lineWidths, m.longestLineWidth = calcLineWidths(m.lines)
m.longestLineWidth = maxLineWidth(m.lines)
m.ClearHighlights()

if m.YOffset > len(m.lines)-1 {
Expand Down Expand Up @@ -730,15 +729,13 @@ func max(a, b int) int {
return b
}

func calcLineWidths(lines []string) ([]int, int) {
func maxLineWidth(lines []string) int {
maxlen := 0
all := make([]int, 0, len(lines))
for _, line := range lines {
llen := ansi.StringWidth(line)
all = append(all, llen+1) // account for "\n"
if llen > maxlen {
maxlen = llen
}
}
return all, maxlen
return maxlen
}

0 comments on commit 7b96ddd

Please sign in to comment.