Avoid adding empty tickers

master
Michael Dvorkin 11 years ago
parent 540797e6ea
commit d52a23c49b
  1. 26
      line_editor.go

@ -11,18 +11,20 @@ import (
) )
type LineEditor struct { type LineEditor struct {
command rune command rune // Keyboard command such as '+' or '-'.
prompt string cursor int // Current cursor position within the input line.
cursor int prompt string // Prompt string for the command.
input string input string // User typed input string.
screen *Screen screen *Screen // Pointer to Screen.
quotes *Quotes quotes *Quotes // Pointer to Quotes.
regex *regexp.Regexp // Regex to split comma-delimited input string.
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) Initialize(screen *Screen, quotes *Quotes) *LineEditor { func (self *LineEditor) Initialize(screen *Screen, quotes *Quotes) *LineEditor {
self.screen = screen self.screen = screen
self.quotes = quotes self.quotes = quotes
self.regex = regexp.MustCompile(`[,\s]+`)
return self return self
} }
@ -168,9 +170,6 @@ func (self *LineEditor) execute() *LineEditor {
for i := before; i > after; i-- { for i := before; i > after; i-- {
self.screen.ClearLine(0, i + 4) self.screen.ClearLine(0, i + 4)
} }
if after == 0 { // Hide quotes header is the are no tickers left.
self.screen.ClearLine(0, 4)
}
} }
} }
} }
@ -186,8 +185,11 @@ func (self *LineEditor) done() bool {
return true return true
} }
//----------------------------------------------------------------------------- //
// Split by whitespace/comma to convert a string to array of tickers. Make sure
// the string is trimmed to avoid empty tickers in the array.
//
func (self *LineEditor) tokenize() []string { func (self *LineEditor) tokenize() []string {
input := strings.ToUpper(strings.TrimSpace(self.input)) input := strings.ToUpper(strings.Trim(self.input, `, `))
return regexp.MustCompile(`[,\s]+`).Split(input, -1) return self.regex.Split(input, -1)
} }

Loading…
Cancel
Save