No updates when the list of tickers is empty

master
Michael Dvorkin 11 years ago
parent ef65f51624
commit a79b4605df
  1. 3
      line_editor.go
  2. 8
      screen.go
  3. 13
      yahoo_quotes.go

@ -166,6 +166,9 @@ func (self *LineEditor) execute() *LineEditor {
for i := before; i > after; i-- { for i := before; i > after; i-- {
self.screen.ClearLine(0, i + 4) self.screen.ClearLine(0, i + 4)
} }
if after == 0 { // Hide quotes header is the are no tickers left.
self.screen.ClearLine(0, 4)
}
} }
} }
} }

@ -49,7 +49,7 @@ func (self *Screen) Close() *Screen {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Screen) Draw(objects ...interface{}) { func (self *Screen) Draw(objects ...interface{}) *Screen {
for _, ptr := range objects { for _, ptr := range objects {
switch ptr.(type) { switch ptr.(type) {
case *Market: case *Market:
@ -57,11 +57,15 @@ func (self *Screen) Draw(objects ...interface{}) {
self.draw(object.Fetch().Format()) self.draw(object.Fetch().Format())
case *Quotes: case *Quotes:
object := ptr.(*Quotes) object := ptr.(*Quotes)
self.draw(object.Fetch().Format()) if object.Ready() {
self.draw(object.Fetch().Format())
}
default: default:
self.draw(ptr.(string)) self.draw(ptr.(string))
} }
} }
return self
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

@ -70,9 +70,10 @@ func (self *Quotes) Initialize(market *Market, profile *Profile) *Quotes {
return self return self
} }
//----------------------------------------------------------------------------- // Fetch the latest stock quotes and parse raw fetched data into array of
// []Stock structs.
func (self *Quotes) Fetch() *Quotes { func (self *Quotes) Fetch() *Quotes {
if !self.market.IsClosed || self.stocks == nil { if self.Ready() {
// Format the URL and send the request. // Format the URL and send the request.
url := fmt.Sprintf(yahoo_quotes_url, self.profile.ListOfTickers()) url := fmt.Sprintf(yahoo_quotes_url, self.profile.ListOfTickers())
response, err := http.Get(url) response, err := http.Get(url)
@ -93,6 +94,14 @@ func (self *Quotes) Fetch() *Quotes {
return self return self
} }
// Return true if we haven't fetched the quotes yet *or* the stock market is
// still open and we might want to grab the latest quotes. In both cases we
// make sure the list of requested tickers is not empty.
func (self *Quotes) Ready() bool {
return (self.stocks == nil || !self.market.IsClosed) && len(self.profile.Tickers) > 0
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Quotes) Format() string { func (self *Quotes) Format() string {
return new(Layout).Initialize().Quotes(self) return new(Layout).Initialize().Quotes(self)

Loading…
Cancel
Save