100 lines
1.9 KiB
Go
Raw Normal View History

2016-09-11 17:55:19 +02:00
package components
import "github.com/gizak/termui"
type Input struct {
2016-09-13 21:02:02 +02:00
Par *termui.Par
2016-09-11 17:55:19 +02:00
CursorPosition int
CursorFgColor termui.Attribute
CursorBgColor termui.Attribute
}
func CreateInput() *Input {
input := &Input{
2016-09-13 21:02:02 +02:00
Par: termui.NewPar(""),
2016-09-11 17:55:19 +02:00
CursorPosition: 0,
CursorBgColor: termui.ColorBlack,
CursorFgColor: termui.ColorWhite,
}
2016-09-13 21:02:02 +02:00
input.Par.Height = 3
2016-09-11 17:55:19 +02:00
return input
}
// implements interface termui.Bufferer
func (i *Input) Buffer() termui.Buffer {
2016-09-13 21:02:02 +02:00
buf := i.Par.Buffer()
// Set cursor
char := buf.At(i.Par.InnerX()+i.CursorPosition, i.Par.Block.InnerY())
buf.Set(
i.Par.InnerX()+i.CursorPosition,
i.Par.Block.InnerY(),
termui.Cell{Ch: char.Ch, Fg: termui.ColorBlack, Bg: termui.ColorWhite},
)
return buf
2016-09-11 17:55:19 +02:00
}
// implements interface termui.GridBufferer
func (i *Input) GetHeight() int {
2016-09-13 21:02:02 +02:00
return i.Par.Block.GetHeight()
2016-09-11 17:55:19 +02:00
}
// implements interface termui.GridBufferer
func (i *Input) SetWidth(w int) {
2016-09-13 21:02:02 +02:00
i.Par.SetWidth(w)
2016-09-11 17:55:19 +02:00
}
// implements interface termui.GridBufferer
func (i *Input) SetX(x int) {
2016-09-13 21:02:02 +02:00
i.Par.SetX(x)
2016-09-11 17:55:19 +02:00
}
// implements interface termui.GridBufferer
func (i *Input) SetY(y int) {
2016-09-13 21:02:02 +02:00
i.Par.SetY(y)
2016-09-11 17:55:19 +02:00
}
func (i *Input) Insert(key string) {
2016-09-13 21:02:02 +02:00
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:len(i.Par.Text)]
2016-09-12 22:08:44 +02:00
i.MoveCursorRight()
2016-09-11 17:55:19 +02:00
}
func (i *Input) Remove() {
if i.CursorPosition > 0 {
2016-09-13 21:02:02 +02:00
i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:len(i.Par.Text)]
2016-09-12 22:08:44 +02:00
i.MoveCursorLeft()
2016-09-11 17:55:19 +02:00
}
}
func (i *Input) MoveCursorRight() {
2016-09-13 21:02:02 +02:00
if i.CursorPosition < len(i.Par.Text) {
2016-09-11 17:55:19 +02:00
i.CursorPosition++
}
}
func (i *Input) MoveCursorLeft() {
if i.CursorPosition > 0 {
i.CursorPosition--
}
}
func (i *Input) IsEmpty() bool {
2016-09-13 21:02:02 +02:00
if i.Par.Text == "" {
2016-09-11 17:55:19 +02:00
return true
}
return false
}
func (i *Input) Clear() {
2016-09-13 21:02:02 +02:00
i.Par.Text = ""
2016-09-11 17:55:19 +02:00
i.CursorPosition = 0
}
func (i *Input) Text() string {
2016-09-13 21:02:02 +02:00
return i.Par.Text
2016-09-11 17:55:19 +02:00
}