package config 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"` SidebarWidth int `json:"sidebar_width"` MainWidth int `json:"-"` KeyMap map[string]keyMapping `json:"key_map"` Theme Theme `json:"theme"` } type keyMapping map[string]string // NewConfig loads the config file and returns a Config struct 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, KeyMap: map[string]keyMapping{ "command": { "i": "mode-insert", "/": "mode-search", "k": "channel-up", "j": "channel-down", "g": "channel-top", "G": "channel-bottom", "": "chat-up", "C-b": "chat-up", "C-u": "chat-up", "": "chat-down", "C-f": "chat-down", "C-d": "chat-down", "n": "channel-search-next", "N": "channel-search-prev", "q": "quit", "": "help", }, "insert": { "": "cursor-left", "": "cursor-right", "": "send", "": "mode-command", "": "backspace", "C-8": "backspace", "": "delete", "": "space", }, "search": { "": "cursor-left", "": "cursor-right", "": "clear-input", "": "clear-input", "": "backspace", "C-8": "backspace", "": "delete", "": "space", }, }, Theme: Theme{ View: View{ Fg: "white", Bg: "default", BorderFg: "white", LabelFg: "green,bold", ParFg: "white", ParLabelFg: "white", }, Channel: Channel{ Prefix: "", Icon: "", Text: "", }, Message: Message{ Time: "", Name: "", Text: "", }, }, } }