diff --git a/line_editor.go b/line_editor.go index b11496b..3170bdb 100644 --- a/line_editor.go +++ b/line_editor.go @@ -166,6 +166,9 @@ func (self *LineEditor) execute() *LineEditor { for i := before; i > after; i-- { self.screen.ClearLine(0, i + 4) } + if after == 0 { // Hide quotes header is the are no tickers left. + self.screen.ClearLine(0, 4) + } } } } diff --git a/screen.go b/screen.go index 15f60c4..32f78e8 100644 --- a/screen.go +++ b/screen.go @@ -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 { switch ptr.(type) { case *Market: @@ -57,11 +57,15 @@ func (self *Screen) Draw(objects ...interface{}) { self.draw(object.Fetch().Format()) case *Quotes: object := ptr.(*Quotes) - self.draw(object.Fetch().Format()) + if object.Ready() { + self.draw(object.Fetch().Format()) + } default: self.draw(ptr.(string)) } } + + return self } //----------------------------------------------------------------------------- diff --git a/yahoo_quotes.go b/yahoo_quotes.go index bf5fb5b..6910b30 100644 --- a/yahoo_quotes.go +++ b/yahoo_quotes.go @@ -70,9 +70,10 @@ func (self *Quotes) Initialize(market *Market, profile *Profile) *Quotes { return self } -//----------------------------------------------------------------------------- +// Fetch the latest stock quotes and parse raw fetched data into array of +// []Stock structs. func (self *Quotes) Fetch() *Quotes { - if !self.market.IsClosed || self.stocks == nil { + if self.Ready() { // Format the URL and send the request. url := fmt.Sprintf(yahoo_quotes_url, self.profile.ListOfTickers()) response, err := http.Get(url) @@ -93,6 +94,14 @@ func (self *Quotes) Fetch() *Quotes { 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 { return new(Layout).Initialize().Quotes(self)