Browse Source

Reduce TextView reindexing when a scroll bar is visible

tablepad
Trevor Slocum 2 years ago
parent
commit
ac2b3155fd
  1. 2
      demos/presentation/cover.go
  2. 4
      go.mod
  3. 8
      go.sum
  4. 29
      textview.go

2
demos/presentation/cover.go

@ -11,7 +11,7 @@ import (
const logo = `
======= === === === ======== === === ===
=== === === === === === === ===
=== === === === ====== === === ===
=== === === === ====== === === ===
=== ====== === === ===========
======= == === ======== ==== ====
`

4
go.mod

@ -3,10 +3,10 @@ module gitlab.com/tslocum/cview
go 1.12
require (
github.com/gdamore/tcell/v2 v2.0.0
github.com/gdamore/tcell/v2 v2.0.1-0.20201019142633-1057d5591ed1
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mattn/go-runewidth v0.0.9
github.com/rivo/uniseg v0.1.0
gitlab.com/tslocum/cbind v0.1.3
golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca // indirect
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
)

8
go.sum

@ -1,8 +1,8 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.0.0-dev/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gdamore/tcell/v2 v2.0.0 h1:GRWG8aLfWAlekj9Q6W29bVvkHENc6hp79XOqG4AWDOs=
github.com/gdamore/tcell/v2 v2.0.0/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gdamore/tcell/v2 v2.0.1-0.20201019142633-1057d5591ed1 h1:gp9ujdOQmQf1gMvqOYYgxdMS5tRpRGE3HAgRH4Hgzd4=
github.com/gdamore/tcell/v2 v2.0.1-0.20201019142633-1057d5591ed1/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
@ -14,8 +14,8 @@ gitlab.com/tslocum/cbind v0.1.3 h1:FT/fTQ4Yj3eo5021lB3IbkIt8eVtYGhrw/xur+cjvUU=
gitlab.com/tslocum/cbind v0.1.3/go.mod h1:RvwYE3auSjBNlCmWeGspzn+jdLUVQ8C2QGC+0nP9ChI=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201013132646-2da7054afaeb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca h1:mLWBs1i4Qi5cHWGEtn2jieJQ2qtwV/gT0A2zLrmzaoE=
golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a h1:e3IU37lwO4aq3uoRKINC7JikojFmE5gO7xhfxs8VC34=
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

29
textview.go

@ -105,10 +105,16 @@ type TextView struct {
// The last bytes that have been received but are not part of the buffer yet.
recentBytes []byte
// The last width and height of the text view.
lastWidth, lastHeight int
// The processed line index. This is nil if the buffer has changed and needs
// to be re-indexed.
index []*textViewIndex
// The width of the text view buffer index.
indexWidth int
// If set to true, the buffer will be reindexed each time it is modified.
reindex bool
@ -130,9 +136,6 @@ type TextView struct {
// A set of region IDs that are currently highlighted.
highlights map[string]struct{}
// The last width for which the current table is drawn.
lastWidth int
// The screen width of the longest line in the index (not the buffer).
longestLine int
@ -782,10 +785,11 @@ func (t *TextView) SetReindexBuffer(reindex bool) {
// into the buffer from which on we will print text. It will also contain the
// color with which the line starts.
func (t *TextView) reindexBuffer(width int) {
if t.index != nil {
if t.index != nil && (!t.wrap || width == t.indexWidth) {
return // Nothing has changed. We can still use the current index.
}
t.index = nil
t.indexWidth = width
t.fromHighlight, t.toHighlight, t.posHighlight = -1, -1, -1
// If there's no space, there's no index.
@ -970,23 +974,16 @@ func (t *TextView) Draw(screen tcell.Screen) {
}
t.pageSize = height
t.reindexBuffer(width)
if t.index == nil || width != t.lastWidth || height != t.lastHeight {
t.reindexBuffer(width)
}
t.lastWidth, t.lastHeight = width, height
showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.index) > height)
if showVerticalScrollBar {
width-- // Subtract space for scroll bar.
if t.wrap {
t.index = nil
}
}
// If the width has changed, we need to reindex.
if width != t.lastWidth && t.wrap {
t.index = nil
}
t.lastWidth = width
// Re-index.
t.reindexBuffer(width)
if t.regions {
t.regionInfos = nil

Loading…
Cancel
Save