From 76fef0ee0a47ce64b55d4998be49cc2e5b5e372d Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Tue, 9 Jul 2013 13:41:14 -0700 Subject: [PATCH] Converted quotes fetcher to goroutine --- lib/format.go | 6 +++--- lib/screen.go | 9 ++++++++- lib/yahoo_finance.go | 9 +-------- mop.go | 15 ++++++++++----- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/format.go b/lib/format.go index 917c7da..c169df8 100644 --- a/lib/format.go +++ b/lib/format.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{{.Now}} + `Hello{{.Now}} {{.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 { diff --git a/lib/screen.go b/lib/screen.go index c1e3540..2ec9049 100644 --- a/lib/screen.go +++ b/lib/screen.go @@ -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, "" + now + "") +} + // // Return regular expression that matches all possible tags, i.e. // || ... | @@ -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() } diff --git a/lib/yahoo_finance.go b/lib/yahoo_finance.go index 5333210..e38dcfa 100644 --- a/lib/yahoo_finance.go +++ b/lib/yahoo_finance.go @@ -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 { diff --git a/mop.go b/mop.go index 1e62124..bde1081 100644 --- a/mop.go +++ b/mop.go @@ -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() } } }