135 lines
3.0 KiB
Go
Raw Normal View History

2016-09-11 17:55:19 +02:00
package components
2016-09-27 22:05:44 +02:00
import (
"github.com/gizak/termui"
2016-10-01 14:07:42 +02:00
2016-10-19 09:08:31 +02:00
"github.com/erroneousboat/slack-term/service"
2016-09-27 22:05:44 +02:00
)
2016-09-11 17:55:19 +02:00
2016-10-02 14:53:00 +02:00
// Input is the definition of an Input component
2016-09-11 17:55:19 +02:00
type Input struct {
2016-09-13 21:02:02 +02:00
Par *termui.Par
2016-10-21 21:23:25 +02:00
Text []rune
2016-09-11 17:55:19 +02:00
CursorPosition int
}
2016-09-13 21:13:51 +02:00
// CreateInput is the constructor of the Input struct
2016-09-11 17:55:19 +02:00
func CreateInput() *Input {
input := &Input{
2016-09-13 21:02:02 +02:00
Par: termui.NewPar(""),
2016-10-21 21:23:25 +02:00
Text: make([]rune, 0),
2016-09-11 17:55:19 +02:00
CursorPosition: 0,
}
2016-09-13 21:02:02 +02:00
input.Par.Height = 3
2016-09-11 17:55:19 +02:00
return input
}
2016-09-13 21:13:51 +02:00
// Buffer implements interface termui.Bufferer
2016-09-11 17:55:19 +02:00
func (i *Input) Buffer() termui.Buffer {
2016-09-13 21:02:02 +02:00
buf := i.Par.Buffer()
2016-09-13 21:13:51 +02:00
// Set visible cursor
2016-09-13 21:02:02 +02:00
char := buf.At(i.Par.InnerX()+i.CursorPosition, i.Par.Block.InnerY())
buf.Set(
i.Par.InnerX()+i.CursorPosition,
i.Par.Block.InnerY(),
2016-10-11 19:28:37 +02:00
termui.Cell{
Ch: char.Ch,
Fg: i.Par.TextBgColor,
Bg: i.Par.TextFgColor,
},
2016-09-13 21:02:02 +02:00
)
return buf
2016-09-11 17:55:19 +02:00
}
2016-09-13 21:13:51 +02:00
// GetHeight implements interface termui.GridBufferer
2016-09-11 17:55:19 +02:00
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
}
2016-09-13 21:13:51 +02:00
// SetWidth implements interface termui.GridBufferer
2016-09-11 17:55:19 +02:00
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
}
2016-09-13 21:13:51 +02:00
// SetX implements interface termui.GridBufferer
2016-09-11 17:55:19 +02:00
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
}
2016-09-13 21:13:51 +02:00
// SetY implements interface termui.GridBufferer
2016-09-11 17:55:19 +02:00
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
}
2016-10-02 16:07:35 +02:00
// SendMessage send the input text through the SlackService
2016-09-29 19:40:09 +02:00
func (i *Input) SendMessage(svc *service.SlackService, channel string, message string) {
svc.SendMessage(channel, message)
2016-09-27 22:05:44 +02:00
}
2016-09-13 21:13:51 +02:00
// Insert will insert a given key at the place of the current CursorPosition
2016-10-21 21:23:25 +02:00
func (i *Input) Insert(key rune) {
if len(i.Text) < i.Par.InnerBounds().Dx()-1 {
first := append(i.Text[0:i.CursorPosition], key)
i.Text = append(first, i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
2016-09-24 20:18:09 +02:00
i.MoveCursorRight()
}
2016-09-11 17:55:19 +02:00
}
2016-10-09 14:59:48 +02:00
// Backspace will remove a character in front of the CursorPosition
func (i *Input) Backspace() {
2016-09-11 17:55:19 +02:00
if i.CursorPosition > 0 {
2016-10-21 21:23:25 +02:00
i.Text = append(i.Text[0:i.CursorPosition-1], i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
2016-09-12 22:08:44 +02:00
i.MoveCursorLeft()
2016-09-11 17:55:19 +02:00
}
}
2016-10-09 14:59:48 +02:00
// Delete will remove a character at the CursorPosition
func (i *Input) Delete() {
2016-10-21 21:23:25 +02:00
if i.CursorPosition < len(i.Text) {
i.Text = append(i.Text[0:i.CursorPosition], i.Text[i.CursorPosition+1:]...)
i.Par.Text = string(i.Text)
2016-10-09 14:59:48 +02:00
}
}
2016-09-13 21:13:51 +02:00
// MoveCursorRight will increase the current CursorPosition with 1
2016-09-11 17:55:19 +02:00
func (i *Input) MoveCursorRight() {
2016-10-22 09:41:35 +02:00
if i.CursorPosition < len(i.Text) {
2016-09-11 17:55:19 +02:00
i.CursorPosition++
}
}
2016-09-13 21:13:51 +02:00
// MoveCursorLeft will decrease the current CursorPosition with 1
2016-09-11 17:55:19 +02:00
func (i *Input) MoveCursorLeft() {
if i.CursorPosition > 0 {
i.CursorPosition--
}
}
2016-09-13 21:13:51 +02:00
// IsEmpty will return true when the input is empty
2016-09-11 17:55:19 +02:00
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
}
2016-09-13 21:13:51 +02:00
// Clear will empty the input and move the cursor to the start position
2016-09-11 17:55:19 +02:00
func (i *Input) Clear() {
2016-10-22 10:51:21 +02:00
i.Text = make([]rune, 0)
2016-09-13 21:02:02 +02:00
i.Par.Text = ""
2016-09-11 17:55:19 +02:00
i.CursorPosition = 0
}
2016-10-21 21:23:25 +02:00
// GetText returns the text currently in the input
func (i *Input) GetText() string {
2016-09-13 21:02:02 +02:00
return i.Par.Text
2016-09-11 17:55:19 +02:00
}