2016-09-25 22:34:02 +02:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/erroneousboat/slack-term/src/service"
|
|
|
|
"github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Channels struct {
|
2016-09-27 22:05:44 +02:00
|
|
|
List *termui.List
|
|
|
|
SlackChannels []SlackChannel
|
|
|
|
SelectedChannel int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SlackChannel struct {
|
|
|
|
Name string
|
|
|
|
ID string
|
2016-09-25 22:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateChannels(svc *service.SlackService, inputHeight int) *Channels {
|
|
|
|
channels := &Channels{
|
|
|
|
List: termui.NewList(),
|
|
|
|
}
|
|
|
|
|
|
|
|
channels.List.BorderLabel = "Channels"
|
|
|
|
channels.List.Height = termui.TermHeight() - inputHeight
|
|
|
|
|
2016-09-28 22:10:04 +02:00
|
|
|
channels.SelectedChannel = 0
|
2016-09-27 22:05:44 +02:00
|
|
|
|
2016-09-25 22:34:02 +02:00
|
|
|
channels.GetChannels(svc)
|
|
|
|
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer implements interface termui.Bufferer
|
|
|
|
func (c *Channels) Buffer() termui.Buffer {
|
|
|
|
buf := c.List.Buffer()
|
2016-09-27 22:05:44 +02:00
|
|
|
|
|
|
|
for y, item := range c.List.Items {
|
|
|
|
var cells []termui.Cell
|
|
|
|
if y == c.SelectedChannel {
|
|
|
|
cells = termui.DefaultTxBuilder.Build(
|
|
|
|
item, termui.ColorBlack, termui.ColorWhite)
|
|
|
|
} else {
|
|
|
|
cells = termui.DefaultTxBuilder.Build(
|
|
|
|
item, c.List.ItemFgColor, c.List.ItemBgColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
cells = termui.DTrimTxCls(cells, c.List.InnerWidth())
|
|
|
|
|
|
|
|
x := 0
|
|
|
|
for _, cell := range cells {
|
|
|
|
width := cell.Width()
|
|
|
|
buf.Set(
|
|
|
|
c.List.InnerBounds().Min.X+x,
|
|
|
|
c.List.InnerBounds().Min.Y+y,
|
|
|
|
cell,
|
|
|
|
)
|
|
|
|
x += width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:34:02 +02:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:05:44 +02:00
|
|
|
func (c *Channels) Add() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:34:02 +02:00
|
|
|
// GetHeight implements interface termui.GridBufferer
|
|
|
|
func (c *Channels) GetHeight() int {
|
|
|
|
return c.List.Block.GetHeight()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWidth implements interface termui.GridBufferer
|
|
|
|
func (c *Channels) SetWidth(w int) {
|
|
|
|
c.List.SetWidth(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetX implements interface termui.GridBufferer
|
|
|
|
func (c *Channels) SetX(x int) {
|
|
|
|
c.List.SetX(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetY implements interface termui.GridBufferer
|
|
|
|
func (c *Channels) SetY(y int) {
|
|
|
|
c.List.SetY(y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channels) GetChannels(svc *service.SlackService) {
|
|
|
|
for _, slackChan := range svc.GetChannels() {
|
|
|
|
c.List.Items = append(c.List.Items, slackChan.Name)
|
2016-09-27 22:05:44 +02:00
|
|
|
c.SlackChannels = append(
|
|
|
|
c.SlackChannels,
|
|
|
|
SlackChannel{
|
|
|
|
ID: slackChan.ID,
|
|
|
|
Name: slackChan.Name,
|
|
|
|
},
|
|
|
|
)
|
2016-09-25 22:34:02 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 22:05:44 +02:00
|
|
|
|
|
|
|
func (c *Channels) SetSelectedChannel(num int) {
|
|
|
|
c.SelectedChannel = num
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channels) MoveCursorUp() {
|
|
|
|
if c.SelectedChannel > 0 {
|
|
|
|
c.SetSelectedChannel(c.SelectedChannel - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channels) MoveCursorDown() {
|
|
|
|
if c.SelectedChannel < len(c.List.Items)-1 {
|
|
|
|
c.SetSelectedChannel(c.SelectedChannel + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channels) NewMessage() {
|
|
|
|
}
|