Update documentation input

This commit is contained in:
erroneousboat 2016-09-13 21:13:51 +02:00
parent fee679b4d0
commit 7de84017bf

View File

@ -2,6 +2,7 @@ package components
import "github.com/gizak/termui" import "github.com/gizak/termui"
// Input is the definition of and input box
type Input struct { type Input struct {
Par *termui.Par Par *termui.Par
CursorPosition int CursorPosition int
@ -9,6 +10,7 @@ type Input struct {
CursorBgColor termui.Attribute CursorBgColor termui.Attribute
} }
// CreateInput is the constructor of the Input struct
func CreateInput() *Input { func CreateInput() *Input {
input := &Input{ input := &Input{
Par: termui.NewPar(""), Par: termui.NewPar(""),
@ -22,11 +24,11 @@ func CreateInput() *Input {
return input return input
} }
// implements interface termui.Bufferer // Buffer implements interface termui.Bufferer
func (i *Input) Buffer() termui.Buffer { func (i *Input) Buffer() termui.Buffer {
buf := i.Par.Buffer() buf := i.Par.Buffer()
// Set cursor // Set visible cursor
char := buf.At(i.Par.InnerX()+i.CursorPosition, i.Par.Block.InnerY()) char := buf.At(i.Par.InnerX()+i.CursorPosition, i.Par.Block.InnerY())
buf.Set( buf.Set(
i.Par.InnerX()+i.CursorPosition, i.Par.InnerX()+i.CursorPosition,
@ -37,32 +39,33 @@ func (i *Input) Buffer() termui.Buffer {
return buf return buf
} }
// implements interface termui.GridBufferer // GetHeight implements interface termui.GridBufferer
func (i *Input) GetHeight() int { func (i *Input) GetHeight() int {
return i.Par.Block.GetHeight() return i.Par.Block.GetHeight()
} }
// implements interface termui.GridBufferer // SetWidth implements interface termui.GridBufferer
func (i *Input) SetWidth(w int) { func (i *Input) SetWidth(w int) {
i.Par.SetWidth(w) i.Par.SetWidth(w)
} }
// implements interface termui.GridBufferer // SetX implements interface termui.GridBufferer
func (i *Input) SetX(x int) { func (i *Input) SetX(x int) {
i.Par.SetX(x) i.Par.SetX(x)
} }
// implements interface termui.GridBufferer // SetY implements interface termui.GridBufferer
func (i *Input) SetY(y int) { func (i *Input) SetY(y int) {
i.Par.SetY(y) i.Par.SetY(y)
} }
// Insert will insert a given key at the place of the current CursorPosition
func (i *Input) Insert(key string) { func (i *Input) Insert(key string) {
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:len(i.Par.Text)] i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:len(i.Par.Text)]
i.MoveCursorRight() i.MoveCursorRight()
} }
// Remove will remove a character at the place of the current CursorPosition
func (i *Input) Remove() { func (i *Input) Remove() {
if i.CursorPosition > 0 { if i.CursorPosition > 0 {
i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:len(i.Par.Text)] i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:len(i.Par.Text)]
@ -70,18 +73,21 @@ func (i *Input) Remove() {
} }
} }
// MoveCursorRight will increase the current CursorPosition with 1
func (i *Input) MoveCursorRight() { func (i *Input) MoveCursorRight() {
if i.CursorPosition < len(i.Par.Text) { if i.CursorPosition < len(i.Par.Text) {
i.CursorPosition++ i.CursorPosition++
} }
} }
// MoveCursorLeft will decrease the current CursorPosition with 1
func (i *Input) MoveCursorLeft() { func (i *Input) MoveCursorLeft() {
if i.CursorPosition > 0 { if i.CursorPosition > 0 {
i.CursorPosition-- i.CursorPosition--
} }
} }
// IsEmpty will return true when the input is empty
func (i *Input) IsEmpty() bool { func (i *Input) IsEmpty() bool {
if i.Par.Text == "" { if i.Par.Text == "" {
return true return true
@ -89,11 +95,13 @@ func (i *Input) IsEmpty() bool {
return false return false
} }
// Clear will empty the input and move the cursor to the start position
func (i *Input) Clear() { func (i *Input) Clear() {
i.Par.Text = "" i.Par.Text = ""
i.CursorPosition = 0 i.CursorPosition = 0
} }
// Text returns the text currently in the input
func (i *Input) Text() string { func (i *Input) Text() string {
return i.Par.Text return i.Par.Text
} }