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
|
|
|
|
|
|
|
"github.com/erroneousboat/slack-term/src/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-09-11 17:55:19 +02:00
|
|
|
CursorPosition int
|
|
|
|
CursorFgColor termui.Attribute
|
|
|
|
CursorBgColor termui.Attribute
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
termui.Cell{Ch: char.Ch, Fg: termui.ColorBlack, Bg: termui.ColorWhite},
|
|
|
|
)
|
|
|
|
|
|
|
|
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-09-11 17:55:19 +02:00
|
|
|
func (i *Input) Insert(key string) {
|
2016-09-24 20:18:09 +02:00
|
|
|
if len(i.Par.Text) < i.Par.InnerBounds().Dx()-1 {
|
2016-10-09 14:59:48 +02:00
|
|
|
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:]
|
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-09 14:59:48 +02:00
|
|
|
i.Par.Text = i.Par.Text[0:i.CursorPosition-1] + i.Par.Text[i.CursorPosition:]
|
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() {
|
|
|
|
if i.CursorPosition < len(i.Par.Text) {
|
|
|
|
i.Par.Text = i.Par.Text[0:i.CursorPosition] + i.Par.Text[i.CursorPosition+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-13 21:02:02 +02:00
|
|
|
if i.CursorPosition < len(i.Par.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-09-13 21:02:02 +02:00
|
|
|
i.Par.Text = ""
|
2016-09-11 17:55:19 +02:00
|
|
|
i.CursorPosition = 0
|
|
|
|
}
|
|
|
|
|
2016-09-13 21:13:51 +02:00
|
|
|
// Text returns the text currently in the input
|
2016-09-11 17:55:19 +02:00
|
|
|
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
|
|
|
}
|