Scrolling performance improvement.

Every time the user scrolls, DrawLine is called many times. Each time
this happens, termbox.Flush is called, leading to unnecessary cpu usage.
This is eliminated by only calling termbox.Flush at the end of all the
DrawLine calls.
master
root 3 years ago
parent 6f57bd3697
commit 09551a6e71
  1. 41
      screen.go

@ -92,7 +92,7 @@ func (screen *Screen) ClearLine(x int, y int) *Screen {
// Increase the offset for scrolling feature by n // Increase the offset for scrolling feature by n
// Takes number of tickers as max, so not scrolling down forever // Takes number of tickers as max, so not scrolling down forever
func (screen *Screen) IncreaseOffset(n int, max int) { func (screen *Screen) IncreaseOffset(n int, max int) {
if screen.offset+n+1 < max { if screen.offset + n +1 < max {
screen.offset += n screen.offset += n
} }
} }
@ -142,6 +142,8 @@ func (screen *Screen) Draw(objects ...interface{}) *Screen {
} }
} }
termbox.Flush()
return screen return screen
} }
@ -170,6 +172,31 @@ func (screen *Screen) DrawLine(x int, y int, str string) {
termbox.Flush() termbox.Flush()
} }
// Identical to DrawLine, no flush at the end perhaps should be a part
// of DrawLine, with a flush parameter, or a wrapper function could be
// used
func (screen *Screen) DrawLineWithoutFlush(x int, y int, str string) {
start, column := 0, 0
for _, token := range screen.markup.Tokenize(str) {
// First check if it's a tag. Tags are eaten up and not displayed.
if screen.markup.IsTag(token) {
continue
}
// Here comes the actual text: display it one character at a time.
for i, char := range token {
if !screen.markup.RightAligned {
start = x + column
column++
} else {
start = screen.width - len(token) + i
}
termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.Background)
}
}
}
// Underlying workhorse function that takes multiline string, splits it into // Underlying workhorse function that takes multiline string, splits it into
// lines, and displays them row by row. // lines, and displays them row by row.
func (screen *Screen) draw(str string, offset bool) { func (screen *Screen) draw(str string, offset bool) {
@ -197,7 +224,7 @@ func (screen *Screen) draw(str string, offset bool) {
strings.Contains(allLines[row], "Change") { strings.Contains(allLines[row], "Change") {
drewHeading = true drewHeading = true
screen.headerLine = row screen.headerLine = row
screen.DrawLine(0, row, allLines[row]) screen.DrawLineWithoutFlush(0, row, allLines[row])
// move on to the point to offset to // move on to the point to offset to
row += screen.offset row += screen.offset
} }
@ -205,13 +232,13 @@ func (screen *Screen) draw(str string, offset bool) {
// only write the necessary lines // only write the necessary lines
if row <= len(allLines) && if row <= len(allLines) &&
row > screen.headerLine { row > screen.headerLine {
screen.DrawLine(0, row-screen.offset, allLines[row]) screen.DrawLineWithoutFlush(0, row-screen.offset, allLines[row])
} else if row > len(allLines) { } else if row > len(allLines) {
row = len(allLines) row = len(allLines)
} }
} }
} else { } else {
screen.DrawLine(0, row, allLines[row]) screen.DrawLineWithoutFlush(0, row, allLines[row])
} }
} }
// If the quotes lines in this cycle are shorter than in the previous // If the quotes lines in this cycle are shorter than in the previous
@ -227,8 +254,10 @@ func (screen *Screen) draw(str string, offset bool) {
// cycle. In that case, padding with blank lines would overwrite the // cycle. In that case, padding with blank lines would overwrite the
// stocks list.) // stocks list.)
if drewHeading { if drewHeading {
for i := len(allLines) - 1 - screen.offset; i < screen.height; i++ { for i := len(allLines) - 1 - screen.offset ; i < screen.height; i++ {
screen.DrawLine(0, i, blankLine) if i > screen.headerLine {
screen.DrawLine(0, i, blankLine)
}
} }
} }
} }

Loading…
Cancel
Save