diff --git a/lib/line_editor.go b/lib/line_editor.go index c12f2f3..72534d1 100644 --- a/lib/line_editor.go +++ b/lib/line_editor.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, ""+self.prompt+"") 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) { +} diff --git a/mop b/mop deleted file mode 100755 index 144fd25..0000000 Binary files a/mop and /dev/null differ diff --git a/mop.go b/mop.go index 179c1e5..cd31484 100644 --- a/mop.go +++ b/mop.go @@ -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)