From d624f7eac143d81057f4272e26c7fc3c25c68be8 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Fri, 1 Dec 2017 21:02:52 +0100 Subject: [PATCH] Start with Input component --- components/channels.go | 1 - components/chat.go | 1 - components/components.go | 5 ++ components/debug.go | 1 - components/input.go | 139 ++++++--------------------------------- components/input_bkp.go | 137 ++++++++++++++++++++++++++++++++++++++ service/slack.go | 2 +- views/chat.go | 8 ++- 8 files changed, 168 insertions(+), 126 deletions(-) create mode 100644 components/input_bkp.go diff --git a/components/channels.go b/components/channels.go index 1bf1458..2221d67 100644 --- a/components/channels.go +++ b/components/channels.go @@ -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 diff --git a/components/chat.go b/components/chat.go index 50c4743..784d5b4 100644 --- a/components/chat.go +++ b/components/chat.go @@ -8,7 +8,6 @@ import ( type Chat struct { Component - View *gocui.View Items []string } diff --git a/components/components.go b/components/components.go index e00b7ef..cdb1b4f 100644 --- a/components/components.go +++ b/components/components.go @@ -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 } diff --git a/components/debug.go b/components/debug.go index e53f479..b99e149 100644 --- a/components/debug.go +++ b/components/debug.go @@ -14,7 +14,6 @@ import ( // ctx.View.Debug.SetText("debugging statement") type Debug struct { Component - View *gocui.View Text string } diff --git a/components/input.go b/components/input.go index 38bb38d..3fd2b44 100644 --- a/components/input.go +++ b/components/input.go @@ -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 } diff --git a/components/input_bkp.go b/components/input_bkp.go new file mode 100644 index 0000000..b7bf151 --- /dev/null +++ b/components/input_bkp.go @@ -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 +} diff --git a/service/slack.go b/service/slack.go index 08c179a..19c5baa 100644 --- a/service/slack.go +++ b/service/slack.go @@ -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) diff --git a/views/chat.go b/views/chat.go index adb6136..0766f64 100644 --- a/views/chat.go +++ b/views/chat.go @@ -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