From 74e9ac15b3028739ad15e4d9104267278bb05601 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 16 Mar 2019 14:42:27 +0100 Subject: [PATCH] Start with different threads setup --- components/channels.go | 11 +++++------ components/threads.go | 26 ++++++++++++++++++++++++++ context/context.go | 8 ++++++-- handlers/event.go | 8 ++++++-- service/slack.go | 20 +++++++++++++++++--- views/view.go | 14 ++++++++++++-- 6 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 components/threads.go diff --git a/components/channels.go b/components/channels.go index 3dbbce2..29f6a97 100644 --- a/components/channels.go +++ b/components/channels.go @@ -108,13 +108,13 @@ type Channels struct { } // CreateChannels is the constructor for the Channels component -func CreateChannelsComponent(inputHeight int) *Channels { +func CreateChannelsComponent(height int) *Channels { channels := &Channels{ List: termui.NewList(), } channels.List.BorderLabel = "Channels" - channels.List.Height = termui.TermHeight() - inputHeight + channels.List.Height = height channels.SelectedChannel = 0 channels.Offset = 0 @@ -148,11 +148,10 @@ func (c *Channels) Buffer() termui.Buffer { // Append ellipsis when overflows cells = termui.DTrimTxCls(cells, c.List.InnerWidth()) - x := 0 + x := c.List.InnerBounds().Min.X for _, cell := range cells { - width := cell.Width() - buf.Set(c.List.InnerBounds().Min.X+x, y, cell) - x += width + buf.Set(x, y, cell) + x += cell.Width() } // When not at the end of the pane fill it up empty characters diff --git a/components/threads.go b/components/threads.go new file mode 100644 index 0000000..86b2822 --- /dev/null +++ b/components/threads.go @@ -0,0 +1,26 @@ +package components + +import ( + "github.com/erroneousboat/termui" +) + +type Threads struct { + *Channels +} + +func CreateThreadsComponent(height int) *Threads { + threads := &Threads{ + Channels: &Channels{ + List: termui.NewList(), + }, + } + + threads.List.BorderLabel = "Threads" + threads.List.Height = height + + threads.SelectedChannel = 0 + threads.Offset = 0 + threads.CursorPosition = threads.List.InnerBounds().Min.Y + + return threads +} diff --git a/context/context.go b/context/context.go index 91447a6..29408fd 100644 --- a/context/context.go +++ b/context/context.go @@ -87,11 +87,13 @@ func CreateAppContext(flgConfig string, flgToken string, flgDebug bool, version // Setup the interface if flgDebug { + // FIXME: threads width configurable termui.Body.AddRows( termui.NewRow( termui.NewCol(config.SidebarWidth, 0, view.Channels), termui.NewCol(config.MainWidth-5, 0, view.Chat), - termui.NewCol(config.MainWidth-6, 0, view.Debug), + termui.NewCol(config.MainWidth-10, 0, view.Threads), + termui.NewCol(config.MainWidth-7, 0, view.Debug), ), termui.NewRow( termui.NewCol(config.SidebarWidth, 0, view.Mode), @@ -99,10 +101,12 @@ func CreateAppContext(flgConfig string, flgToken string, flgDebug bool, version ), ) } else { + // FIXME: threads width configurable termui.Body.AddRows( termui.NewRow( termui.NewCol(config.SidebarWidth, 0, view.Channels), - termui.NewCol(config.MainWidth, 0, view.Chat), + termui.NewCol(config.MainWidth-1, 0, view.Chat), + termui.NewCol(config.MainWidth-10, 0, view.Threads), ), termui.NewRow( termui.NewCol(config.SidebarWidth, 0, view.Mode), diff --git a/handlers/event.go b/handlers/event.go index e2f0f56..d3c652d 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -332,7 +332,7 @@ func actionSearchMode(ctx *context.AppContext) { } func actionGetMessages(ctx *context.AppContext) { - msgs, err := ctx.Service.GetMessages( + msgs, _, err := ctx.Service.GetMessages( ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID, ctx.View.Chat.GetMaxItems(), ) @@ -418,7 +418,7 @@ func actionChangeChannel(ctx *context.AppContext) { // Get messages of the SelectedChannel, and get the count of messages // that fit into the Chat component - msgs, err := ctx.Service.GetMessages( + msgs, threads, err := ctx.Service.GetMessages( ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID, ctx.View.Chat.GetMaxItems(), ) @@ -436,6 +436,9 @@ func actionChangeChannel(ctx *context.AppContext) { ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].GetChannelName(), ) + // Set threads + ctx.View.Threads.SetChannels(threads) + // Clear notification icon if there is any channelItem := ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel] if channelItem.Notification { @@ -444,6 +447,7 @@ func actionChangeChannel(ctx *context.AppContext) { } termui.Render(ctx.View.Channels) + termui.Render(ctx.View.Threads) termui.Render(ctx.View.Chat) } diff --git a/service/slack.go b/service/slack.go index fcdfe21..3e24a8d 100644 --- a/service/slack.go +++ b/service/slack.go @@ -407,7 +407,7 @@ func (s *SlackService) SendCommand(channelID string, message string) (bool, erro // GetMessages will get messages for a channel, group or im channel delimited // by a count. -func (s *SlackService) GetMessages(channelID string, count int) ([]components.Message, error) { +func (s *SlackService) GetMessages(channelID string, count int) ([]components.Message, []components.ChannelItem, error) { // https://godoc.org/github.com/nlopes/slack#GetConversationHistoryParameters historyParams := slack.GetConversationHistoryParameters{ @@ -418,14 +418,28 @@ func (s *SlackService) GetMessages(channelID string, count int) ([]components.Me history, err := s.Client.GetConversationHistory(&historyParams) if err != nil { - return nil, err + return nil, nil, err } // Construct the messages var messages []components.Message + var threads []components.ChannelItem for _, message := range history.Messages { msg := s.CreateMessage(message, channelID) messages = append(messages, msg) + + // FIXME: create boolean isThread + if msg.Thread != "" { + + threads = append(threads, components.ChannelItem{ + ID: msg.ID, + Name: msg.Thread, + Type: components.ChannelTypeGroup, + StylePrefix: s.Config.Theme.Channel.Prefix, + StyleIcon: s.Config.Theme.Channel.Icon, + StyleText: s.Config.Theme.Channel.Text, + }) + } } // Reverse the order of the messages, we want the newest in @@ -435,7 +449,7 @@ func (s *SlackService) GetMessages(channelID string, count int) ([]components.Me messagesReversed = append(messagesReversed, messages[i]) } - return messagesReversed, nil + return messagesReversed, threads, nil } // CreateMessage will create a string formatted message that can be rendered diff --git a/views/view.go b/views/view.go index 44e0c4b..1c1b25d 100644 --- a/views/view.go +++ b/views/view.go @@ -13,6 +13,7 @@ type View struct { Input *components.Input Chat *components.Chat Channels *components.Channels + Threads *components.Threads Mode *components.Mode Debug *components.Debug } @@ -22,7 +23,8 @@ func CreateView(config *config.Config, svc *service.SlackService) (*View, error) input := components.CreateInputComponent() // Channels: create the component - channels := components.CreateChannelsComponent(input.Par.Height) + sideBarHeight := termui.TermHeight() - input.Par.Height + channels := components.CreateChannelsComponent(sideBarHeight) // Channels: fill the component slackChans, err := svc.GetChannels() @@ -33,11 +35,14 @@ func CreateView(config *config.Config, svc *service.SlackService) (*View, error) // Channels: set channels in component channels.SetChannels(slackChans) + // Threads: create component + threads := components.CreateThreadsComponent(sideBarHeight) + // Chat: create the component chat := components.CreateChatComponent(input.Par.Height) // Chat: fill the component - msgs, err := svc.GetMessages( + msgs, thds, err := svc.GetMessages( channels.ChannelItems[channels.SelectedChannel].ID, chat.GetMaxItems(), ) @@ -52,6 +57,9 @@ func CreateView(config *config.Config, svc *service.SlackService) (*View, error) channels.ChannelItems[channels.SelectedChannel].GetChannelName(), ) + // Threads: set threads in component + threads.SetChannels(thds) + // Debug: create the component debug := components.CreateDebugComponent(input.Par.Height) @@ -62,6 +70,7 @@ func CreateView(config *config.Config, svc *service.SlackService) (*View, error) Config: config, Input: input, Channels: channels, + Threads: threads, Chat: chat, Mode: mode, Debug: debug, @@ -75,6 +84,7 @@ func (v *View) Refresh() { v.Input, v.Chat, v.Channels, + v.Threads, v.Mode, ) }