From 11522855205d6631622d17f1bb62480f840cd0b0 Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Thu, 8 Aug 2013 22:26:14 -0700 Subject: [PATCH] Refactored to avoid recompiling templates on each refresh --- column_editor.go | 4 +--- layout.go | 50 ++++++++++++++++++++++++++++++------------------ screen.go | 6 ++++-- yahoo_market.go | 5 ----- yahoo_quotes.go | 5 ----- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/column_editor.go b/column_editor.go index 74fb19d..78eebe9 100644 --- a/column_editor.go +++ b/column_editor.go @@ -10,7 +10,6 @@ import ( type ColumnEditor struct { screen *Screen - layout *Layout quotes *Quotes profile *Profile } @@ -20,7 +19,6 @@ func (self *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEdit self.screen = screen self.quotes = quotes self.profile = quotes.profile - self.layout = new(Layout).Initialize() self.select_current_column() return self @@ -89,7 +87,7 @@ func (self *ColumnEditor) done() bool { //----------------------------------------------------------------------------- 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() } diff --git a/layout.go b/layout.go index 65ef670..9c84e49 100644 --- a/layout.go +++ b/layout.go @@ -21,8 +21,10 @@ type Column struct { } type Layout struct { - columns []Column - regex *regexp.Regexp + columns []Column + regex *regexp.Regexp + market_template *template.Template + quotes_template *template.Template } //----------------------------------------------------------------------------- @@ -45,6 +47,8 @@ func (self *Layout) Initialize() *Layout { { 11, `MktCap` }, } self.regex = regexp.MustCompile(`(\.\d+)[MB]?$`) + self.market_template = build_market_template() + self.quotes_template = build_quotes_template() return self } @@ -55,14 +59,9 @@ func (self *Layout) Market(market *Market) string { 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}}U.S. markets closed{{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) - template,_ := template.New(`market`).Parse(markup) - template.Execute(buffer, market) + buffer := new(bytes.Buffer) + self.market_template.Execute(buffer, market) return buffer.String() } @@ -84,16 +83,7 @@ func (self *Layout) Quotes(quotes *Quotes) string { } buffer := new(bytes.Buffer) - markup := `{{.Now}} - - - -{{.Header}} -{{range.Stocks}}{{if .Advancing}}{{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) + self.quotes_template.Execute(buffer, vars) 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) } +//----------------------------------------------------------------------------- +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}}U.S. markets closed{{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 := `{{.Now}} + + + +{{.Header}} +{{range.Stocks}}{{if .Advancing}}{{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) { for _, collection := range collections { diff --git a/screen.go b/screen.go index a1eb05e..552c964 100644 --- a/screen.go +++ b/screen.go @@ -14,6 +14,7 @@ type Screen struct { width int height int cleared bool + layout *Layout markup *Markup } @@ -22,6 +23,7 @@ func (self *Screen) Initialize() *Screen { if err := termbox.Init(); err != nil { panic(err) } + self.layout = new(Layout).Initialize() self.markup = new(Markup).Initialize() return self.Resize() @@ -56,10 +58,10 @@ func (self *Screen) Draw(objects ...interface{}) *Screen { switch ptr.(type) { case *Market: object := ptr.(*Market) - self.draw(object.Fetch().Format()) + self.draw(self.layout.Market(object.Fetch())) case *Quotes: object := ptr.(*Quotes) - self.draw(object.Fetch().Format()) + self.draw(self.layout.Quotes(object.Fetch())) default: self.draw(ptr.(string)) } diff --git a/yahoo_market.go b/yahoo_market.go index 8cb9e5f..ecc7d70 100644 --- a/yahoo_market.go +++ b/yahoo_market.go @@ -90,11 +90,6 @@ func (self *Market) Fetch() (this *Market) { return self.extract(self.trim(body)) } -//----------------------------------------------------------------------------- -func (self *Market) Format() string { - return new(Layout).Initialize().Market(self) -} - //----------------------------------------------------------------------------- func (self *Market) Ok() (bool, string) { return self.errors == ``, self.errors diff --git a/yahoo_quotes.go b/yahoo_quotes.go index 60268c6..554171f 100644 --- a/yahoo_quotes.go +++ b/yahoo_quotes.go @@ -110,11 +110,6 @@ func (self *Quotes) Ok() (bool, string) { 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) { if added, err = self.profile.AddTickers(tickers); err == nil && added > 0 {