2016-09-25 22:34:02 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-10-18 17:55:37 +02:00
|
|
|
"errors"
|
2016-09-25 22:34:02 +02:00
|
|
|
"os"
|
2016-10-10 21:45:31 +02:00
|
|
|
|
|
|
|
"github.com/gizak/termui"
|
2016-09-25 22:34:02 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 16:07:35 +02:00
|
|
|
// Config is the definition of a Config struct
|
2016-09-25 22:34:02 +02:00
|
|
|
type Config struct {
|
2016-10-27 18:52:54 +02:00
|
|
|
SlackToken string `json:"slack_token"`
|
|
|
|
Theme string `json:"theme"`
|
|
|
|
SidebarWidth int `json:"sidebar_width"`
|
|
|
|
MainWidth int `json:"-"`
|
2016-10-30 14:26:12 +01:00
|
|
|
KeyMap map[string]keyMapping `json:"key_map"`
|
2016-09-25 22:34:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 18:52:54 +02:00
|
|
|
type keyMapping map[string]string
|
|
|
|
|
2016-10-02 16:07:35 +02:00
|
|
|
// NewConfig loads the config file and returns a Config struct
|
2016-09-25 22:34:02 +02:00
|
|
|
func NewConfig(filepath string) (*Config, error) {
|
2016-10-10 21:45:31 +02:00
|
|
|
cfg := Config{
|
2016-10-19 18:21:00 +02:00
|
|
|
Theme: "dark",
|
|
|
|
SidebarWidth: 1,
|
2016-10-21 16:21:13 +02:00
|
|
|
MainWidth: 11,
|
2016-10-29 17:57:58 +02:00
|
|
|
KeyMap: map[string]keyMapping{
|
|
|
|
"command": {
|
|
|
|
"i": "mode-insert",
|
|
|
|
"k": "channel-up",
|
|
|
|
"j": "channel-down",
|
|
|
|
"g": "channel-top",
|
|
|
|
"G": "channel-bottom",
|
|
|
|
"<previous>": "chat-up",
|
|
|
|
"C-b": "chat-up",
|
|
|
|
"C-u": "chat-up",
|
|
|
|
"<next>": "chat-down",
|
|
|
|
"C-f": "chat-down",
|
|
|
|
"C-d": "chat-down",
|
|
|
|
"q": "quit",
|
2016-10-30 14:26:12 +01:00
|
|
|
"<f1>": "help",
|
2016-10-27 18:52:54 +02:00
|
|
|
},
|
2016-10-29 17:57:58 +02:00
|
|
|
"insert": {
|
|
|
|
"<left>": "cursor-left",
|
|
|
|
"<right>": "cursor-right",
|
|
|
|
"<enter>": "send",
|
|
|
|
"<escape>": "mode-command",
|
|
|
|
"<backspace>": "backspace",
|
|
|
|
"<delete>": "delete",
|
|
|
|
"<space>": "space",
|
2016-10-27 18:52:54 +02:00
|
|
|
},
|
|
|
|
},
|
2016-10-10 21:45:31 +02:00
|
|
|
}
|
2016-09-25 22:34:02 +02:00
|
|
|
|
|
|
|
file, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return &cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
|
|
|
|
return &cfg, err
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:55:37 +02:00
|
|
|
if cfg.SlackToken == "" {
|
|
|
|
return &cfg, errors.New("couldn't find 'slack_token' parameter")
|
|
|
|
}
|
|
|
|
|
2016-10-21 16:21:13 +02:00
|
|
|
if cfg.SidebarWidth < 1 || cfg.SidebarWidth > 11 {
|
|
|
|
return &cfg, errors.New("please specify the 'sidebar_width' between 1 and 11")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.MainWidth = 12 - cfg.SidebarWidth
|
|
|
|
|
2016-10-10 21:45:31 +02:00
|
|
|
if cfg.Theme == "light" {
|
|
|
|
termui.ColorMap = map[string]termui.Attribute{
|
|
|
|
"fg": termui.ColorBlack,
|
2016-10-14 16:22:47 +02:00
|
|
|
"bg": termui.ColorWhite,
|
2016-10-10 21:45:31 +02:00
|
|
|
"border.fg": termui.ColorBlack,
|
2016-10-11 19:28:37 +02:00
|
|
|
"label.fg": termui.ColorBlue,
|
2016-10-10 21:45:31 +02:00
|
|
|
"par.fg": termui.ColorYellow,
|
|
|
|
"par.label.bg": termui.ColorWhite,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:34:02 +02:00
|
|
|
return &cfg, nil
|
|
|
|
}
|