More golint refactoring

master
Michael Dvorkin 11 years ago
parent 8bab5128c9
commit 40c135a186
  1. 146
      line_editor.go

@ -1,13 +1,13 @@
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved. // Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style license that can
// license that can be found in the LICENSE file. // be found in the LICENSE file.
package mop package mop
import ( import (
`github.com/michaeldv/termbox-go`
`regexp` `regexp`
`strings` `strings`
`github.com/michaeldv/termbox-go`
) )
type LineEditor struct { type LineEditor struct {
@ -21,61 +21,61 @@ type LineEditor struct {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) Initialize(screen *Screen, quotes *Quotes) *LineEditor { func (editor *LineEditor) Initialize(screen *Screen, quotes *Quotes) *LineEditor {
self.screen = screen editor.screen = screen
self.quotes = quotes editor.quotes = quotes
self.regex = regexp.MustCompile(`[,\s]+`) editor.regex = regexp.MustCompile(`[,\s]+`)
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) Prompt(command rune) *LineEditor { func (editor *LineEditor) Prompt(command rune) *LineEditor {
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 editor.prompt = prompt
self.command = command editor.command = command
self.screen.DrawLine(0, 3, `<white>` + self.prompt + `</>`) editor.screen.DrawLine(0, 3, `<white>` + editor.prompt + `</>`)
termbox.SetCursor(len(self.prompt), 3) termbox.SetCursor(len(editor.prompt), 3)
termbox.Flush() termbox.Flush()
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) Handle(ev termbox.Event) bool { func (editor *LineEditor) Handle(ev termbox.Event) bool {
defer termbox.Flush() defer termbox.Flush()
switch ev.Key { switch ev.Key {
case termbox.KeyEsc: case termbox.KeyEsc:
return self.done() return editor.done()
case termbox.KeyEnter: case termbox.KeyEnter:
return self.execute().done() return editor.execute().done()
case termbox.KeyBackspace, termbox.KeyBackspace2: case termbox.KeyBackspace, termbox.KeyBackspace2:
self.delete_previous_character() editor.deletePreviousCharacter()
case termbox.KeyCtrlB, termbox.KeyArrowLeft: case termbox.KeyCtrlB, termbox.KeyArrowLeft:
self.move_left() editor.moveLeft()
case termbox.KeyCtrlF, termbox.KeyArrowRight: case termbox.KeyCtrlF, termbox.KeyArrowRight:
self.move_right() editor.moveRight()
case termbox.KeyCtrlA: case termbox.KeyCtrlA:
self.jump_to_beginning() editor.jumpToBeginning()
case termbox.KeyCtrlE: case termbox.KeyCtrlE:
self.jump_to_end() editor.jumpToEnd()
case termbox.KeySpace: case termbox.KeySpace:
self.insert_character(' ') editor.insertCharacter(' ')
default: default:
if ev.Ch != 0 { if ev.Ch != 0 {
self.insert_character(ev.Ch) editor.insertCharacter(ev.Ch)
} }
} }
@ -83,103 +83,103 @@ func (self *LineEditor) Handle(ev termbox.Event) bool {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) delete_previous_character() *LineEditor { func (editor *LineEditor) deletePreviousCharacter() *LineEditor {
if self.cursor > 0 { if editor.cursor > 0 {
if self.cursor < len(self.input) { if editor.cursor < len(editor.input) {
// Remove character in the middle of the input string. // Remove character in the middle of the input string.
self.input = self.input[0 : self.cursor-1] + self.input[self.cursor : len(self.input)] editor.input = editor.input[0 : editor.cursor-1] + editor.input[editor.cursor : len(editor.input)]
} else { } else {
// Remove last input character. // Remove last input character.
self.input = self.input[ : len(self.input)-1] editor.input = editor.input[ : len(editor.input)-1]
} }
self.screen.DrawLine(len(self.prompt), 3, self.input + ` `) // Erase last character. editor.screen.DrawLine(len(editor.prompt), 3, editor.input + ` `) // Erase last character.
self.move_left() editor.moveLeft()
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) insert_character(ch rune) *LineEditor { func (editor *LineEditor) insertCharacter(ch rune) *LineEditor {
if self.cursor < len(self.input) { if editor.cursor < len(editor.input) {
// Insert the character in the middle of the input string. // 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)] editor.input = editor.input[0 : editor.cursor] + string(ch) + editor.input[editor.cursor : len(editor.input)]
} else { } else {
// Append the character to the end of the input string. // Append the character to the end of the input string.
self.input += string(ch) editor.input += string(ch)
} }
self.screen.DrawLine(len(self.prompt), 3, self.input) editor.screen.DrawLine(len(editor.prompt), 3, editor.input)
self.move_right() editor.moveRight()
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) move_left() *LineEditor { func (editor *LineEditor) moveLeft() *LineEditor {
if self.cursor > 0 { if editor.cursor > 0 {
self.cursor-- editor.cursor--
termbox.SetCursor(len(self.prompt) + self.cursor, 3) termbox.SetCursor(len(editor.prompt) + editor.cursor, 3)
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) move_right() *LineEditor { func (editor *LineEditor) moveRight() *LineEditor {
if self.cursor < len(self.input) { if editor.cursor < len(editor.input) {
self.cursor++ editor.cursor++
termbox.SetCursor(len(self.prompt) + self.cursor, 3) termbox.SetCursor(len(editor.prompt) + editor.cursor, 3)
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) jump_to_beginning() *LineEditor { func (editor *LineEditor) jumpToBeginning() *LineEditor {
self.cursor = 0 editor.cursor = 0
termbox.SetCursor(len(self.prompt) + self.cursor, 3) termbox.SetCursor(len(editor.prompt) + editor.cursor, 3)
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) jump_to_end() *LineEditor { func (editor *LineEditor) jumpToEnd() *LineEditor {
self.cursor = len(self.input) editor.cursor = len(editor.input)
termbox.SetCursor(len(self.prompt) + self.cursor, 3) termbox.SetCursor(len(editor.prompt) + editor.cursor, 3)
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) execute() *LineEditor { func (editor *LineEditor) execute() *LineEditor {
switch self.command { switch editor.command {
case '+': case '+':
tickers := self.tokenize() tickers := editor.tokenize()
if len(tickers) > 0 { if len(tickers) > 0 {
if added,_ := self.quotes.AddTickers(tickers); added > 0 { if added,_ := editor.quotes.AddTickers(tickers); added > 0 {
self.screen.Draw(self.quotes) editor.screen.Draw(editor.quotes)
} }
} }
case '-': case '-':
tickers := self.tokenize() tickers := editor.tokenize()
if len(tickers) > 0 { if len(tickers) > 0 {
before := len(self.quotes.profile.Tickers) before := len(editor.quotes.profile.Tickers)
if removed,_ := self.quotes.RemoveTickers(tickers); removed > 0 { if removed,_ := editor.quotes.RemoveTickers(tickers); removed > 0 {
self.screen.Draw(self.quotes) editor.screen.Draw(editor.quotes)
after := before - removed after := before - removed
for i := before; i > after; i-- { for i := before; i > after; i-- {
self.screen.ClearLine(0, i + 4) editor.screen.ClearLine(0, i + 4)
} }
} }
} }
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) done() bool { func (editor *LineEditor) done() bool {
self.screen.ClearLine(0, 3) editor.screen.ClearLine(0, 3)
termbox.HideCursor() termbox.HideCursor()
return true return true
@ -189,7 +189,7 @@ func (self *LineEditor) done() bool {
// Split by whitespace/comma to convert a string to array of tickers. Make sure // 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. // the string is trimmed to avoid empty tickers in the array.
// //
func (self *LineEditor) tokenize() []string { func (editor *LineEditor) tokenize() []string {
input := strings.ToUpper(strings.Trim(self.input, `, `)) input := strings.ToUpper(strings.Trim(editor.input, `, `))
return self.regex.Split(input, -1) return editor.regex.Split(input, -1)
} }

Loading…
Cancel
Save