Improving draw()

Improving efficiency for drawing quotes with offset.
master
root 3 years ago
parent 7090a2c0d5
commit 4512f39638
  1. 38
      screen.go

@ -124,7 +124,6 @@ func (screen *Screen) DrawOldQuotes(quotes *Quotes) {
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 {
@ -155,33 +154,15 @@ func (screen *Screen) Draw(objects ...interface{}) *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

Loading…
Cancel
Save