Adding tickers

master
Michael Dvorkin 11 years ago
parent 2c40c7f8bd
commit def4195b5a
  1. 66
      lib/line_editor.go
  2. BIN
      mop
  3. 2
      mop.go

@ -4,22 +4,27 @@ package mop
import (
"fmt"
"sort"
"regexp"
"strings"
"github.com/nsf/termbox-go"
)
type LineEditor struct {
command rune
prompt string
cursor int
input string
command rune
prompt string
cursor int
input string
profile *Profile
}
//-----------------------------------------------------------------------------
func (self *LineEditor) Prompt(command rune) {
func (self *LineEditor) Prompt(command rune, profile *Profile) {
prompts := map[rune]string{'+': `Add tickers: `, '-': `Remove tickers: `}
if prompt, ok := prompts[command]; ok {
self.prompt = prompt
self.command = command
self.profile = profile
DrawLine(0, 3, "<white>"+self.prompt+"</white>")
termbox.SetCursor(len(self.prompt), 3)
@ -33,13 +38,12 @@ func (self *LineEditor) Handle(ev termbox.Event) bool {
switch ev.Key {
case termbox.KeyEsc:
ClearLine(0, 3)
termbox.HideCursor()
self.done()
return true
case termbox.KeyEnter:
ClearLine(0, 3)
termbox.HideCursor()
self.execute()
self.done()
return true
case termbox.KeyBackspace, termbox.KeyBackspace2:
@ -124,3 +128,47 @@ func (self *LineEditor) jump_to_end() {
self.cursor = len(self.input)
termbox.SetCursor(len(self.prompt) + self.cursor, 3)
}
//-----------------------------------------------------------------------------
func (self *LineEditor) done() {
ClearLine(0, 3)
termbox.HideCursor()
}
//-----------------------------------------------------------------------------
func (self *LineEditor) execute() {
switch self.command {
case '+', '-':
input := strings.ToUpper(strings.TrimSpace(self.input))
tickers := regexp.MustCompile(`[,\s]+`).Split(input, -1)
if len(tickers) > 0 {
if self.command == '+' {
self.add_tickers(tickers)
} else {
self.remove_tickers(tickers)
}
}
}
}
//-----------------------------------------------------------------------------
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) {
}

BIN
mop

Binary file not shown.

@ -44,7 +44,7 @@ loop:
break loop
} else if event.Ch == '+' || event.Ch == '-' {
line_editor = new(mop.LineEditor)
line_editor.Prompt(event.Ch)
line_editor.Prompt(event.Ch, profile)
}
} else {
done := line_editor.Handle(event)

Loading…
Cancel
Save