From c8583aa4d09f0960912280ab0a32d428dab8f18c Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Thu, 25 Jul 2013 16:46:21 -0700 Subject: [PATCH] Some refactoring --- lib/column_editor.go | 11 +++++------ lib/{format.go => layout.go} | 27 +++++++++++---------------- lib/yahoo_market.go | 2 +- lib/yahoo_quotes.go | 2 +- 4 files changed, 18 insertions(+), 24 deletions(-) rename lib/{format.go => layout.go} (92%) diff --git a/lib/column_editor.go b/lib/column_editor.go index b3534be..5b92260 100644 --- a/lib/column_editor.go +++ b/lib/column_editor.go @@ -10,18 +10,17 @@ import ( type ColumnEditor struct { screen *Screen + layout *Layout profile *Profile - formatter *Formatter } //----------------------------------------------------------------------------- func (self *ColumnEditor) Initialize(screen *Screen, profile *Profile) *ColumnEditor { self.screen = screen self.profile = profile - self.formatter = new(Formatter).Initialize() + self.layout = new(Layout).Initialize() self.select_current_column() - return self } @@ -57,7 +56,7 @@ func (self *ColumnEditor) select_current_column() *ColumnEditor { func (self *ColumnEditor) select_left_column() *ColumnEditor { self.profile.selected_column-- if self.profile.selected_column < 0 { - self.profile.selected_column = self.formatter.TotalColumns() - 1 + self.profile.selected_column = TotalColumns - 1 } return self } @@ -65,7 +64,7 @@ func (self *ColumnEditor) select_left_column() *ColumnEditor { //----------------------------------------------------------------------------- func (self *ColumnEditor) select_right_column() *ColumnEditor { self.profile.selected_column++ - if self.profile.selected_column > self.formatter.TotalColumns() - 1 { + if self.profile.selected_column > TotalColumns - 1 { self.profile.selected_column = 0 } return self @@ -85,7 +84,7 @@ func (self *ColumnEditor) done() bool { //----------------------------------------------------------------------------- func (self *ColumnEditor) redraw_header() { - self.screen.DrawLine(0, 4, self.formatter.DoHeader(self.profile)) + self.screen.DrawLine(0, 4, self.layout.Header(self.profile.selected_column)) termbox.Flush() } diff --git a/lib/format.go b/lib/layout.go similarity index 92% rename from lib/format.go rename to lib/layout.go index 62a4f33..a68a5e5 100644 --- a/lib/format.go +++ b/lib/layout.go @@ -11,18 +11,20 @@ import ( `time` ) +const TotalColumns = 15 + type Column struct { width int title string } -type Formatter struct { +type Layout struct { columns []Column } //----------------------------------------------------------------------------- -func (self *Formatter) Initialize() *Formatter { - self.columns = make([]Column, 15) +func (self *Layout) Initialize() *Layout { + self.columns = make([]Column, TotalColumns) self.columns[0] = Column{ -7, `Ticker`} self.columns[1] = Column{ 10, `Last`} @@ -44,7 +46,7 @@ func (self *Formatter) Initialize() *Formatter { } //----------------------------------------------------------------------------- -func (self *Formatter) DoMarket(m *Market) string { +func (self *Layout) Market(m *Market) string { markup := `{{.Dow.name}}: ` if m.Dow[`change`][0:1] != `-` { markup += `{{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}}, ` @@ -87,14 +89,14 @@ func (self *Formatter) DoMarket(m *Market) string { } //----------------------------------------------------------------------------- -func (self *Formatter) DoQuotes(quotes *Quotes) string { +func (self *Layout) Quotes(quotes *Quotes) string { vars := struct { Now string Header string Stocks []Stock }{ time.Now().Format(`3:04:05pm PST`), - self.DoHeader(quotes.profile), + self.Header(quotes.profile.selected_column), self.prettify(quotes), } @@ -121,12 +123,10 @@ func (self *Formatter) DoQuotes(quotes *Quotes) string { } //----------------------------------------------------------------------------- -func (self *Formatter) DoHeader(profile *Profile) string { - selected := profile.selected_column - +func (self *Layout) Header(selected_column int) string { str := `` for i,col := range self.columns { - if i != selected { + if i != selected_column { str += fmt.Sprintf(`%*s`, col.width, col.title) } else { str += fmt.Sprintf(`%*s`, col.width, col.title) @@ -138,12 +138,7 @@ func (self *Formatter) DoHeader(profile *Profile) string { } //----------------------------------------------------------------------------- -func (self *Formatter) TotalColumns() int { - return len(self.columns) -} - -//----------------------------------------------------------------------------- -func (self *Formatter) prettify(quotes *Quotes) []Stock { +func (self *Layout) prettify(quotes *Quotes) []Stock { pretty := make([]Stock, len(quotes.stocks)) for i, q := range group(quotes) { pretty[i].Ticker = pad(q.Ticker, self.columns[0].width) diff --git a/lib/yahoo_market.go b/lib/yahoo_market.go index a3cbaf0..83469d2 100644 --- a/lib/yahoo_market.go +++ b/lib/yahoo_market.go @@ -57,7 +57,7 @@ func (self *Market) Fetch() *Market { //----------------------------------------------------------------------------- func (self *Market) Format() string { - return new(Formatter).Initialize().DoMarket(self) + return new(Layout).Initialize().Market(self) } // private diff --git a/lib/yahoo_quotes.go b/lib/yahoo_quotes.go index 0b3aa76..ef01e44 100644 --- a/lib/yahoo_quotes.go +++ b/lib/yahoo_quotes.go @@ -95,7 +95,7 @@ func (self *Quotes) Fetch() *Quotes { //----------------------------------------------------------------------------- func (self *Quotes) Format() string { - return new(Formatter).Initialize().DoQuotes(self) + return new(Layout).Initialize().Quotes(self) } //-----------------------------------------------------------------------------