Fixed bug where quotes list did not clear when shortened.

If the quotes list gets shorter (e.g. by adding a filter), then display
now overwrites the no-longer-updated quotes with blank lines.  See issue
 #85 in the mop-tracker/mop repo.
master
Adam Hayes 3 years ago
parent 6a716bb30b
commit 09f9234e7b
  1. 2
      layout.go
  2. 38
      screen.go

@ -165,12 +165,14 @@ func (layout *Layout) prettify(quotes *Quotes) []Stock {
profile := quotes.profile
if profile.Filter != ""{ // Fix for blank display if invalid filter expression was cleared.
if profile.filterExpression != nil {
if layout.filter == nil { // Initialize filter on first invocation.
layout.filter = NewFilter(profile)
}
pretty = layout.filter.Apply(pretty)
}
}
if layout.sorter == nil { // Initialize sorter on first invocation.
layout.sorter = NewSorter(profile)

@ -8,6 +8,8 @@ import (
`github.com/nsf/termbox-go`
`strings`
`time`
`strconv`
`fmt`
)
// Screen is thin wrapper aroung Termbox library to provide basic display
@ -140,7 +142,39 @@ func (screen *Screen) draw(str string) {
if !screen.cleared {
screen.Clear()
}
for row, line := range strings.Split(str, "\n") {
screen.DrawLine(0, row, line)
var allLines []string
drewHeading := false
tempFormat := "%" + strconv.Itoa(screen.width) + "s"
blankLine := fmt.Sprintf(tempFormat,"")
allLines = strings.Split(str, "\n")
// Write the lines being updated.
for row := 0; row < len(allLines); row++ {
screen.DrawLine(0, row, allLines[row])
// Did we draw the underlined heading row? This is a crude
// check, but--see comments below...
if strings.Contains(allLines[row],"Ticker") &&
strings.Contains(allLines[row],"Last") &&
strings.Contains(allLines[row],"Change") {
drewHeading = true
}
}
// If the quotes lines in this cycle are shorter than in the previous
// cycles, e.g., because a filter was just applied, then one or more
// lines from the previous cycles will not be cleared. Since the
// incoming lines don't mark explicitly whether they are part of the
// market summary or quotes, we can't check whether quotes were updated
// in a way that is robust for code changes. This is a simple test: if
// we drew the heading row ("Ticker Last Change..."), then we are
// updating the quotes section in this cycle, and we should pad the
// quotes section with blank lines. If we didn't draw the heading row,
// then we probably only updated the market summary at the top in this
// cycle. In that case, padding with blank lines would overwrite the
// stocks list.)
if drewHeading {
for i := len(allLines)-1; i < screen.height; i++ {
screen.DrawLine(0, i, blankLine)
}
}
}

Loading…
Cancel
Save