Scrolling improvements and loading change.

Load market before quotes so something appears on screen quickly.

Using screen.max to prevent scrolling off the screen.
master
root 3 years ago
parent 83b716e9a4
commit 5070eb61f4
  1. 18
      cmd/mop/main.go
  2. 20
      screen.go

@ -69,7 +69,8 @@ func mainLoop(screen *mop.Screen, profile *mop.Profile) {
market := mop.NewMarket() market := mop.NewMarket()
quotes := mop.NewQuotes(market, profile) quotes := mop.NewQuotes(market, profile)
screen.Draw(market, quotes) screen.Draw(market)
screen.Draw(quotes)
loop: loop:
for { for {
@ -102,7 +103,7 @@ loop:
screen.Clear().Draw(help) screen.Clear().Draw(help)
} else if event.Key == termbox.KeyPgdn || } else if event.Key == termbox.KeyPgdn ||
event.Ch == 'J' { event.Ch == 'J' {
screen.IncreaseOffset(upDownJump, len(profile.Tickers)) screen.IncreaseOffset(upDownJump)
redrawQuotesFlag = true redrawQuotesFlag = true
} else if event.Key == termbox.KeyPgup || } else if event.Key == termbox.KeyPgup ||
event.Ch == 'K' { event.Ch == 'K' {
@ -112,13 +113,13 @@ loop:
screen.DecreaseOffset(1) screen.DecreaseOffset(1)
redrawQuotesFlag = true redrawQuotesFlag = true
} else if event.Key == termbox.KeyArrowDown || event.Ch == 'j' { } else if event.Key == termbox.KeyArrowDown || event.Ch == 'j' {
screen.IncreaseOffset(1, len(profile.Tickers)) screen.IncreaseOffset(1)
redrawQuotesFlag = true redrawQuotesFlag = true
} else if event.Key == termbox.KeyHome { } else if event.Key == termbox.KeyHome {
screen.ScrollTop() screen.ScrollTop()
redrawQuotesFlag = true redrawQuotesFlag = true
} else if event.Key == termbox.KeyEnd { } else if event.Key == termbox.KeyEnd {
screen.ScrollBottom(len(profile.Tickers)) screen.ScrollBottom()
redrawQuotesFlag = true redrawQuotesFlag = true
} }
} else if lineEditor != nil { } else if lineEditor != nil {
@ -136,7 +137,8 @@ loop:
case termbox.EventResize: case termbox.EventResize:
screen.Resize() screen.Resize()
if !showingHelp { if !showingHelp {
screen.Draw(market, quotes) screen.Draw(market)
redrawQuotesFlag = true
} else { } else {
screen.Draw(help) screen.Draw(help)
} }
@ -147,7 +149,7 @@ loop:
screen.DecreaseOffset(5) screen.DecreaseOffset(5)
redrawQuotesFlag = true redrawQuotesFlag = true
case termbox.MouseWheelDown: case termbox.MouseWheelDown:
screen.IncreaseOffset(5, len(profile.Tickers)) screen.IncreaseOffset(5)
redrawQuotesFlag = true redrawQuotesFlag = true
} }
} }
@ -159,8 +161,8 @@ loop:
} }
case <-quotesQueue.C: case <-quotesQueue.C:
if !showingHelp && !paused { if !showingHelp && !paused && len(keyboardQueue) == 0 {
redrawQuotesFlag = true screen.Draw(quotes)
} }
case <-marketQueue.C: case <-marketQueue.C:

@ -24,6 +24,7 @@ type Screen struct {
pausedAt *time.Time // Timestamp of the pause request or nil if none. pausedAt *time.Time // Timestamp of the pause request or nil if none.
offset int // Offset for scolling offset int // Offset for scolling
headerLine int // Line number of header for scroll feature headerLine int // Line number of header for scroll feature
max int // highest offset
} }
// Initializes Termbox, creates screen along with layout and markup, and // Initializes Termbox, creates screen along with layout and markup, and
@ -91,9 +92,11 @@ 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) {
if screen.offset + n < max - screen.height + screen.headerLine{ if screen.offset+n <= screen.max {
screen.offset += n screen.offset += n
} else if screen.max > screen.height {
screen.offset = screen.max
} }
} }
@ -110,12 +113,9 @@ func (screen *Screen) ScrollTop() {
screen.offset = 0 screen.offset = 0
} }
func (screen *Screen) ScrollBottom(max int) { func (screen *Screen) ScrollBottom() {
bottom := max - screen.height + screen.headerLine if screen.max > screen.height {
if bottom > 0 { screen.offset = screen.max
screen.offset = bottom
} else {
screen.offset = 0
} }
} }
@ -202,6 +202,10 @@ func (screen *Screen) draw(str string, offset bool) {
blankLine := fmt.Sprintf(tempFormat, "") blankLine := fmt.Sprintf(tempFormat, "")
allLines = strings.Split(str, "\n") allLines = strings.Split(str, "\n")
if offset {
screen.max = len(allLines) - screen.height
}
// Write the lines being updated. // Write the lines being updated.
for row := 0; row < len(allLines); row++ { for row := 0; row < len(allLines); row++ {
if offset { if offset {

Loading…
Cancel
Save