Finished with adding/removing tickers

master
Michael Dvorkin 12 years ago
parent def4195b5a
commit a8e5c05cc1
  1. 2
      lib/format.go
  2. 46
      lib/line_editor.go
  3. 30
      lib/profile.go

@ -69,7 +69,7 @@ func FormatQuotes(quotes 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}}`
template, err := template.New("quotes").Parse(markup) template, err := template.New("quotes").Parse(markup)
if err != nil { if err != nil {

@ -4,7 +4,6 @@ package mop
import ( import (
"fmt" "fmt"
"sort"
"regexp" "regexp"
"strings" "strings"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
@ -138,37 +137,30 @@ func (self *LineEditor) done() {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) execute() { func (self *LineEditor) execute() {
switch self.command { switch self.command {
case '+', '-': case '+':
input := strings.ToUpper(strings.TrimSpace(self.input)) tickers := self.tokenize()
tickers := regexp.MustCompile(`[,\s]+`).Split(input, -1)
if len(tickers) > 0 { if len(tickers) > 0 {
if self.command == '+' { self.profile.AddTickers(tickers)
self.add_tickers(tickers) DrawQuotes(self.profile.Quotes())
} else {
self.remove_tickers(tickers)
}
} }
} case '-':
} tickers := self.tokenize()
if len(tickers) > 0 {
//----------------------------------------------------------------------------- before := len(self.profile.Tickers)
func (self *LineEditor) add_tickers(tickers []string) { self.profile.RemoveTickers(tickers)
existing := make(map[string]bool) after := len(self.profile.Tickers)
for _, ticker := range self.profile.Tickers { if after < before {
existing[ticker] = true DrawQuotes(self.profile.Quotes())
} for i := before; i > after; i-- {
ClearLine(0, i + 4)
for _, ticker := range tickers { }
if _, found := existing[ticker]; !found { }
self.profile.Tickers = append(self.profile.Tickers, ticker)
} }
} }
sort.Strings(self.profile.Tickers)
self.profile.Save()
DrawQuotes(self.profile.Quotes())
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) remove_tickers(tickers []string) { func (self *LineEditor) tokenize() []string {
input := strings.ToUpper(strings.TrimSpace(self.input))
return regexp.MustCompile(`[,\s]+`).Split(input, -1)
} }

@ -3,6 +3,7 @@
package mop package mop
import ( import (
"sort"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os/user" "os/user"
@ -50,6 +51,35 @@ func (self *Profile) Quotes() string {
return strings.Join(self.Tickers, "+") return strings.Join(self.Tickers, "+")
} }
//-----------------------------------------------------------------------------
func (self *Profile) AddTickers(tickers []string) {
existing := make(map[string]bool)
for _, ticker := range self.Tickers {
existing[ticker] = true
}
for _, ticker := range tickers {
if _, found := existing[ticker]; !found {
self.Tickers = append(self.Tickers, ticker)
}
}
sort.Strings(self.Tickers)
self.Save()
}
//-----------------------------------------------------------------------------
func (self *Profile) RemoveTickers(tickers []string) {
for _, ticker := range tickers {
for i, existing := range self.Tickers {
if ticker == existing { // Requested ticker is there: remove i-th slice item.
self.Tickers = append(self.Tickers[:i], self.Tickers[i+1:]...)
}
}
}
self.Save()
}
// private // private
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Profile) default_file_name() string { func (self *Profile) default_file_name() string {

Loading…
Cancel
Save