Implemented grouping by advanced/declined

master
Michael Dvorkin 12 years ago
parent 233e5122d6
commit 3ca08c6522
  1. 35
      lib/format.go
  2. 8
      lib/profile.go
  3. 3
      mop.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 { vars := struct {
Now string Now string
Header string Header string
@ -76,7 +76,7 @@ func (self *Formatter) format_quotes(q *Quotes) string {
}{ }{
time.Now().Format(`3:04:05pm PST`), time.Now().Format(`3:04:05pm PST`),
header(), header(),
prettify(q.stocks), prettify(quotes),
} }
markup := `<right><white>{{.Now}}</white></right> markup := `<right><white>{{.Now}}</white></right>
@ -86,7 +86,7 @@ func (self *Formatter) format_quotes(q *Quotes) string {
{{.Header}} {{.Header}}
{{range .Stocks}}{{.Color}}{{.Ticker}} {{.LastTrade}} {{.Change}} {{.ChangePercent}} {{.Open}} {{.Low}} {{.High}} {{.Low52}} {{.High52}} {{.Volume}} {{.AvgVolume}} {{.PeRatio}} {{.Dividend}} {{.Yield}} {{.MarketCap}} {{range .Stocks}}{{.Color}}{{.Ticker}} {{.LastTrade}} {{.Change}} {{.ChangePercent}} {{.Open}} {{.Low}} {{.High}} {{.Low52}} {{.High52}} {{.Volume}} {{.AvgVolume}} {{.PeRatio}} {{.Dividend}} {{.Yield}} {{.MarketCap}}
{{end}}` {{end}}`
//markup += fmt.Sprintf("[%v]", quotes.profile.Grouped)
template, err := template.New(`quotes`).Parse(markup) template, err := template.New(`quotes`).Parse(markup)
if err != nil { if err != nil {
panic(err) panic(err)
@ -123,9 +123,9 @@ func header() string {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func prettify(stocks []Stock) []Stock { func prettify(quotes *Quotes) []Stock {
pretty := make([]Stock, len(stocks)) pretty := make([]Stock, len(quotes.stocks))
for i, q := range stocks { for i, q := range group(quotes) {
pretty[i].Ticker = pad(q.Ticker, -7) pretty[i].Ticker = pad(q.Ticker, -7)
pretty[i].LastTrade = pad(with_currency(q.LastTrade), 9) pretty[i].LastTrade = pad(with_currency(q.LastTrade), 9)
pretty[i].Change = pad(with_currency(q.Change), 9) pretty[i].Change = pad(with_currency(q.Change), 9)
@ -145,6 +145,29 @@ func prettify(stocks []Stock) []Stock {
return pretty 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 { func nullify(str string) string {
if len(str) == 3 && str[0:3] == `N/A` { if len(str) == 3 && str[0:3] == `N/A` {

@ -15,6 +15,7 @@ const moprc = `/.moprc`
type Profile struct { type Profile struct {
MarketRefresh int MarketRefresh int
QuotesRefresh int QuotesRefresh int
Grouped bool
Tickers []string Tickers []string
SortBy string SortBy string
SortOrder string SortOrder string
@ -27,6 +28,7 @@ func (self *Profile) Initialize() *Profile {
// Set default values. // Set default values.
self.MarketRefresh = 12 self.MarketRefresh = 12
self.QuotesRefresh = 5 self.QuotesRefresh = 5
self.Grouped = false
self.Tickers = []string{`AAPL`, `C`, `GOOG`, `IBM`, `KO`, `ORCL`, `V`} self.Tickers = []string{`AAPL`, `C`, `GOOG`, `IBM`, `KO`, `ORCL`, `V`}
self.SortBy = `Ticker` self.SortBy = `Ticker`
self.SortOrder = `Desc` self.SortOrder = `Desc`
@ -51,6 +53,12 @@ func (self *Profile) ListOfTickers() string {
return strings.Join(self.Tickers, `+`) 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) { func (self *Profile) AddTickers(tickers []string) (added int, err error) {
added = 0 added = 0

@ -38,6 +38,9 @@ loop:
} else if event.Ch == '+' || event.Ch == '-' { } else if event.Ch == '+' || event.Ch == '-' {
line_editor = new(mop.LineEditor).Initialize(screen, quotes) line_editor = new(mop.LineEditor).Initialize(screen, quotes)
line_editor.Prompt(event.Ch) line_editor.Prompt(event.Ch)
} else if event.Ch == 'g' {
profile.Regroup()
screen.Draw(quotes)
} }
} else { } else {
done := line_editor.Handle(event) done := line_editor.Handle(event)

Loading…
Cancel
Save