Start with theming functionality

This commit is contained in:
erroneousboat 2017-12-16 22:54:00 +01:00
parent 64c2257df0
commit 6ebeb7b5fc
8 changed files with 117 additions and 35 deletions

View File

@ -18,19 +18,23 @@ type Message struct {
Content string
}
func (m Message) ToString(timeAttr string, nameAttr string, contentAttr string) string {
func (m Message) ToString(stlTime string, stlName string, stlContent string) string {
if (m.Time != time.Time{} && m.Name != "") {
return html.UnescapeString(
fmt.Sprintf(
"[[%s]](fg-red,fg-bold) [<%s>](fg-blue,fg-bold) %s",
"[[%s]](%s) [<%s>](%s) [%s](%s)",
m.Time.Format("15:04"),
stlTime,
m.Name,
stlName,
m.Content,
stlContent,
),
)
} else {
return html.UnescapeString(
fmt.Sprintf("%s", m.Content),
fmt.Sprintf("[%s](%s)", m.Content, stlContent),
)
}
}

View File

@ -4,17 +4,16 @@ import (
"encoding/json"
"errors"
"os"
"github.com/erroneousboat/termui"
)
// Config is the definition of a Config struct
type Config struct {
SlackToken string `json:"slack_token"`
Theme string `json:"theme"`
SlackToken string `json:"slack_token"`
// Theme string `json:"theme"`
SidebarWidth int `json:"sidebar_width"`
MainWidth int `json:"-"`
KeyMap map[string]keyMapping `json:"key_map"`
Theme Theme `json:"theme"`
}
type keyMapping map[string]string
@ -22,7 +21,7 @@ type keyMapping map[string]string
// NewConfig loads the config file and returns a Config struct
func NewConfig(filepath string) (*Config, error) {
cfg := Config{
Theme: "dark",
// Theme: "dark",
SidebarWidth: 1,
MainWidth: 11,
KeyMap: map[string]keyMapping{
@ -63,6 +62,18 @@ func NewConfig(filepath string) (*Config, error) {
"<space>": "space",
},
},
Theme: Theme{
Message: Message{
Time: "fg-red,fg-bold",
Name: "fg-blue,fg-bold",
Content: "",
},
Channel: Channel{
Prefix: "",
Icon: "fg-red",
Name: "",
},
},
}
file, err := os.Open(filepath)
@ -84,16 +95,16 @@ func NewConfig(filepath string) (*Config, error) {
cfg.MainWidth = 12 - cfg.SidebarWidth
if cfg.Theme == "light" {
termui.ColorMap = map[string]termui.Attribute{
"fg": termui.ColorBlack,
"bg": termui.ColorWhite,
"border.fg": termui.ColorBlack,
"label.fg": termui.ColorBlue,
"par.fg": termui.ColorYellow,
"par.label.bg": termui.ColorWhite,
}
}
// if cfg.Theme == "light" {
// termui.ColorMap = map[string]termui.Attribute{
// "fg": termui.ColorBlack,
// "bg": termui.ColorWhite,
// "border.fg": termui.ColorBlack,
// "label.fg": termui.ColorBlue,
// "par.fg": termui.ColorYellow,
// "par.label.bg": termui.ColorWhite,
// }
// }
return &cfg, nil
}

18
config/theme.go Normal file
View File

@ -0,0 +1,18 @@
package config
type Theme struct {
Message Message `json:"message"`
Channel Channel `json:"channel"`
}
type Message struct {
Time string `json:"time"`
Name string `json:"name"`
Content string `json:"content"`
}
type Channel struct {
Prefix string `json:"prefix"`
Icon string `json:"icon"`
Name string `json:"name"`
}

View File

@ -44,13 +44,13 @@ func CreateAppContext(flgConfig string, flgDebug bool) (*AppContext, error) {
}
// Create Service
svc, err := service.NewSlackService(config.SlackToken)
svc, err := service.NewSlackService(config)
if err != nil {
return nil, err
}
// Create the main view
view := views.CreateView(svc)
view := views.CreateView(config, svc)
// Setup the interface
if flgDebug {

View File

@ -110,7 +110,11 @@ func messageHandler(ctx *context.AppContext) {
// when attachments are added to message
for i := len(msg) - 1; i >= 0; i-- {
ctx.View.Chat.AddMessage(
msg[i].ToString(),
msg[i].ToString(
ctx.Config.Theme.Message.Time,
ctx.Config.Theme.Message.Name,
ctx.Config.Theme.Message.Content,
),
)
}
@ -268,7 +272,14 @@ func actionGetMessages(ctx *context.AppContext) {
var strMsgs []string
for _, msg := range msgs {
strMsgs = append(strMsgs, msg.ToString())
strMsgs = append(
strMsgs,
msg.ToString(
ctx.Config.Theme.Message.Time,
ctx.Config.Theme.Message.Name,
ctx.Config.Theme.Message.Content,
),
)
}
ctx.View.Chat.SetMessages(strMsgs)
@ -339,7 +350,14 @@ func actionChangeChannel(ctx *context.AppContext) {
var strMsgs []string
for _, msg := range msgs {
strMsgs = append(strMsgs, msg.ToString())
strMsgs = append(
strMsgs,
msg.ToString(
ctx.Config.Theme.Message.Time,
ctx.Config.Theme.Message.Name,
ctx.Config.Theme.Message.Content,
),
)
}
// Set messages for the channel

View File

@ -25,7 +25,7 @@ type Channel struct {
// ToString will set the label of the channel, how it will be
// displayed on screen. Based on the type, different icons are
// shown, as well as an optional notification icon.
func (c Channel) ToString() string {
func (c Channel) ToString(stlPrefix string, stlIcon string, stlName string) string {
var prefix string
if c.Notification {
prefix = components.IconNotification
@ -33,14 +33,13 @@ func (c Channel) ToString() string {
prefix = " "
}
var label string
var icon string
switch c.Type {
case ChannelTypeChannel:
label = fmt.Sprintf("%s %s %s", prefix, components.IconChannel, c.Name)
icon = components.IconChannel
case ChannelTypeGroup:
label = fmt.Sprintf("%s %s %s", prefix, components.IconGroup, c.Name)
icon = components.IconGroup
case ChannelTypeIM:
var icon string
switch c.Presence {
case PresenceActive:
icon = components.IconOnline
@ -49,9 +48,15 @@ func (c Channel) ToString() string {
default:
icon = components.IconIM
}
label = fmt.Sprintf("%s %s %s", prefix, icon, c.Name)
}
label := fmt.Sprintf(
"[%s](%s) [%s](%s) [%s](%s)",
prefix, stlPrefix,
icon, stlIcon,
c.Name, stlName,
)
return label
}

View File

@ -22,6 +22,7 @@ const (
)
type SlackService struct {
Config *config.Config
Client *slack.Client
RTM *slack.RTM
SlackChannels []interface{}
@ -33,9 +34,10 @@ type SlackService struct {
// NewSlackService is the constructor for the SlackService and will initialize
// the RTM and a Client
func NewSlackService(token string) (*SlackService, error) {
func NewSlackService(config *config.Config) (*SlackService, error) {
svc := &SlackService{
Client: slack.New(token),
Config: config,
Client: slack.New(config.SlackToken),
UserCache: make(map[string]string),
}
@ -153,7 +155,14 @@ func (s *SlackService) GetChannels() []string {
var channels []string
for _, chn := range s.Channels {
channels = append(channels, chn.ToString())
channels = append(
channels,
chn.ToString(
s.Config.Theme.Channel.Prefix,
s.Config.Theme.Channel.Icon,
s.Config.Theme.Channel.Name,
),
)
}
return channels
}
@ -162,7 +171,14 @@ func (s *SlackService) GetChannels() []string {
func (s *SlackService) ChannelsToString() []string {
var channels []string
for _, chn := range s.Channels {
channels = append(channels, chn.ToString())
channels = append(
channels,
chn.ToString(
s.Config.Theme.Channel.Prefix,
s.Config.Theme.Channel.Icon,
s.Config.Theme.Channel.Name,
),
)
}
return channels
}

View File

@ -4,10 +4,12 @@ import (
"github.com/erroneousboat/termui"
"github.com/erroneousboat/slack-term/components"
"github.com/erroneousboat/slack-term/config"
"github.com/erroneousboat/slack-term/service"
)
type View struct {
Config *config.Config
Input *components.Input
Chat *components.Chat
Channels *components.Channels
@ -15,7 +17,7 @@ type View struct {
Debug *components.Debug
}
func CreateView(svc *service.SlackService) *View {
func CreateView(config *config.Config, svc *service.SlackService) *View {
// Create Input component
input := components.CreateInputComponent()
@ -37,7 +39,14 @@ func CreateView(svc *service.SlackService) *View {
var strMsgs []string
for _, msg := range msgs {
strMsgs = append(strMsgs, msg.ToString())
strMsgs = append(
strMsgs,
msg.ToString(
config.Theme.Message.Time,
config.Theme.Message.Name,
config.Theme.Message.Content,
),
)
}
chat.SetMessages(strMsgs)
@ -50,6 +59,7 @@ func CreateView(svc *service.SlackService) *View {
mode := components.CreateModeComponent()
view := &View{
Config: config,
Input: input,
Channels: channels,
Chat: chat,