Start with different threads setup

This commit is contained in:
erroneousboat 2019-03-16 14:42:27 +01:00
parent cfcf6469d6
commit 74e9ac15b3
6 changed files with 72 additions and 15 deletions

View File

@ -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

26
components/threads.go Normal file
View File

@ -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
}

View File

@ -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),

View File

@ -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)
}

View File

@ -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

View File

@ -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,
)
}