forked from jpbruinsslot/slack-term
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
136 lines (122 loc) · 3.39 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/erroneousboat/termui"
)
const (
NotifyAll = "all"
NotifyMention = "mention"
)
// Config is the definition of a Config struct
type Config struct {
SlackToken string `json:"slack_token"`
Notify string `json:"notify"`
Emoji bool `json:"emoji"`
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, fmt.Errorf("couldn't find the slack-term config file: %v", err)
}
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
return &cfg, fmt.Errorf("the slack-term config file isn't valid json: %v", err)
}
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
switch cfg.Notify {
case NotifyAll, NotifyMention, "":
break
default:
return &cfg, fmt.Errorf("unsupported setting for notify: %s", cfg.Notify)
}
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),
"border.bg": termui.StringToAttribute(cfg.Theme.View.BorderBg),
"label.fg": termui.StringToAttribute(cfg.Theme.View.LabelFg),
"label.bg": termui.StringToAttribute(cfg.Theme.View.LabelBg),
}
return &cfg, nil
}
func getDefaultConfig() Config {
return Config{
SidebarWidth: 1,
MainWidth: 11,
Notify: "",
Emoji: false,
KeyMap: map[string]keyMapping{
"command": {
"i": "mode-insert",
"/": "mode-search",
"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",
"n": "channel-search-next",
"N": "channel-search-prev",
"q": "quit",
"<f1>": "help",
},
"insert": {
"<left>": "cursor-left",
"<right>": "cursor-right",
"<enter>": "send",
"<escape>": "mode-command",
"<backspace>": "backspace",
"C-8": "backspace",
"<delete>": "delete",
"<space>": "space",
},
"search": {
"<left>": "cursor-left",
"<right>": "cursor-right",
"<escape>": "clear-input",
"<enter>": "clear-input",
"<backspace>": "backspace",
"C-8": "backspace",
"<delete>": "delete",
"<space>": "space",
},
},
Theme: Theme{
View: View{
Fg: "white",
Bg: "default",
BorderFg: "white",
BorderBg: "",
LabelFg: "green,bold",
LabelBg: "",
},
Channel: Channel{
Prefix: "",
Icon: "",
Text: "",
},
Message: Message{
Time: "",
TimeFormat: "15:04",
Name: "",
Text: "",
},
},
}
}