Initial formatter refactoring

master
Michael Dvorkin 11 years ago
parent d20bd3e822
commit 9695ec3ad0
  1. 31
      in.go
  2. 17
      lib/format.go
  3. 4
      lib/line_editor.go
  4. 14
      lib/screen.go
  5. 5
      lib/yahoo_market.go
  6. 5
      lib/yahoo_quotes.go
  7. 12
      mop.go

31
in.go

@ -0,0 +1,31 @@
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
package main
import (
`fmt`
`reflect`
)
type Formatter struct {
entity interface{}
}
type Dog struct {
name string
}
func (self *Formatter) Initialize(e interface{}) *Formatter {
self.entity = e
fmt.Printf("[%v]\n", reflect.TypeOf(e).String())
return self
}
func main() {
str := `hello`
f1 := new(Formatter).Initialize(str)
dog := new(Dog)
dog.name = `Google`
f2 := new(Formatter).Initialize(dog)
fmt.Printf("[%v] [%v]\n", f1, f2)
}

@ -11,8 +11,21 @@ import (
`time` `time`
) )
type Formatter struct {}
//-----------------------------------------------------------------------------
func (self *Formatter) Format(entity interface{}) string {
switch entity.(type) {
case *Market:
return self.format_market(entity.(*Market))
case *Quotes:
return self.format_quotes(entity.(*Quotes))
}
return ``
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func FormatMarket(m *Market) string { func (self *Formatter) format_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}}, `
@ -52,7 +65,7 @@ func FormatMarket(m *Market) string {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func FormatQuotes(q *Quotes) string { func (self *Formatter) format_quotes(q *Quotes) string {
vars := struct { vars := struct {
Now string Now string
Header string Header string

@ -148,7 +148,7 @@ func (self *LineEditor) execute() {
tickers := self.tokenize() tickers := self.tokenize()
if len(tickers) > 0 { if len(tickers) > 0 {
self.quotes.profile.AddTickers(tickers) self.quotes.profile.AddTickers(tickers)
self.screen.DrawQuotes(self.quotes) self.screen.Draw(self.quotes)
} }
case '-': case '-':
tickers := self.tokenize() tickers := self.tokenize()
@ -157,7 +157,7 @@ func (self *LineEditor) execute() {
self.quotes.profile.RemoveTickers(tickers) self.quotes.profile.RemoveTickers(tickers)
after := len(self.quotes.profile.Tickers) after := len(self.quotes.profile.Tickers)
if after < before { if after < before {
self.screen.DrawQuotes(self.quotes) self.screen.Draw(self.quotes)
for i := before; i > after; i-- { for i := before; i > after; i-- {
self.screen.ClearLine(0, i + 4) self.screen.ClearLine(0, i + 4)
} }

@ -62,13 +62,15 @@ func (self *Screen) Close() {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Screen) DrawMarket(market *Market) { func (self *Screen) Draw(ptr interface{}) {
self.draw(FormatMarket(market.Fetch())) switch ptr.(type) {
case *Market:
object := ptr.(*Market)
self.draw(object.Fetch().Format())
case *Quotes:
object := ptr.(*Quotes)
self.draw(object.Fetch().Format())
} }
//-----------------------------------------------------------------------------
func (self *Screen) DrawQuotes(quotes *Quotes) {
self.draw(FormatQuotes(quotes.Fetch()))
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

@ -54,6 +54,11 @@ func (self *Market) Fetch() *Market {
return self.extract(self.trim(body)) return self.extract(self.trim(body))
} }
//-----------------------------------------------------------------------------
func (self *Market) Format() string {
return new(Formatter).Format(self)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Market) trim(body []byte) []byte { func (self *Market) trim(body []byte) []byte {
start := bytes.Index(body, []byte(`<table id="yfimktsumm"`)) start := bytes.Index(body, []byte(`<table id="yfimktsumm"`))

@ -90,6 +90,11 @@ func (self *Quotes) Fetch() *Quotes {
return self.parse(self.sanitize(body)) return self.parse(self.sanitize(body))
} }
//-----------------------------------------------------------------------------
func (self *Quotes) Format() string {
return new(Formatter).Format(self)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Quotes) parse(body []byte) *Quotes { func (self *Quotes) parse(body []byte) *Quotes {
lines := bytes.Split(body, []byte{'\n'}) lines := bytes.Split(body, []byte{'\n'})

@ -24,8 +24,8 @@ func mainLoop(screen *mop.Screen, profile *mop.Profile) {
market := new(mop.Market).Initialize().Fetch() market := new(mop.Market).Initialize().Fetch()
quotes := new(mop.Quotes).Initialize(market, profile) quotes := new(mop.Quotes).Initialize(market, profile)
screen.DrawMarket(market) screen.Draw(market)
screen.DrawQuotes(quotes) screen.Draw(quotes)
loop: loop:
for { for {
@ -48,18 +48,18 @@ loop:
} }
case termbox.EventResize: case termbox.EventResize:
screen.Resize() screen.Resize()
screen.DrawMarket(market) screen.Draw(market)
screen.DrawQuotes(quotes) screen.Draw(quotes)
} }
case <-timestamp_queue.C: case <-timestamp_queue.C:
screen.DrawTime() screen.DrawTime()
case <-quotes_queue.C: case <-quotes_queue.C:
screen.DrawQuotes(quotes) screen.Draw(quotes)
case <-market_queue.C: case <-market_queue.C:
screen.DrawMarket(market) screen.Draw(market)
} }
} }
} }

Loading…
Cancel
Save