Some refactoring

master
Michael Dvorkin 12 years ago
parent 7668f80e41
commit c8583aa4d0
  1. 11
      lib/column_editor.go
  2. 27
      lib/layout.go
  3. 2
      lib/yahoo_market.go
  4. 2
      lib/yahoo_quotes.go

@ -10,18 +10,17 @@ import (
type ColumnEditor struct { type ColumnEditor struct {
screen *Screen screen *Screen
layout *Layout
profile *Profile profile *Profile
formatter *Formatter
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) Initialize(screen *Screen, profile *Profile) *ColumnEditor { func (self *ColumnEditor) Initialize(screen *Screen, profile *Profile) *ColumnEditor {
self.screen = screen self.screen = screen
self.profile = profile self.profile = profile
self.formatter = new(Formatter).Initialize() self.layout = new(Layout).Initialize()
self.select_current_column() self.select_current_column()
return self return self
} }
@ -57,7 +56,7 @@ func (self *ColumnEditor) select_current_column() *ColumnEditor {
func (self *ColumnEditor) select_left_column() *ColumnEditor { func (self *ColumnEditor) select_left_column() *ColumnEditor {
self.profile.selected_column-- self.profile.selected_column--
if self.profile.selected_column < 0 { if self.profile.selected_column < 0 {
self.profile.selected_column = self.formatter.TotalColumns() - 1 self.profile.selected_column = TotalColumns - 1
} }
return self return self
} }
@ -65,7 +64,7 @@ func (self *ColumnEditor) select_left_column() *ColumnEditor {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) select_right_column() *ColumnEditor { func (self *ColumnEditor) select_right_column() *ColumnEditor {
self.profile.selected_column++ 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 self.profile.selected_column = 0
} }
return self return self
@ -85,7 +84,7 @@ func (self *ColumnEditor) done() bool {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) redraw_header() { 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() termbox.Flush()
} }

@ -11,18 +11,20 @@ import (
`time` `time`
) )
const TotalColumns = 15
type Column struct { type Column struct {
width int width int
title string title string
} }
type Formatter struct { type Layout struct {
columns []Column columns []Column
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Formatter) Initialize() *Formatter { func (self *Layout) Initialize() *Layout {
self.columns = make([]Column, 15) self.columns = make([]Column, TotalColumns)
self.columns[0] = Column{ -7, `Ticker`} self.columns[0] = Column{ -7, `Ticker`}
self.columns[1] = Column{ 10, `Last`} 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}}: ` markup := `{{.Dow.name}}: `
if m.Dow[`change`][0:1] != `-` { if m.Dow[`change`][0:1] != `-` {
markup += `<green>{{.Dow.change}} ({{.Dow.percent}})</green> at {{.Dow.latest}}, ` markup += `<green>{{.Dow.change}} ({{.Dow.percent}})</green> 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 { vars := struct {
Now string Now string
Header string Header string
Stocks []Stock Stocks []Stock
}{ }{
time.Now().Format(`3:04:05pm PST`), time.Now().Format(`3:04:05pm PST`),
self.DoHeader(quotes.profile), self.Header(quotes.profile.selected_column),
self.prettify(quotes), self.prettify(quotes),
} }
@ -121,12 +123,10 @@ func (self *Formatter) DoQuotes(quotes *Quotes) string {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Formatter) DoHeader(profile *Profile) string { func (self *Layout) Header(selected_column int) string {
selected := profile.selected_column
str := `<u>` str := `<u>`
for i,col := range self.columns { for i,col := range self.columns {
if i != selected { if i != selected_column {
str += fmt.Sprintf(`%*s`, col.width, col.title) str += fmt.Sprintf(`%*s`, col.width, col.title)
} else { } else {
str += fmt.Sprintf(`<r>%*s</r>`, col.width, col.title) str += fmt.Sprintf(`<r>%*s</r>`, col.width, col.title)
@ -138,12 +138,7 @@ func (self *Formatter) DoHeader(profile *Profile) string {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Formatter) TotalColumns() int { func (self *Layout) prettify(quotes *Quotes) []Stock {
return len(self.columns)
}
//-----------------------------------------------------------------------------
func (self *Formatter) prettify(quotes *Quotes) []Stock {
pretty := make([]Stock, len(quotes.stocks)) pretty := make([]Stock, len(quotes.stocks))
for i, q := range group(quotes) { for i, q := range group(quotes) {
pretty[i].Ticker = pad(q.Ticker, self.columns[0].width) pretty[i].Ticker = pad(q.Ticker, self.columns[0].width)

@ -57,7 +57,7 @@ func (self *Market) Fetch() *Market {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Market) Format() string { func (self *Market) Format() string {
return new(Formatter).Initialize().DoMarket(self) return new(Layout).Initialize().Market(self)
} }
// private // private

@ -95,7 +95,7 @@ func (self *Quotes) Fetch() *Quotes {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Quotes) Format() string { func (self *Quotes) Format() string {
return new(Formatter).Initialize().DoQuotes(self) return new(Layout).Initialize().Quotes(self)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

Loading…
Cancel
Save