erroneousboat-slack-term/config/config.go

123 lines
3.0 KiB
Go
Raw Normal View History

2016-09-25 22:34:02 +02:00
package config
import (
"encoding/json"
"errors"
2016-09-25 22:34:02 +02:00
"os"
"github.com/erroneousboat/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 {
SlackToken string `json:"slack_token"`
SidebarWidth int `json:"sidebar_width"`
MainWidth int `json:"-"`
2016-10-30 14:26:12 +01:00
KeyMap map[string]keyMapping `json:"key_map"`
2017-12-16 22:54:00 +01:00
Theme Theme `json:"theme"`
2016-09-25 22:34:02 +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) {
cfg := getDefaultConfig()
file, err := os.Open(filepath)
if err != nil {
return &cfg, err
}
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
return &cfg, err
}
if cfg.SlackToken == "" {
return &cfg, errors.New("couldn't find 'slack_token' parameter")
}
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
termui.ColorMap = map[string]termui.Attribute{
"fg": termui.StringToAttribute(cfg.Theme.View.Fg),
"bg": termui.StringToAttribute(cfg.Theme.View.Bg),
"border.fg": termui.StringToAttribute(cfg.Theme.View.BorderFg),
"label.fg": termui.StringToAttribute(cfg.Theme.View.LabelFg),
"par.fg": termui.StringToAttribute(cfg.Theme.View.ParFg),
"par.label.bg": termui.StringToAttribute(cfg.Theme.View.ParLabelFg),
}
return &cfg, nil
}
func getDefaultConfig() Config {
return Config{
SidebarWidth: 1,
MainWidth: 11,
2016-10-29 17:57:58 +02:00
KeyMap: map[string]keyMapping{
"command": {
"i": "mode-insert",
2017-07-15 23:56:39 +02:00
"/": "mode-search",
2016-10-29 17:57:58 +02:00
"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",
2017-12-17 13:48:20 +01:00
"n": "channel-search-next",
"N": "channel-search-prev",
2016-10-29 17:57:58 +02:00
"q": "quit",
2016-10-30 14:26:12 +01:00
"<f1>": "help",
},
2016-10-29 17:57:58 +02:00
"insert": {
"<left>": "cursor-left",
"<right>": "cursor-right",
"<enter>": "send",
"<escape>": "mode-command",
"<backspace>": "backspace",
"C-8": "backspace",
2016-10-29 17:57:58 +02:00
"<delete>": "delete",
"<space>": "space",
},
2017-07-15 23:56:39 +02:00
"search": {
"<left>": "cursor-left",
"<right>": "cursor-right",
"<escape>": "clear-input",
"<enter>": "clear-input",
"<backspace>": "backspace",
"C-8": "backspace",
"<delete>": "delete",
"<space>": "space",
},
},
2017-12-16 22:54:00 +01:00
Theme: Theme{
View: View{
Fg: "white",
Bg: "default",
BorderFg: "white",
LabelFg: "green,bold",
ParFg: "white",
ParLabelFg: "white",
2017-12-16 22:54:00 +01:00
},
Channel: Channel{
2017-12-22 13:48:36 +01:00
Prefix: "",
Icon: "",
Text: "",
},
Message: Message{
2017-12-22 13:48:36 +01:00
Time: "",
Name: "",
Text: "",
2017-12-16 22:54:00 +01:00
},
},
2016-10-10 21:45:31 +02:00
}
2016-09-25 22:34:02 +02:00
}