Some refactoring

master
Michael Dvorkin 11 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 {
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()
}

@ -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 += `<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 {
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 := `<u>`
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(`<r>%*s</r>`, 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)

@ -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

@ -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)
}
//-----------------------------------------------------------------------------

Loading…
Cancel
Save