Improving draw()

Improving efficiency for drawing quotes with offset.
master
root 3 years ago
parent 7090a2c0d5
commit 4512f39638
  1. 52
      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
} }
} }
@ -121,10 +121,9 @@ func (screen *Screen) ScrollBottom(max int) {
func (screen *Screen) DrawOldQuotes(quotes *Quotes) { func (screen *Screen) DrawOldQuotes(quotes *Quotes) {
screen.draw(screen.layout.Quotes(quotes), true) screen.draw(screen.layout.Quotes(quotes), true)
termbox.Flush() termbox.Flush()
} }
// Draw accepts variable number of arguments and knows how to display the // Draw accepts variable number of arguments and knows how to display the
// market data, stock quotes, current time, and an arbitrary string. // market data, stock quotes, current time, and an arbitrary string.
func (screen *Screen) Draw(objects ...interface{}) *Screen { func (screen *Screen) Draw(objects ...interface{}) *Screen {
@ -148,40 +147,22 @@ func (screen *Screen) Draw(objects ...interface{}) *Screen {
} }
} }
termbox.Flush() termbox.Flush()
return screen return screen
} }
// DrawLine takes the incoming string, tokenizes it to extract markup // DrawLine takes the incoming string, tokenizes it to extract markup
// elements, and displays it all starting at (x,y) location. // elements, and displays it all starting at (x,y) location.
func (screen *Screen) DrawLine(x int, y int, str string) {
start, column := 0, 0
for _, token := range screen.markup.Tokenize(str) { // DrawLineFlush gives the option to flush screen after drawing
// 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. // wrapper for DrawLineFlush
for i, char := range token { func (screen *Screen) DrawLine(x int, y int, str string) {
if !screen.markup.RightAligned { screen.DrawLineFlush(x, y, str, true)
start = x + column
column++
} else {
start = screen.width - len(token) + i
}
termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.Background)
}
}
termbox.Flush()
} }
// Identical to DrawLine, no flush at the end perhaps should be a part func (screen *Screen) DrawLineFlush(x int, y int, str string, flush bool) {
// 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 start, column := 0, 0
for _, token := range screen.markup.Tokenize(str) { for _, token := range screen.markup.Tokenize(str) {
@ -201,6 +182,9 @@ func (screen *Screen) DrawLineWithoutFlush(x int, y int, str string) {
termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.Background) termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.Background)
} }
} }
if flush {
termbox.Flush()
}
} }
// Underlying workhorse function that takes multiline string, splits it into // Underlying workhorse function that takes multiline string, splits it into
@ -230,7 +214,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.DrawLineWithoutFlush(0, row, allLines[row]) screen.DrawLine(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
} }
@ -238,13 +222,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.DrawLineWithoutFlush(0, row-screen.offset, allLines[row]) screen.DrawLineFlush(0, row-screen.offset, allLines[row], false)
} else if row > len(allLines) { } else if row > len(allLines) {
row = len(allLines) row = len(allLines)
} }
} }
} else { } else {
screen.DrawLineWithoutFlush(0, row, allLines[row]) screen.DrawLineFlush(0, row, allLines[row], false)
} }
} }
// 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
@ -260,10 +244,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++ {
if i > screen.headerLine { if i > screen.headerLine {
screen.DrawLine(0, i, blankLine) screen.DrawLine(0, i, blankLine)
} }
} }
} }
} }

Loading…
Cancel
Save