Finished with adding/removing tickers

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

@ -69,7 +69,7 @@ func FormatQuotes(quotes Quotes) string {
{{.Header}}
{{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)
if err != nil {

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

Loading…
Cancel
Save