You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mop/line_editor.go

194 lines
4.9 KiB

// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
11 years ago
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package mop
import (
12 years ago
`regexp`
`strings`
`github.com/michaeldv/termbox-go`
)
type LineEditor struct {
12 years ago
command rune
prompt string
cursor int
input string
12 years ago
screen *Screen
12 years ago
quotes *Quotes
}
//-----------------------------------------------------------------------------
12 years ago
func (self *LineEditor) Initialize(screen *Screen, quotes *Quotes) *LineEditor {
12 years ago
self.screen = screen
12 years ago
self.quotes = quotes
12 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) Prompt(command rune) *LineEditor {
prompts := map[rune]string{'+': `Add tickers: `, '-': `Remove tickers: `}
if prompt, ok := prompts[command]; ok {
self.prompt = prompt
self.command = command
11 years ago
self.screen.DrawLine(0, 3, `<white>` + self.prompt + `</>`)
termbox.SetCursor(len(self.prompt), 3)
termbox.Flush()
}
11 years ago
return self
}
//-----------------------------------------------------------------------------
func (self *LineEditor) Handle(ev termbox.Event) bool {
defer termbox.Flush()
switch ev.Key {
case termbox.KeyEsc:
11 years ago
return self.done()
case termbox.KeyEnter:
11 years ago
return self.execute().done()
case termbox.KeyBackspace, termbox.KeyBackspace2:
self.delete_previous_character()
case termbox.KeyCtrlB, termbox.KeyArrowLeft:
self.move_left()
case termbox.KeyCtrlF, termbox.KeyArrowRight:
self.move_right()
case termbox.KeyCtrlA:
self.jump_to_beginning()
case termbox.KeyCtrlE:
self.jump_to_end()
case termbox.KeySpace:
self.insert_character(' ')
default:
if ev.Ch != 0 {
self.insert_character(ev.Ch)
}
}
11 years ago
return false
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) delete_previous_character() *LineEditor {
if self.cursor > 0 {
if self.cursor < len(self.input) {
// Remove character in the middle of the input string.
self.input = self.input[0 : self.cursor-1] + self.input[self.cursor : len(self.input)]
} else {
// Remove last input character.
self.input = self.input[ : len(self.input)-1]
}
12 years ago
self.screen.DrawLine(len(self.prompt), 3, self.input + ` `) // Erase last character.
self.move_left()
}
11 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) insert_character(ch rune) *LineEditor {
if self.cursor < len(self.input) {
// Insert the character in the middle of the input string.
self.input = self.input[0 : self.cursor] + string(ch) + self.input[self.cursor : len(self.input)]
} else {
// Append the character to the end of the input string.
self.input += string(ch)
}
12 years ago
self.screen.DrawLine(len(self.prompt), 3, self.input)
self.move_right()
11 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) move_left() *LineEditor {
if self.cursor > 0 {
11 years ago
self.cursor--
termbox.SetCursor(len(self.prompt) + self.cursor, 3)
}
11 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) move_right() *LineEditor {
if self.cursor < len(self.input) {
11 years ago
self.cursor++
termbox.SetCursor(len(self.prompt) + self.cursor, 3)
}
11 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) jump_to_beginning() *LineEditor {
self.cursor = 0
termbox.SetCursor(len(self.prompt) + self.cursor, 3)
11 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) jump_to_end() *LineEditor {
self.cursor = len(self.input)
termbox.SetCursor(len(self.prompt) + self.cursor, 3)
12 years ago
11 years ago
return self
12 years ago
}
//-----------------------------------------------------------------------------
11 years ago
func (self *LineEditor) execute() *LineEditor {
12 years ago
switch self.command {
case '+':
tickers := self.tokenize()
12 years ago
if len(tickers) > 0 {
if added,_ := self.quotes.AddTickers(tickers); added > 0 {
self.screen.Draw(self.quotes)
}
12 years ago
}
case '-':
tickers := self.tokenize()
if len(tickers) > 0 {
12 years ago
before := len(self.quotes.profile.Tickers)
if removed,_ := self.quotes.RemoveTickers(tickers); removed > 0 {
self.screen.Draw(self.quotes)
after := before - removed
for i := before; i > after; i-- {
12 years ago
self.screen.ClearLine(0, i + 4)
}
if after == 0 { // Hide quotes header is the are no tickers left.
self.screen.ClearLine(0, 4)
}
}
12 years ago
}
}
11 years ago
return self
}
//-----------------------------------------------------------------------------
func (self *LineEditor) done() bool {
self.screen.ClearLine(0, 3)
termbox.HideCursor()
return true
12 years ago
}
//-----------------------------------------------------------------------------
func (self *LineEditor) tokenize() []string {
input := strings.ToUpper(strings.TrimSpace(self.input))
return regexp.MustCompile(`[,\s]+`).Split(input, -1)
12 years ago
}