More robust pause implementation

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

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

@ -18,6 +18,7 @@ type Screen struct {
cleared bool // True after the screens gets cleared. cleared bool // True after the screens gets cleared.
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.
} }
// Initialize loads the Termbox, allocates and initializes layout and markup, // Initialize loads the Termbox, allocates and initializes layout and markup,
@ -49,6 +50,17 @@ func (screen *Screen) Resize() *Screen {
return 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. // Clear makes the entire screen blank using default background color.
func (screen *Screen) Clear() *Screen { func (screen *Screen) Clear() *Screen {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) 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 // 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 {
if screen.pausedAt != nil {
defer screen.DrawLine(0, 0, `<right><r>` + screen.pausedAt.Format(`3:04:05pm PST`) + `</r></right>`)
}
for _, ptr := range objects { for _, ptr := range objects {
switch ptr.(type) { switch ptr.(type) {
case *Market: case *Market:
@ -82,14 +97,6 @@ func (screen *Screen) Draw(objects ...interface{}) *Screen {
case time.Time: case time.Time:
timestamp := ptr.(time.Time).Format(`3:04:05pm PST`) timestamp := ptr.(time.Time).Format(`3:04:05pm PST`)
screen.DrawLine(0, 0, `<right>` + timestamp + `</right>`) 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: default:
screen.draw(ptr.(string)) screen.draw(ptr.(string))
} }

Loading…
Cancel
Save