Converted quotes fetcher to goroutine

master
Michael Dvorkin 12 years ago
parent b498decc9c
commit 76fef0ee0a
  1. 6
      lib/format.go
  2. 9
      lib/screen.go
  3. 9
      lib/yahoo_finance.go
  4. 15
      mop.go

@ -4,11 +4,11 @@ package mop
import (
"fmt"
"time"
"bytes"
"regexp"
"strings"
"text/template"
"time"
)
//-----------------------------------------------------------------------------
@ -24,7 +24,7 @@ func Format(quotes Quotes) string {
}
markup :=
`Hello<right>{{.Now}}</right>
`Hello<right><white>{{.Now}}</white></right>
{{.Header}}
{{range .Stocks}}{{.Color}}{{.Ticker}} {{.LastTrade}} {{.Change}} {{.ChangePercent}} {{.Open}} {{.Low}} {{.High}} {{.Low52}} {{.High52}} {{.Volume}} {{.AvgVolume}} {{.PeRatio}} {{.Dividend}} {{.Yield}} {{.MarketCap}}
@ -41,7 +41,7 @@ func Format(quotes Quotes) string {
panic(err)
}
return buffer.String()
return buffer.String()
}
func header() string {

@ -5,6 +5,7 @@ package mop
import (
"github.com/michaeldv/just"
"github.com/nsf/termbox-go"
"time"
"regexp"
"strings"
)
@ -39,6 +40,12 @@ func Draw(stocks string) {
drawScreen(Format(quotes))
}
//-----------------------------------------------------------------------------
func DrawTime() {
now := time.Now().Format("3:04:05pm PST")
drawLine(0, 0, "<right>" + now + "</right>")
}
//
// Return regular expression that matches all possible tags, i.e.
// </?black>|</?red>| ... |</?white>
@ -108,6 +115,7 @@ func drawLine(x int, y int, str string) {
column += 1
}
}
termbox.Flush()
}
//-----------------------------------------------------------------------------
@ -116,5 +124,4 @@ func drawScreen(str string) {
for row, line := range strings.Split(str, "\n") {
drawLine(0, row, line)
}
termbox.Flush()
}

@ -4,7 +4,6 @@ package mop
import (
"fmt"
"time"
"bytes"
"strings"
"net/http"
@ -59,13 +58,8 @@ type Quote struct {
}
type Quotes []Quote
var quotes Quotes
// func Get(tickers []string) Quotes {
func Get(tickers string) Quotes {
if len(quotes) > 0 && time.Now().Second() % 5 != 0 { // Fetch quotes every 5 seconds.
return quotes
}
// Format the URL and send the request.
// url := fmt.Sprintf(yahoo_finance_url, strings.Join(tickers, "+"))
@ -81,9 +75,8 @@ func Get(tickers string) Quotes {
if err != nil {
panic(err)
}
quotes = parse(sanitize(body))
return quotes
return parse(sanitize(body))
}
func (q *Quote) Color() string {

@ -18,12 +18,13 @@ func initTermbox() {
//-----------------------------------------------------------------------------
func mainLoop(profile string) {
event_queue := make(chan termbox.Event)
event_tick := time.NewTicker(1 * time.Second)
keyboard_queue := make(chan termbox.Event)
quotes_queue := time.NewTicker(5 * time.Second)
timestamp_queue := time.NewTicker(1 * time.Second)
go func() {
for {
event_queue <- termbox.PollEvent()
keyboard_queue <- termbox.PollEvent()
}
}()
@ -31,7 +32,7 @@ func mainLoop(profile string) {
loop:
for {
select {
case event := <-event_queue:
case event := <-keyboard_queue:
switch event.Type {
case termbox.EventKey:
if event.Key == termbox.KeyEsc {
@ -40,8 +41,12 @@ loop:
case termbox.EventResize:
mop.Draw(profile)
}
case <-event_tick.C:
case <-quotes_queue.C:
mop.Draw(profile)
case <-timestamp_queue.C:
mop.DrawTime()
}
}
}

Loading…
Cancel
Save