From 5070eb61f4d5614527f87f02154e21f69f3cf227 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 8 May 2022 12:04:37 +0100 Subject: [PATCH] Scrolling improvements and loading change. Load market before quotes so something appears on screen quickly. Using screen.max to prevent scrolling off the screen. --- cmd/mop/main.go | 18 ++++++++++-------- screen.go | 26 +++++++++++++++----------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cmd/mop/main.go b/cmd/mop/main.go index c0d14df..a150347 100644 --- a/cmd/mop/main.go +++ b/cmd/mop/main.go @@ -69,7 +69,8 @@ func mainLoop(screen *mop.Screen, profile *mop.Profile) { market := mop.NewMarket() quotes := mop.NewQuotes(market, profile) - screen.Draw(market, quotes) + screen.Draw(market) + screen.Draw(quotes) loop: for { @@ -102,7 +103,7 @@ loop: screen.Clear().Draw(help) } else if event.Key == termbox.KeyPgdn || event.Ch == 'J' { - screen.IncreaseOffset(upDownJump, len(profile.Tickers)) + screen.IncreaseOffset(upDownJump) redrawQuotesFlag = true } else if event.Key == termbox.KeyPgup || event.Ch == 'K' { @@ -112,13 +113,13 @@ loop: screen.DecreaseOffset(1) redrawQuotesFlag = true } else if event.Key == termbox.KeyArrowDown || event.Ch == 'j' { - screen.IncreaseOffset(1, len(profile.Tickers)) + screen.IncreaseOffset(1) redrawQuotesFlag = true } else if event.Key == termbox.KeyHome { screen.ScrollTop() redrawQuotesFlag = true } else if event.Key == termbox.KeyEnd { - screen.ScrollBottom(len(profile.Tickers)) + screen.ScrollBottom() redrawQuotesFlag = true } } else if lineEditor != nil { @@ -136,7 +137,8 @@ loop: case termbox.EventResize: screen.Resize() if !showingHelp { - screen.Draw(market, quotes) + screen.Draw(market) + redrawQuotesFlag = true } else { screen.Draw(help) } @@ -147,7 +149,7 @@ loop: screen.DecreaseOffset(5) redrawQuotesFlag = true case termbox.MouseWheelDown: - screen.IncreaseOffset(5, len(profile.Tickers)) + screen.IncreaseOffset(5) redrawQuotesFlag = true } } @@ -159,8 +161,8 @@ loop: } case <-quotesQueue.C: - if !showingHelp && !paused { - redrawQuotesFlag = true + if !showingHelp && !paused && len(keyboardQueue) == 0 { + screen.Draw(quotes) } case <-marketQueue.C: diff --git a/screen.go b/screen.go index 27f7118..5e62329 100644 --- a/screen.go +++ b/screen.go @@ -24,6 +24,7 @@ type Screen struct { pausedAt *time.Time // Timestamp of the pause request or nil if none. offset int // Offset for scolling headerLine int // Line number of header for scroll feature + max int // highest offset } // Initializes Termbox, creates screen along with layout and markup, and @@ -91,10 +92,12 @@ func (screen *Screen) ClearLine(x int, y int) *Screen { // Increase the offset for scrolling feature by n // Takes number of tickers as max, so not scrolling down forever -func (screen *Screen) IncreaseOffset(n int, max int) { - if screen.offset + n < max - screen.height + screen.headerLine{ +func (screen *Screen) IncreaseOffset(n int) { + if screen.offset+n <= screen.max { screen.offset += n - } + } else if screen.max > screen.height { + screen.offset = screen.max + } } // Decrease the offset for scrolling feature by n @@ -110,17 +113,14 @@ func (screen *Screen) ScrollTop() { screen.offset = 0 } -func (screen *Screen) ScrollBottom(max int) { - bottom := max - screen.height + screen.headerLine - if bottom > 0 { - screen.offset = bottom - } else { - screen.offset = 0 - } +func (screen *Screen) ScrollBottom() { + if screen.max > screen.height { + screen.offset = screen.max + } } func (screen *Screen) DrawOldQuotes(quotes *Quotes) { - screen.draw(screen.layout.Quotes(quotes), true) + screen.draw(screen.layout.Quotes(quotes), true) termbox.Flush() } @@ -202,6 +202,10 @@ func (screen *Screen) draw(str string, offset bool) { blankLine := fmt.Sprintf(tempFormat, "") allLines = strings.Split(str, "\n") + if offset { + screen.max = len(allLines) - screen.height + } + // Write the lines being updated. for row := 0; row < len(allLines); row++ { if offset {