From 49f8f185ea1948ff5545e48ea250faefb58cbdc8 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 7 Oct 2017 20:30:45 +0200 Subject: [PATCH] Continue with work on the chat component Using own fork of gocui where I can use my own termui.PollEvent() --- components/channels.go | 6 +++--- components/chat.go | 6 ++++-- components/debug.go | 2 +- handlers/event.go | 31 +++++++++++-------------------- main.go | 19 ++++++------------- vendor/vendor.json | 12 +++++++++--- views/chat.go | 24 ++++++++++++------------ 7 files changed, 46 insertions(+), 54 deletions(-) diff --git a/components/channels.go b/components/channels.go index a86d850..a479ce9 100644 --- a/components/channels.go +++ b/components/channels.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/jroimartin/gocui" + "github.com/erroneousboat/gocui" "github.com/erroneousboat/slack-term/service" ) @@ -123,7 +123,7 @@ func (c *Channels) GetSelectedChannel() string { } // MoveCursorUp will decrease the SelectedChannel by 1 -func (c *Channels) MoveCursorUp(g *gocui.Gui, v *gocui.View) error { +func (c *Channels) MoveCursorUp() error { if c.SelectedChannel > 0 { c.SetSelectedChannel(c.SelectedChannel - 1) c.ScrollUp() @@ -133,7 +133,7 @@ func (c *Channels) MoveCursorUp(g *gocui.Gui, v *gocui.View) error { } // MoveCursorDown will increase the SelectedChannel by 1 -func (c *Channels) MoveCursorDown(g *gocui.Gui, v *gocui.View) error { +func (c *Channels) MoveCursorDown() error { if c.SelectedChannel < len(c.Items)-1 { c.SetSelectedChannel(c.SelectedChannel + 1) c.ScrollDown() diff --git a/components/chat.go b/components/chat.go index 7ae5ab9..216d249 100644 --- a/components/chat.go +++ b/components/chat.go @@ -3,7 +3,7 @@ package components import ( "fmt" - "github.com/jroimartin/gocui" + "github.com/erroneousboat/gocui" ) type Chat struct { @@ -53,7 +53,8 @@ func (c *Chat) Refresh() { } } -// FIXME: maybe rename to LoadMessages? +// SetMessage will put the provided message into the the Items field +// of the Chat view func (c *Chat) SetMessages(messages []string) { for _, msg := range messages { c.Items = append(c.Items, msg) @@ -63,4 +64,5 @@ func (c *Chat) SetMessages(messages []string) { // ClearMessages clear the c.Items func (c *Chat) ClearMessages() { c.Items = []string{} + c.View.Clear() } diff --git a/components/debug.go b/components/debug.go index 73e9d9f..46d3972 100644 --- a/components/debug.go +++ b/components/debug.go @@ -3,7 +3,7 @@ package components import ( "fmt" - "github.com/jroimartin/gocui" + "github.com/erroneousboat/gocui" ) type Debug struct { diff --git a/handlers/event.go b/handlers/event.go index 67be838..0989d9a 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -1,12 +1,10 @@ package handlers import ( - "log" "os" "strconv" "time" - "github.com/jroimartin/gocui" termbox "github.com/nsf/termbox-go" "github.com/erroneousboat/slack-term/context" @@ -43,18 +41,7 @@ func RegisterEventHandlers(ctx *context.AppContext) { // incomingMessageHandler(ctx) } -func RegisterKeyBindings(ctx *context.AppContext) { - if err := ctx.View.GUI.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, actionQuit); err != nil { - log.Fatal(err) - } - if err := ctx.View.GUI.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, view.Channels.MoveCursorDown); err != nil { - log.Fatal(err) - } - if err := ctx.View.GUI.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, view.Channels.MoveCursorUp); err != nil { - log.Fatal(err) - } -} - +// TODO: add incomingMessageHandler to the select statement func eventHandler(ctx *context.AppContext) { go func() { for { @@ -62,10 +49,12 @@ func eventHandler(ctx *context.AppContext) { } }() - go func() { - for { - ev := <-ctx.EventQueue + for { + ctx.View.GUI.Flush() + select { + + case ev := <-ctx.EventQueue: switch ev.Type { case termbox.EventKey: actionKeyEvent(ctx, ev) @@ -73,7 +62,9 @@ func eventHandler(ctx *context.AppContext) { // actionResizeEvent(ctx, ev) } } - }() + + ctx.View.GUI.Flush() + } } // func incomingMessageHandler(ctx *context.AppContext) { @@ -259,7 +250,6 @@ func actionMoveCursorUpChannels(ctx *context.AppContext) { ctx.View.Channels.MoveCursorUp() - ctx.View.Debug.SetText("test") timer = time.NewTimer(time.Second / 4) <-timer.C @@ -275,7 +265,6 @@ func actionMoveCursorDownChannels(ctx *context.AppContext) { ctx.View.Channels.MoveCursorDown() - ctx.View.Debug.SetText("test") timer = time.NewTimer(time.Second / 4) <-timer.C @@ -316,6 +305,8 @@ func actionChangeChannel(ctx *context.AppContext) { // Set read mark // ctx.View.Channels.SetReadMark(ctx.Service) + ctx.View.Debug.SetText("hello, world") + // Refresh Chat component ctx.View.Chat.Refresh() } diff --git a/main.go b/main.go index a6964bb..febc579 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,11 @@ import ( "flag" "fmt" "log" + "net/http" + _ "net/http/pprof" "os/user" "path" - "github.com/jroimartin/gocui" - "github.com/erroneousboat/slack-term/context" "github.com/erroneousboat/slack-term/handlers" ) @@ -57,6 +57,10 @@ func init() { } func main() { + go func() { + log.Println(http.ListenAndServe("localhost:6060", nil)) + }() + // Create context appCTX, err := context.CreateAppContext(flgConfig) if err != nil { @@ -64,17 +68,6 @@ func main() { } defer appCTX.View.GUI.Close() - // Create the view - // view, err := views.CreateView(ctx) - // if err != nil { - // log.Fatal(err) - // } - // defer view.Close() - // Register handlers handlers.RegisterEventHandlers(appCTX) - - if err := appCTX.View.GUI.MainLoop(); err != nil && err != gocui.ErrQuit { - log.Fatal(err) - } } diff --git a/vendor/vendor.json b/vendor/vendor.json index 9b64b5c..94edd0e 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -2,6 +2,12 @@ "comment": "", "ignore": "test", "package": [ + { + "checksumSHA1": "68RoiZ+7Vw6FkRuV1ZLHJyFRZlU=", + "path": "github.com/erroneousboat/gocui", + "revision": "0b3eb1f51efeae086da64024a431b69524f67698", + "revisionTime": "2017-10-07T12:13:47Z" + }, { "checksumSHA1": "En911lu7f/u83ScLGxAaTM2SJVE=", "path": "github.com/gizak/termui", @@ -33,10 +39,10 @@ "revisionTime": "2017-07-25T12:17:30Z" }, { - "checksumSHA1": "lqSuR5kZpYqI18/Hfhh3t/SYzPA=", + "checksumSHA1": "GY8G0wn9l4zVTQW9POZCdH+iftM=", "path": "github.com/nsf/termbox-go", - "revision": "4ed959e0540971545eddb8c75514973d670cf739", - "revisionTime": "2017-07-10T10:34:07Z" + "revision": "d51f2f6d6ccb97dd83ed04ae2f79c34234851f39", + "revisionTime": "2017-09-30T11:41:06Z" }, { "checksumSHA1": "7EZyXN0EmZLgGxZxK01IJua4c8o=", diff --git a/views/chat.go b/views/chat.go index 38cf89d..0afe5be 100644 --- a/views/chat.go +++ b/views/chat.go @@ -3,8 +3,8 @@ package views import ( "log" + "github.com/erroneousboat/gocui" "github.com/gizak/termui" - "github.com/jroimartin/gocui" "github.com/erroneousboat/slack-term/components" "github.com/erroneousboat/slack-term/service" @@ -88,17 +88,17 @@ func (v *View) RefreshComponent(name string) { ) } -func initKeyBindings(view *View) { - if err := view.GUI.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { - log.Fatal(err) - } - if err := view.GUI.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, view.Channels.MoveCursorDown); err != nil { - log.Fatal(err) - } - if err := view.GUI.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, view.Channels.MoveCursorUp); err != nil { - log.Fatal(err) - } -} +// func initKeyBindings(view *View) { +// if err := view.GUI.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { +// log.Fatal(err) +// } +// if err := view.GUI.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, view.Channels.MoveCursorDown); err != nil { +// log.Fatal(err) +// } +// if err := view.GUI.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, view.Channels.MoveCursorUp); err != nil { +// log.Fatal(err) +// } +// } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit