Adding scroll feature

Adding an 'offset' to the quotes draw function in screen.go in order to
allow moving down the list of tickers, using PgUp/PgDown or the Up/Down
arrow keys.
master
root 3 years ago
parent e65c1bc08e
commit 6f78b90964
  1. 2
      README.md
  2. 11
      cmd/mop/main.go
  3. 41
      screen.go

@ -37,6 +37,8 @@ For demonstration purposes Mop comes preconfigured with a number of stock ticker
g Group stocks by advancing/declining issues. g Group stocks by advancing/declining issues.
f Set a filtering expression. f Set a filtering expression.
F Unset a filtering expression. F Unset a filtering expression.
PgDn Scroll Down, down arrow key also works.
PgUp Scroll up, up arrow key also works.
? Display help screen. ? Display help screen.
esc Quit mop. esc Quit mop.

@ -33,6 +33,8 @@ NO WARRANTIES OF ANY KIND WHATSOEVER. SEE THE LICENSE FILE FOR DETAILS.
g Group stocks by advancing/declining issues. g Group stocks by advancing/declining issues.
o Change column sort order. o Change column sort order.
p Pause market data and stock updates. p Pause market data and stock updates.
PgDn Scroll Down, down arrow key also works.
PgUp Scroll up, up arrow key also works.
q Quit mop. q Quit mop.
esc Ditto. esc Ditto.
@ -52,6 +54,7 @@ func mainLoop(screen *mop.Screen, profile *mop.Profile) {
marketQueue := time.NewTicker(12 * time.Second) marketQueue := time.NewTicker(12 * time.Second)
showingHelp := false showingHelp := false
paused := false paused := false
pgUpDownLines := 10
go func() { go func() {
for { for {
@ -92,6 +95,14 @@ loop:
} else if event.Ch == '?' || event.Ch == 'h' || event.Ch == 'H' { } else if event.Ch == '?' || event.Ch == 'h' || event.Ch == 'H' {
showingHelp = true showingHelp = true
screen.Clear().Draw(help) screen.Clear().Draw(help)
} else if event.Key == termbox.KeyPgdn ||
event.Key == termbox.KeyArrowDown {
screen.IncreaseOffset(pgUpDownLines, len(profile.Tickers))
screen.Clear().Draw(market, quotes)
} else if event.Key == termbox.KeyPgup ||
event.Key == termbox.KeyArrowUp {
screen.DecreaseOffset(pgUpDownLines)
screen.Clear().Draw(market, quotes)
} }
} else if lineEditor != nil { } else if lineEditor != nil {
if done := lineEditor.Handle(event); done { if done := lineEditor.Handle(event); done {

@ -22,6 +22,8 @@ type Screen struct {
layout *Layout // Pointer to layout (gets created by screen). layout *Layout // Pointer to layout (gets created by screen).
markup *Markup // Pointer to markup processor (gets created by screen). markup *Markup // Pointer to markup processor (gets created by screen).
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
headerLine int // Line number of header for scroll feature
} }
// Initializes Termbox, creates screen along with layout and markup, and // Initializes Termbox, creates screen along with layout and markup, and
@ -34,6 +36,7 @@ func NewScreen(profile *Profile) *Screen {
screen := &Screen{} screen := &Screen{}
screen.layout = NewLayout() screen.layout = NewLayout()
screen.markup = NewMarkup(profile) screen.markup = NewMarkup(profile)
screen.offset = 0
return screen.Resize() return screen.Resize()
} }
@ -86,6 +89,23 @@ func (screen *Screen) ClearLine(x int, y int) *Screen {
return screen return 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.offset += n
}
}
// Decrease the offset for scrolling feature by n
func (screen *Screen) DecreaseOffset(n int) {
if screen.offset > n {
screen.offset -= n
} else {
screen.offset = 0
}
}
// 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 {
@ -97,15 +117,15 @@ func (screen *Screen) Draw(objects ...interface{}) *Screen {
switch ptr.(type) { switch ptr.(type) {
case *Market: case *Market:
object := ptr.(*Market) object := ptr.(*Market)
screen.draw(screen.layout.Market(object.Fetch())) screen.draw(screen.layout.Market(object.Fetch()), false)
case *Quotes: case *Quotes:
object := ptr.(*Quotes) object := ptr.(*Quotes)
screen.draw(screen.layout.Quotes(object.Fetch())) screen.draw(screen.layout.Quotes(object.Fetch()), true)
case time.Time: case time.Time:
timestamp := ptr.(time.Time).Format(`3:04:05pm ` + zonename) timestamp := ptr.(time.Time).Format(`3:04:05pm ` + zonename)
screen.DrawLine(0, 0, `<right><time>`+timestamp+`</></right>`) screen.DrawLine(0, 0, `<right><time>`+timestamp+`</></right>`)
default: default:
screen.draw(ptr.(string)) screen.draw(ptr.(string), false)
} }
} }
@ -139,7 +159,7 @@ func (screen *Screen) DrawLine(x int, y int, str string) {
// Underlying workhorse function that takes multiline string, splits it into // Underlying workhorse function that takes multiline string, splits it into
// lines, and displays them row by row. // lines, and displays them row by row.
func (screen *Screen) draw(str string) { func (screen *Screen) draw(str string, offset bool) {
if !screen.cleared { if !screen.cleared {
screen.Clear() screen.Clear()
} }
@ -152,13 +172,24 @@ func (screen *Screen) draw(str string) {
// Write the lines being updated. // Write the lines being updated.
for row := 0; row < len(allLines); row++ { for row := 0; row < len(allLines); row++ {
screen.DrawLine(0, row, allLines[row]) if offset {
// Did we draw the underlined heading row? This is a crude // Did we draw the underlined heading row? This is a crude
// check, but--see comments below... // check, but--see comments below...
// --- Heading row only appears for quotes, so offset is true
if strings.Contains(allLines[row], "Ticker") && if strings.Contains(allLines[row], "Ticker") &&
strings.Contains(allLines[row], "Last") && strings.Contains(allLines[row], "Last") &&
strings.Contains(allLines[row], "Change") { strings.Contains(allLines[row], "Change") {
drewHeading = true drewHeading = true
screen.headerLine = row
screen.DrawLine(0, row, allLines[row])
} else {
if row+screen.offset < len(allLines) &&
row > screen.headerLine {
screen.DrawLine(0, row, allLines[row+screen.offset])
}
}
} else {
screen.DrawLine(0, row, allLines[row])
} }
} }
// 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