Start with Input component

This commit is contained in:
erroneousboat 2017-12-01 21:02:52 +01:00
parent d45103932c
commit d624f7eac1
8 changed files with 168 additions and 126 deletions

View File

@ -23,7 +23,6 @@ const (
type Channels struct {
Component
View *gocui.View
Items []string
SelectedChannel int // index of which channel is selected from the Items
Offset int // from what offset are channels rendered, FIXME probably not necessary anymore

View File

@ -8,7 +8,6 @@ import (
type Chat struct {
Component
View *gocui.View
Items []string
}

View File

@ -1,9 +1,14 @@
package components
import "github.com/erroneousboat/gocui"
// TODO: documentation
// Component
type Component struct {
Name string
X int
Y int
Width int
Height int
View *gocui.View
}

View File

@ -14,7 +14,6 @@ import (
// ctx.View.Debug.SetText("debugging statement")
type Debug struct {
Component
View *gocui.View
Text string
}

View File

@ -1,137 +1,38 @@
package components
import (
"github.com/gizak/termui"
"github.com/erroneousboat/slack-term/service"
"github.com/erroneousboat/gocui"
)
// Input is the definition of an Input component
type Input struct {
Par *termui.Par
Text []rune
CursorPosition int
Component
// Text []rune
}
// CreateInput is the constructor of the Input struct
func CreateInput() *Input {
input := &Input{
Par: termui.NewPar(""),
Text: make([]rune, 0),
CursorPosition: 0,
}
func CreateInputComponent(x, y, w, h int) *Input {
input := &Input{}
input.Par.Height = 3
input.Name = "input"
input.Y = y
input.X = x
input.Width = w
input.Height = h
return input
}
// Buffer implements interface termui.Bufferer
func (i *Input) Buffer() termui.Buffer {
buf := i.Par.Buffer()
// Layout will setup the visible part of the Channels component and implements
// the gocui.Manager interface
func (i *Input) Layout(g *gocui.Gui) error {
if v, err := g.SetView(i.Name, i.X, i.Y, i.X+i.Width, i.Y+i.Height); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Set visible 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: i.Par.TextBgColor,
Bg: i.Par.TextFgColor,
},
)
v.Editable = true
return buf
}
i.View = v
// GetHeight implements interface termui.GridBufferer
func (i *Input) GetHeight() int {
return i.Par.Block.GetHeight()
}
// SetWidth implements interface termui.GridBufferer
func (i *Input) SetWidth(w int) {
i.Par.SetWidth(w)
}
// SetX implements interface termui.GridBufferer
func (i *Input) SetX(x int) {
i.Par.SetX(x)
}
// SetY implements interface termui.GridBufferer
func (i *Input) SetY(y int) {
i.Par.SetY(y)
}
// SendMessage send the input text through the SlackService
func (i *Input) SendMessage(svc *service.SlackService, channel string, message string) {
svc.SendMessage(channel, message)
}
// Insert will insert a given key at the place of the current CursorPosition
func (i *Input) Insert(key rune) {
if len(i.Text) < i.Par.InnerBounds().Dx()-1 {
left := make([]rune, len(i.Text[0:i.CursorPosition]))
copy(left, i.Text[0:i.CursorPosition])
left = append(left, key)
i.Text = append(left, i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
i.MoveCursorRight()
}
}
// Backspace will remove a character in front of the CursorPosition
func (i *Input) Backspace() {
if i.CursorPosition > 0 {
i.Text = append(i.Text[0:i.CursorPosition-1], i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
i.MoveCursorLeft()
}
}
// Delete will remove a character at the CursorPosition
func (i *Input) Delete() {
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)
}
}
// MoveCursorRight will increase the current CursorPosition with 1
func (i *Input) MoveCursorRight() {
if i.CursorPosition < len(i.Text) {
i.CursorPosition++
}
}
// MoveCursorLeft will decrease the current CursorPosition with 1
func (i *Input) MoveCursorLeft() {
if i.CursorPosition > 0 {
i.CursorPosition--
}
}
// IsEmpty will return true when the input is empty
func (i *Input) IsEmpty() bool {
if i.Par.Text == "" {
return true
}
return false
}
// Clear will empty the input and move the cursor to the start position
func (i *Input) Clear() {
i.Text = make([]rune, 0)
i.Par.Text = ""
i.CursorPosition = 0
}
// GetText returns the text currently in the input
func (i *Input) GetText() string {
return i.Par.Text
return nil
}

137
components/input_bkp.go Normal file
View File

@ -0,0 +1,137 @@
package components
import (
"github.com/gizak/termui"
"github.com/erroneousboat/slack-term/service"
)
// Input is the definition of an Input component
type InputBKP struct {
Par *termui.Par
Text []rune
CursorPosition int
}
// CreateInput is the constructor of the Input struct
func CreateInput() *InputBKP {
input := &InputBKP{
Par: termui.NewPar(""),
Text: make([]rune, 0),
CursorPosition: 0,
}
input.Par.Height = 3
return input
}
// Buffer implements interface termui.Bufferer
func (i *InputBKP) Buffer() termui.Buffer {
buf := i.Par.Buffer()
// Set visible 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: i.Par.TextBgColor,
Bg: i.Par.TextFgColor,
},
)
return buf
}
// GetHeight implements interface termui.GridBufferer
func (i *InputBKP) GetHeight() int {
return i.Par.Block.GetHeight()
}
// SetWidth implements interface termui.GridBufferer
func (i *InputBKP) SetWidth(w int) {
i.Par.SetWidth(w)
}
// SetX implements interface termui.GridBufferer
func (i *InputBKP) SetX(x int) {
i.Par.SetX(x)
}
// SetY implements interface termui.GridBufferer
func (i *InputBKP) SetY(y int) {
i.Par.SetY(y)
}
// SendMessage send the input text through the SlackService
func (i *InputBKP) SendMessage(svc *service.SlackService, channel string, message string) {
svc.SendMessage(channel, message)
}
// Insert will insert a given key at the place of the current CursorPosition
func (i *InputBKP) Insert(key rune) {
if len(i.Text) < i.Par.InnerBounds().Dx()-1 {
left := make([]rune, len(i.Text[0:i.CursorPosition]))
copy(left, i.Text[0:i.CursorPosition])
left = append(left, key)
i.Text = append(left, i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
i.MoveCursorRight()
}
}
// Backspace will remove a character in front of the CursorPosition
func (i *InputBKP) Backspace() {
if i.CursorPosition > 0 {
i.Text = append(i.Text[0:i.CursorPosition-1], i.Text[i.CursorPosition:]...)
i.Par.Text = string(i.Text)
i.MoveCursorLeft()
}
}
// Delete will remove a character at the CursorPosition
func (i *InputBKP) Delete() {
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)
}
}
// MoveCursorRight will increase the current CursorPosition with 1
func (i *InputBKP) MoveCursorRight() {
if i.CursorPosition < len(i.Text) {
i.CursorPosition++
}
}
// MoveCursorLeft will decrease the current CursorPosition with 1
func (i *InputBKP) MoveCursorLeft() {
if i.CursorPosition > 0 {
i.CursorPosition--
}
}
// IsEmpty will return true when the input is empty
func (i *InputBKP) IsEmpty() bool {
if i.Par.Text == "" {
return true
}
return false
}
// Clear will empty the input and move the cursor to the start position
func (i *InputBKP) Clear() {
i.Text = make([]rune, 0)
i.Par.Text = ""
i.CursorPosition = 0
}
// GetText returns the text currently in the input
func (i *InputBKP) GetText() string {
return i.Par.Text
}

View File

@ -398,7 +398,7 @@ func parseMessage(s *SlackService, msg string) string {
// NOTE: Commented out because rendering of the emoji's
// creates artifacts from the last view because of
// double width emoji's
// msg = parseEmoji(msg)
msg = parseEmoji(msg)
msg = parseMentions(s, msg)

View File

@ -20,7 +20,7 @@ type View struct {
}
type ViewBKP struct {
Input *components.Input
Input *components.InputBKP
Chat *components.ChatBKP
Channels *components.ChannelsBKP
Mode *components.Mode
@ -51,11 +51,13 @@ func CreateChatView(svc *service.SlackService) *View {
channels.SetChannels(slackChans)
channels.SetPresenceChannels(slackChans)
// TODO Input component
// Create Input component
input := components.CreateInputComponent(11, maxY-11, maxX-12, 5)
view.Input = input
// TODO Mode component
// Chat component
// Create Chat component
chat := components.CreateChatComponent(11, 0, maxX-12, maxY-1)
view.Chat = chat