Refactored to avoid recompiling templates on each refresh

master
Michael Dvorkin 11 years ago
parent 79a1d5b508
commit 1152285520
  1. 4
      column_editor.go
  2. 46
      layout.go
  3. 6
      screen.go
  4. 5
      yahoo_market.go
  5. 5
      yahoo_quotes.go

@ -10,7 +10,6 @@ import (
type ColumnEditor struct { type ColumnEditor struct {
screen *Screen screen *Screen
layout *Layout
quotes *Quotes quotes *Quotes
profile *Profile profile *Profile
} }
@ -20,7 +19,6 @@ func (self *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEdit
self.screen = screen self.screen = screen
self.quotes = quotes self.quotes = quotes
self.profile = quotes.profile self.profile = quotes.profile
self.layout = new(Layout).Initialize()
self.select_current_column() self.select_current_column()
return self return self
@ -89,7 +87,7 @@ func (self *ColumnEditor) done() bool {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) redraw_header() { func (self *ColumnEditor) redraw_header() {
self.screen.DrawLine(0, 4, self.layout.Header(self.profile)) self.screen.DrawLine(0, 4, self.screen.layout.Header(self.profile))
termbox.Flush() termbox.Flush()
} }

@ -23,6 +23,8 @@ type Column struct {
type Layout struct { type Layout struct {
columns []Column columns []Column
regex *regexp.Regexp regex *regexp.Regexp
market_template *template.Template
quotes_template *template.Template
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -45,6 +47,8 @@ func (self *Layout) Initialize() *Layout {
{ 11, `MktCap` }, { 11, `MktCap` },
} }
self.regex = regexp.MustCompile(`(\.\d+)[MB]?$`) self.regex = regexp.MustCompile(`(\.\d+)[MB]?$`)
self.market_template = build_market_template()
self.quotes_template = build_quotes_template()
return self return self
} }
@ -55,14 +59,9 @@ func (self *Layout) Market(market *Market) string {
return err return err
} }
buffer := new(bytes.Buffer)
markup := `{{.Dow.name}}: {{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}}, {{.Sp500.name}}: {{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}}, {{.Nasdaq.name}}: {{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}}
{{.Advances.name}}: {{.Advances.nyse}} ({{.Advances.nysep}}) on NYSE and {{.Advances.nasdaq}} ({{.Advances.nasdaqp}}) on Nasdaq. {{.Declines.name}}: {{.Declines.nyse}} ({{.Declines.nysep}}) on NYSE and {{.Declines.nasdaq}} ({{.Declines.nasdaqp}}) on Nasdaq {{if .IsClosed}}<right>U.S. markets closed</right>{{end}}
New highs: {{.Highs.nyse}} on NYSE and {{.Highs.nasdaq}} on Nasdaq. New lows: {{.Lows.nyse}} on NYSE and {{.Lows.nasdaq}} on Nasdaq.`
highlight(market.Dow, market.Sp500, market.Nasdaq) highlight(market.Dow, market.Sp500, market.Nasdaq)
template,_ := template.New(`market`).Parse(markup) buffer := new(bytes.Buffer)
template.Execute(buffer, market) self.market_template.Execute(buffer, market)
return buffer.String() return buffer.String()
} }
@ -84,16 +83,7 @@ func (self *Layout) Quotes(quotes *Quotes) string {
} }
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
markup := `<right><white>{{.Now}}</></right> self.quotes_template.Execute(buffer, vars)
{{.Header}}
{{range.Stocks}}{{if .Advancing}}<green>{{end}}{{.Ticker}}{{.LastTrade}}{{.Change}}{{.ChangePct}}{{.Open}}{{.Low}}{{.High}}{{.Low52}}{{.High52}}{{.Volume}}{{.AvgVolume}}{{.PeRatio}}{{.Dividend}}{{.Yield}}{{.MarketCap}}</>
{{end}}`
template,_ := template.New(`quotes`).Parse(markup)
template.Execute(buffer, vars)
return buffer.String() return buffer.String()
} }
@ -165,6 +155,28 @@ func (self *Layout) pad(str string, col int) string {
return fmt.Sprintf(`%*s`, self.columns[col].width, str) return fmt.Sprintf(`%*s`, self.columns[col].width, str)
} }
//-----------------------------------------------------------------------------
func build_market_template() *template.Template {
markup := `{{.Dow.name}}: {{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}}, {{.Sp500.name}}: {{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}}, {{.Nasdaq.name}}: {{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}}
{{.Advances.name}}: {{.Advances.nyse}} ({{.Advances.nysep}}) on NYSE and {{.Advances.nasdaq}} ({{.Advances.nasdaqp}}) on Nasdaq. {{.Declines.name}}: {{.Declines.nyse}} ({{.Declines.nysep}}) on NYSE and {{.Declines.nasdaq}} ({{.Declines.nasdaqp}}) on Nasdaq {{if .IsClosed}}<right>U.S. markets closed</right>{{end}}
New highs: {{.Highs.nyse}} on NYSE and {{.Highs.nasdaq}} on Nasdaq. New lows: {{.Lows.nyse}} on NYSE and {{.Lows.nasdaq}} on Nasdaq.`
return template.Must(template.New(`market`).Parse(markup))
}
//-----------------------------------------------------------------------------
func build_quotes_template() *template.Template {
markup := `<right><white>{{.Now}}</></right>
{{.Header}}
{{range.Stocks}}{{if .Advancing}}<green>{{end}}{{.Ticker}}{{.LastTrade}}{{.Change}}{{.ChangePct}}{{.Open}}{{.Low}}{{.High}}{{.Low52}}{{.High52}}{{.Volume}}{{.AvgVolume}}{{.PeRatio}}{{.Dividend}}{{.Yield}}{{.MarketCap}}</>
{{end}}`
return template.Must(template.New(`quotes`).Parse(markup))
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func highlight(collections ...map[string]string) { func highlight(collections ...map[string]string) {
for _, collection := range collections { for _, collection := range collections {

@ -14,6 +14,7 @@ type Screen struct {
width int width int
height int height int
cleared bool cleared bool
layout *Layout
markup *Markup markup *Markup
} }
@ -22,6 +23,7 @@ func (self *Screen) Initialize() *Screen {
if err := termbox.Init(); err != nil { if err := termbox.Init(); err != nil {
panic(err) panic(err)
} }
self.layout = new(Layout).Initialize()
self.markup = new(Markup).Initialize() self.markup = new(Markup).Initialize()
return self.Resize() return self.Resize()
@ -56,10 +58,10 @@ func (self *Screen) Draw(objects ...interface{}) *Screen {
switch ptr.(type) { switch ptr.(type) {
case *Market: case *Market:
object := ptr.(*Market) object := ptr.(*Market)
self.draw(object.Fetch().Format()) self.draw(self.layout.Market(object.Fetch()))
case *Quotes: case *Quotes:
object := ptr.(*Quotes) object := ptr.(*Quotes)
self.draw(object.Fetch().Format()) self.draw(self.layout.Quotes(object.Fetch()))
default: default:
self.draw(ptr.(string)) self.draw(ptr.(string))
} }

@ -90,11 +90,6 @@ func (self *Market) Fetch() (this *Market) {
return self.extract(self.trim(body)) return self.extract(self.trim(body))
} }
//-----------------------------------------------------------------------------
func (self *Market) Format() string {
return new(Layout).Initialize().Market(self)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Market) Ok() (bool, string) { func (self *Market) Ok() (bool, string) {
return self.errors == ``, self.errors return self.errors == ``, self.errors

@ -110,11 +110,6 @@ func (self *Quotes) Ok() (bool, string) {
return self.errors == ``, self.errors return self.errors == ``, self.errors
} }
//-----------------------------------------------------------------------------
func (self *Quotes) Format() string {
return new(Layout).Initialize().Quotes(self)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Quotes) AddTickers(tickers []string) (added int, err error) { func (self *Quotes) AddTickers(tickers []string) (added int, err error) {
if added, err = self.profile.AddTickers(tickers); err == nil && added > 0 { if added, err = self.profile.AddTickers(tickers); err == nil && added > 0 {

Loading…
Cancel
Save