diff --git a/in.go b/in.go new file mode 100644 index 0000000..cfda811 --- /dev/null +++ b/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) +} diff --git a/lib/format.go b/lib/format.go index c9222a2..253369f 100644 --- a/lib/format.go +++ b/lib/format.go @@ -11,8 +11,21 @@ import ( `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}}: ` if m.Dow[`change`][0:1] != `-` { markup += `{{.Dow.change}} ({{.Dow.percent}}) 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 { Now string Header string diff --git a/lib/line_editor.go b/lib/line_editor.go index 5b09011..0af7a11 100644 --- a/lib/line_editor.go +++ b/lib/line_editor.go @@ -148,7 +148,7 @@ func (self *LineEditor) execute() { tickers := self.tokenize() if len(tickers) > 0 { self.quotes.profile.AddTickers(tickers) - self.screen.DrawQuotes(self.quotes) + self.screen.Draw(self.quotes) } case '-': tickers := self.tokenize() @@ -157,7 +157,7 @@ func (self *LineEditor) execute() { self.quotes.profile.RemoveTickers(tickers) after := len(self.quotes.profile.Tickers) if after < before { - self.screen.DrawQuotes(self.quotes) + self.screen.Draw(self.quotes) for i := before; i > after; i-- { self.screen.ClearLine(0, i + 4) } diff --git a/lib/screen.go b/lib/screen.go index 85d6659..9ca23c2 100644 --- a/lib/screen.go +++ b/lib/screen.go @@ -62,13 +62,15 @@ func (self *Screen) Close() { } //----------------------------------------------------------------------------- -func (self *Screen) DrawMarket(market *Market) { - self.draw(FormatMarket(market.Fetch())) -} - -//----------------------------------------------------------------------------- -func (self *Screen) DrawQuotes(quotes *Quotes) { - self.draw(FormatQuotes(quotes.Fetch())) +func (self *Screen) Draw(ptr interface{}) { + switch ptr.(type) { + case *Market: + object := ptr.(*Market) + self.draw(object.Fetch().Format()) + case *Quotes: + object := ptr.(*Quotes) + self.draw(object.Fetch().Format()) + } } //----------------------------------------------------------------------------- diff --git a/lib/yahoo_market.go b/lib/yahoo_market.go index 8a5b0bf..4b49727 100644 --- a/lib/yahoo_market.go +++ b/lib/yahoo_market.go @@ -54,6 +54,11 @@ func (self *Market) Fetch() *Market { return self.extract(self.trim(body)) } +//----------------------------------------------------------------------------- +func (self *Market) Format() string { + return new(Formatter).Format(self) +} + //----------------------------------------------------------------------------- func (self *Market) trim(body []byte) []byte { start := bytes.Index(body, []byte(`