From 3ca08c65225c62ba8e550f66dda38b7d590e2b2b Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Tue, 23 Jul 2013 18:30:14 -0700 Subject: [PATCH] Implemented grouping by advanced/declined --- lib/format.go | 35 +++++++++++++++++++++++++++++------ lib/profile.go | 8 ++++++++ mop.go | 3 +++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/format.go b/lib/format.go index aba5c89..f48c6e5 100644 --- a/lib/format.go +++ b/lib/format.go @@ -68,7 +68,7 @@ func (self *Formatter) format_market(m *Market) string { } //----------------------------------------------------------------------------- -func (self *Formatter) format_quotes(q *Quotes) string { +func (self *Formatter) format_quotes(quotes *Quotes) string { vars := struct { Now string Header string @@ -76,7 +76,7 @@ func (self *Formatter) format_quotes(q *Quotes) string { }{ time.Now().Format(`3:04:05pm PST`), header(), - prettify(q.stocks), + prettify(quotes), } markup := `{{.Now}} @@ -86,7 +86,7 @@ func (self *Formatter) format_quotes(q *Quotes) string { {{.Header}} {{range .Stocks}}{{.Color}}{{.Ticker}} {{.LastTrade}} {{.Change}} {{.ChangePercent}} {{.Open}} {{.Low}} {{.High}} {{.Low52}} {{.High52}} {{.Volume}} {{.AvgVolume}} {{.PeRatio}} {{.Dividend}} {{.Yield}} {{.MarketCap}} {{end}}` - + //markup += fmt.Sprintf("[%v]", quotes.profile.Grouped) template, err := template.New(`quotes`).Parse(markup) if err != nil { panic(err) @@ -123,9 +123,9 @@ func header() string { } //----------------------------------------------------------------------------- -func prettify(stocks []Stock) []Stock { - pretty := make([]Stock, len(stocks)) - for i, q := range stocks { +func prettify(quotes *Quotes) []Stock { + pretty := make([]Stock, len(quotes.stocks)) + for i, q := range group(quotes) { pretty[i].Ticker = pad(q.Ticker, -7) pretty[i].LastTrade = pad(with_currency(q.LastTrade), 9) pretty[i].Change = pad(with_currency(q.Change), 9) @@ -145,6 +145,29 @@ func prettify(stocks []Stock) []Stock { return pretty } +//----------------------------------------------------------------------------- +func group(quotes *Quotes) []Stock { + if !quotes.profile.Grouped { + return quotes.stocks + } else { + grouped := make([]Stock, len(quotes.stocks)) + current := 0 + for _,stock := range quotes.stocks { + if strings.Index(stock.Change, "-") == -1 { + grouped[current] = stock + current++ + } + } + for _,stock := range quotes.stocks { + if strings.Index(stock.Change, "-") != -1 { + grouped[current] = stock + current++ + } + } + return grouped + } +} + //----------------------------------------------------------------------------- func nullify(str string) string { if len(str) == 3 && str[0:3] == `N/A` { diff --git a/lib/profile.go b/lib/profile.go index 136d222..9d845e7 100644 --- a/lib/profile.go +++ b/lib/profile.go @@ -15,6 +15,7 @@ const moprc = `/.moprc` type Profile struct { MarketRefresh int QuotesRefresh int + Grouped bool Tickers []string SortBy string SortOrder string @@ -27,6 +28,7 @@ func (self *Profile) Initialize() *Profile { // Set default values. self.MarketRefresh = 12 self.QuotesRefresh = 5 + self.Grouped = false self.Tickers = []string{`AAPL`, `C`, `GOOG`, `IBM`, `KO`, `ORCL`, `V`} self.SortBy = `Ticker` self.SortOrder = `Desc` @@ -51,6 +53,12 @@ func (self *Profile) ListOfTickers() string { return strings.Join(self.Tickers, `+`) } +//----------------------------------------------------------------------------- +func (self *Profile) Regroup() error { + self.Grouped = !self.Grouped + return self.Save() +} + //----------------------------------------------------------------------------- func (self *Profile) AddTickers(tickers []string) (added int, err error) { added = 0 diff --git a/mop.go b/mop.go index 3b8a63b..8941cb4 100644 --- a/mop.go +++ b/mop.go @@ -38,6 +38,9 @@ loop: } else if event.Ch == '+' || event.Ch == '-' { line_editor = new(mop.LineEditor).Initialize(screen, quotes) line_editor.Prompt(event.Ch) + } else if event.Ch == 'g' { + profile.Regroup() + screen.Draw(quotes) } } else { done := line_editor.Handle(event)