|
|
|
@ -4,6 +4,9 @@ package mop |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"sort" |
|
|
|
|
"regexp" |
|
|
|
|
"strings" |
|
|
|
|
"github.com/nsf/termbox-go" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
@ -12,14 +15,16 @@ type LineEditor struct { |
|
|
|
|
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) { |
|
|
|
|
} |
|
|
|
|