More robust pause implementation

master
Michael Dvorkin 11 years ago
parent 3167e1866e
commit 5e764402b9
  1. 4
      cmd/mop.go
  2. 33
      screen.go

@ -68,7 +68,7 @@ loop:
}
} else if event.Ch == 'p' || event.Ch == 'P' {
paused = !paused
screen.Draw(paused)
screen.Pause(paused).Draw(time.Now())
} else if event.Ch == '?' || event.Ch == 'h' || event.Ch == 'H' {
showingHelp = true
screen.Clear().Draw(help)
@ -82,7 +82,7 @@ loop:
columnEditor = nil
}
} else if showingHelp {
showingHelp, paused = false, false
showingHelp = false
screen.Clear().Draw(market, quotes)
}
case termbox.EventResize:

@ -13,11 +13,12 @@ import (
// Screen is thin wrapper aroung Termbox library to provide basic display
// capabilities as requied by Mop.
type Screen struct {
width int // Current number of columns.
height int // Current number of rows.
cleared bool // True after the screens gets cleared.
layout *Layout // Pointer to layout (gets created by screen).
markup *Markup // Pointer to markup processor (gets created by screen).
width int // Current number of columns.
height int // Current number of rows.
cleared bool // True after the screens gets cleared.
layout *Layout // Pointer to layout (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.
}
// Initialize loads the Termbox, allocates and initializes layout and markup,
@ -49,6 +50,17 @@ func (screen *Screen) Resize() *Screen {
return screen
}
func (screen *Screen) Pause(pause bool) *Screen {
if pause {
screen.pausedAt = new(time.Time)
*screen.pausedAt = time.Now()
} else {
screen.pausedAt = nil
}
return screen
}
// Clear makes the entire screen blank using default background color.
func (screen *Screen) Clear() *Screen {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
@ -71,6 +83,9 @@ func (screen *Screen) ClearLine(x int, y int) *Screen {
// Draw accepts variable number of arguments and knows how to display the
// market data, stock quotes, current time, and an arbitrary string.
func (screen *Screen) Draw(objects ...interface{}) *Screen {
if screen.pausedAt != nil {
defer screen.DrawLine(0, 0, `<right><r>` + screen.pausedAt.Format(`3:04:05pm PST`) + `</r></right>`)
}
for _, ptr := range objects {
switch ptr.(type) {
case *Market:
@ -82,14 +97,6 @@ func (screen *Screen) Draw(objects ...interface{}) *Screen {
case time.Time:
timestamp := ptr.(time.Time).Format(`3:04:05pm PST`)
screen.DrawLine(0, 0, `<right>` + timestamp + `</right>`)
case bool:
timestamp := time.Now().Format(`3:04:05pm PST`)
if ptr.(bool) {
timestamp = `<r>Paused ` + timestamp + `</r>`
} else {
timestamp = ` ` + timestamp
}
screen.DrawLine(0, 0, `<right>` + timestamp + `</right>`)
default:
screen.draw(ptr.(string))
}

Loading…
Cancel
Save