1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
package cellbuf
import (
"strings"
"github.com/charmbracelet/x/ansi"
)
// scrollOptimize optimizes the screen to transform the old buffer into the new
// buffer.
func (s *Screen) scrollOptimize() {
height := s.newbuf.Height()
if s.oldnum == nil || len(s.oldnum) < height {
s.oldnum = make([]int, height)
}
// Calculate the indices
s.updateHashmap()
if len(s.hashtab) < height {
return
}
// Pass 1 - from top to bottom scrolling up
for i := 0; i < height; {
for i < height && (s.oldnum[i] == newIndex || s.oldnum[i] <= i) {
i++
}
if i >= height {
break
}
shift := s.oldnum[i] - i // shift > 0
start := i
i++
for i < height && s.oldnum[i] != newIndex && s.oldnum[i]-i == shift {
i++
}
end := i - 1 + shift
if !s.scrolln(shift, start, end, height-1) {
continue
}
}
// Pass 2 - from bottom to top scrolling down
for i := height - 1; i >= 0; {
for i >= 0 && (s.oldnum[i] == newIndex || s.oldnum[i] >= i) {
i--
}
if i < 0 {
break
}
shift := s.oldnum[i] - i // shift < 0
end := i
i--
for i >= 0 && s.oldnum[i] != newIndex && s.oldnum[i]-i == shift {
i--
}
start := i + 1 - (-shift)
if !s.scrolln(shift, start, end, height-1) {
continue
}
}
}
// scrolln scrolls the screen up by n lines.
func (s *Screen) scrolln(n, top, bot, maxY int) (v bool) { //nolint:unparam
const (
nonDestScrollRegion = false
memoryBelow = false
)
blank := s.clearBlank()
if n > 0 { //nolint:nestif
// Scroll up (forward)
v = s.scrollUp(n, top, bot, 0, maxY, blank)
if !v {
s.buf.WriteString(ansi.SetTopBottomMargins(top+1, bot+1))
// XXX: How should we handle this in inline mode when not using alternate screen?
s.cur.X, s.cur.Y = -1, -1
v = s.scrollUp(n, top, bot, top, bot, blank)
s.buf.WriteString(ansi.SetTopBottomMargins(1, maxY+1))
s.cur.X, s.cur.Y = -1, -1
}
if !v {
v = s.scrollIdl(n, top, bot-n+1, blank)
}
// Clear newly shifted-in lines.
if v &&
(nonDestScrollRegion || (memoryBelow && bot == maxY)) {
if bot == maxY {
s.move(0, bot-n+1)
s.clearToBottom(nil)
} else {
for i := range n {
s.move(0, bot-i)
s.clearToEnd(nil, false)
}
}
}
} else if n < 0 {
// Scroll down (backward)
v = s.scrollDown(-n, top, bot, 0, maxY, blank)
if !v {
s.buf.WriteString(ansi.SetTopBottomMargins(top+1, bot+1))
// XXX: How should we handle this in inline mode when not using alternate screen?
s.cur.X, s.cur.Y = -1, -1
v = s.scrollDown(-n, top, bot, top, bot, blank)
s.buf.WriteString(ansi.SetTopBottomMargins(1, maxY+1))
s.cur.X, s.cur.Y = -1, -1
if !v {
v = s.scrollIdl(-n, bot+n+1, top, blank)
}
// Clear newly shifted-in lines.
if v &&
(nonDestScrollRegion || (memoryBelow && top == 0)) {
for i := range -n {
s.move(0, top+i)
s.clearToEnd(nil, false)
}
}
}
}
if !v {
return v
}
s.scrollBuffer(s.curbuf, n, top, bot, blank)
// shift hash values too, they can be reused
s.scrollOldhash(n, top, bot)
return true
}
// scrollBuffer scrolls the buffer by n lines.
func (s *Screen) scrollBuffer(b *Buffer, n, top, bot int, blank *Cell) {
if top < 0 || bot < top || bot >= b.Height() {
// Nothing to scroll
return
}
if n < 0 {
// shift n lines downwards
limit := top - n
for line := bot; line >= limit && line >= 0 && line >= top; line-- {
copy(b.Lines[line], b.Lines[line+n])
}
for line := top; line < limit && line <= b.Height()-1 && line <= bot; line++ {
b.FillRect(blank, Rect(0, line, b.Width(), 1))
}
}
if n > 0 {
// shift n lines upwards
limit := bot - n
for line := top; line <= limit && line <= b.Height()-1 && line <= bot; line++ {
copy(b.Lines[line], b.Lines[line+n])
}
for line := bot; line > limit && line >= 0 && line >= top; line-- {
b.FillRect(blank, Rect(0, line, b.Width(), 1))
}
}
s.touchLine(b.Width(), b.Height(), top, bot-top+1, true)
}
// touchLine marks the line as touched.
func (s *Screen) touchLine(width, height, y, n int, changed bool) {
if n < 0 || y < 0 || y >= height {
return // Nothing to touch
}
for i := y; i < y+n && i < height; i++ {
if changed {
s.touch[i] = lineData{firstCell: 0, lastCell: width - 1}
} else {
delete(s.touch, i)
}
}
}
// scrollUp scrolls the screen up by n lines.
func (s *Screen) scrollUp(n, top, bot, minY, maxY int, blank *Cell) bool {
if n == 1 && top == minY && bot == maxY { //nolint:nestif
s.move(0, bot)
s.updatePen(blank)
s.buf.WriteByte('\n')
} else if n == 1 && bot == maxY {
s.move(0, top)
s.updatePen(blank)
s.buf.WriteString(ansi.DeleteLine(1))
} else if top == minY && bot == maxY {
supportsSU := s.caps.Contains(capSU)
if supportsSU {
s.move(0, bot)
} else {
s.move(0, top)
}
s.updatePen(blank)
if supportsSU {
s.buf.WriteString(ansi.ScrollUp(n))
} else {
s.buf.WriteString(strings.Repeat("\n", n))
}
} else if bot == maxY {
s.move(0, top)
s.updatePen(blank)
s.buf.WriteString(ansi.DeleteLine(n))
} else {
return false
}
return true
}
// scrollDown scrolls the screen down by n lines.
func (s *Screen) scrollDown(n, top, bot, minY, maxY int, blank *Cell) bool {
if n == 1 && top == minY && bot == maxY { //nolint:nestif
s.move(0, top)
s.updatePen(blank)
s.buf.WriteString(ansi.ReverseIndex)
} else if n == 1 && bot == maxY {
s.move(0, top)
s.updatePen(blank)
s.buf.WriteString(ansi.InsertLine(1))
} else if top == minY && bot == maxY {
s.move(0, top)
s.updatePen(blank)
if s.caps.Contains(capSD) {
s.buf.WriteString(ansi.ScrollDown(n))
} else {
s.buf.WriteString(strings.Repeat(ansi.ReverseIndex, n))
}
} else if bot == maxY {
s.move(0, top)
s.updatePen(blank)
s.buf.WriteString(ansi.InsertLine(n))
} else {
return false
}
return true
}
// scrollIdl scrolls the screen n lines by using [ansi.DL] at del and using
// [ansi.IL] at ins.
func (s *Screen) scrollIdl(n, del, ins int, blank *Cell) bool {
if n < 0 {
return false
}
// Delete lines
s.move(0, del)
s.updatePen(blank)
s.buf.WriteString(ansi.DeleteLine(n))
// Insert lines
s.move(0, ins)
s.updatePen(blank)
s.buf.WriteString(ansi.InsertLine(n))
return true
}
|