Converted quotes fetcher to goroutine

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

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

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

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

Loading…
Cancel
Save