Linted termbox_inputfield.go

This commit is contained in:
Brian Buller 2015-10-21 11:14:57 -05:00
parent e469e625ed
commit 8aa5f1a4c0
1 changed files with 42 additions and 20 deletions

View File

@ -1,4 +1,4 @@
package termbox_util package termboxUtil
import ( import (
"fmt" "fmt"
@ -6,6 +6,7 @@ import (
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
// InputField is a field for inputting text
type InputField struct { type InputField struct {
value string value string
x, y, width, height int x, y, width, height int
@ -14,47 +15,67 @@ type InputField struct {
bordered bool bordered bool
} }
// CreateInputField creates an input field at x, y that is w by h
func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField { func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField {
i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg} i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg}
return &i return &i
} }
// GetValue gets the current text that is in the InputField
func (i *InputField) GetValue() string { return i.value } func (i *InputField) GetValue() string { return i.value }
// SetValue sets the current text in the InputField to s
func (i *InputField) SetValue(s string) *InputField { func (i *InputField) SetValue(s string) *InputField {
i.value = s i.value = s
return i return i
} }
// GetX returns the x position of the input field
func (i *InputField) GetX() int { return i.x } func (i *InputField) GetX() int { return i.x }
// SetX sets the x position of the input field
func (i *InputField) SetX(x int) *InputField { func (i *InputField) SetX(x int) *InputField {
i.x = x i.x = x
return i return i
} }
// GetY returns the y position of the input field
func (i *InputField) GetY() int { return i.y } func (i *InputField) GetY() int { return i.y }
// SetY sets the y position of the input field
func (i *InputField) SetY(y int) *InputField { func (i *InputField) SetY(y int) *InputField {
i.y = y i.y = y
return i return i
} }
// GetWidth returns the current width of the input field
func (i *InputField) GetWidth() int { return i.width } func (i *InputField) GetWidth() int { return i.width }
// SetWidth sets the current width of the input field
func (i *InputField) SetWidth(w int) *InputField { func (i *InputField) SetWidth(w int) *InputField {
i.width = w i.width = w
return i return i
} }
// GetHeight returns the current height of the input field
func (i *InputField) GetHeight() int { return i.height } func (i *InputField) GetHeight() int { return i.height }
// SetHeight sets the current height of the input field
func (i *InputField) SetHeight(h int) *InputField { func (i *InputField) SetHeight(h int) *InputField {
i.height = h i.height = h
return i return i
} }
// IsBordered returns true or false if this input field has a border
func (i *InputField) IsBordered() bool { return i.bordered } func (i *InputField) IsBordered() bool { return i.bordered }
// SetBordered sets whether we render a border around the input field
func (i *InputField) SetBordered(b bool) *InputField { func (i *InputField) SetBordered(b bool) *InputField {
i.bordered = b i.bordered = b
return i return i
} }
// HandleKeyPress accepts the termbox event and returns whether it was consumed
func (i *InputField) HandleKeyPress(event termbox.Event) bool { func (i *InputField) HandleKeyPress(event termbox.Event) bool {
if event.Key == termbox.KeyEnter { if event.Key == termbox.KeyEnter {
// Done editing // Done editing
@ -64,11 +85,11 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
} }
} else if event.Key == termbox.KeyArrowLeft { } else if event.Key == termbox.KeyArrowLeft {
if i.cursor+len(i.value) > 0 { if i.cursor+len(i.value) > 0 {
i.cursor -= 1 i.cursor--
} }
} else if event.Key == termbox.KeyArrowRight { } else if event.Key == termbox.KeyArrowRight {
if i.cursor < 0 { if i.cursor < 0 {
i.cursor += 1 i.cursor++
} }
} else if event.Key == termbox.KeyCtrlU { } else if event.Key == termbox.KeyCtrlU {
// Ctrl+U Clears the Input // Ctrl+U Clears the Input
@ -91,39 +112,40 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
} else if i.cursor == 0 { } else if i.cursor == 0 {
i.value = fmt.Sprintf("%s%s", i.value, ch) i.value = fmt.Sprintf("%s%s", i.value, ch)
} else { } else {
str_pt_1 := i.value[:(len(i.value) + i.cursor)] strPt1 := i.value[:(len(i.value) + i.cursor)]
str_pt_2 := i.value[(len(i.value) + i.cursor):] strPt2 := i.value[(len(i.value) + i.cursor):]
i.value = fmt.Sprintf("%s%s%s", str_pt_1, ch, str_pt_2) i.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2)
} }
} }
return true return true
} }
// Draw outputs the input field on the screen
func (i *InputField) Draw() { func (i *InputField) Draw() {
if i.bordered { if i.bordered {
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
} }
var str_pt_1, str_pt_2 string var strPt1, strPt2 string
var cursor_rune rune var cursorRune rune
if len(i.value) > 0 { if len(i.value) > 0 {
if i.cursor+len(i.value) == 0 { if i.cursor+len(i.value) == 0 {
str_pt_1 = "" strPt1 = ""
str_pt_2 = i.value[1:] strPt2 = i.value[1:]
cursor_rune = rune(i.value[0]) cursorRune = rune(i.value[0])
} else if i.cursor == 0 { } else if i.cursor == 0 {
str_pt_1 = i.value strPt1 = i.value
str_pt_2 = "" strPt2 = ""
cursor_rune = ' ' cursorRune = ' '
} else { } else {
str_pt_1 = i.value[:(len(i.value) + i.cursor)] strPt1 = i.value[:(len(i.value) + i.cursor)]
str_pt_2 = i.value[(len(i.value)+i.cursor)+1:] strPt2 = i.value[(len(i.value)+i.cursor)+1:]
cursor_rune = rune(i.value[len(i.value)+i.cursor]) cursorRune = rune(i.value[len(i.value)+i.cursor])
} }
} else { } else {
str_pt_1, str_pt_2, cursor_rune = "", "", ' ' strPt1, strPt2, cursorRune = "", "", ' '
} }
x, y := DrawStringAtPoint(str_pt_1, i.x+1, i.y+1, i.fg, i.bg) x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
termbox.SetCell(x, y, cursor_rune, i.bg, i.fg) termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
DrawStringAtPoint(str_pt_2, x+1, y, i.fg, i.bg) DrawStringAtPoint(str_pt_2, x+1, y, i.fg, i.bg)
} }