Adding tickers

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

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

Loading…
Cancel
Save