Commit
+115 -79 +/-4 browse
1 | diff --git a/config.go b/config.go |
2 | new file mode 100644 |
3 | index 0000000..777e82e |
4 | --- /dev/null |
5 | +++ b/config.go |
6 | @@ -0,0 +1,102 @@ |
7 | + package main |
8 | + |
9 | + import ( |
10 | + "encoding/json" |
11 | + "io/ioutil" |
12 | + "os" |
13 | + "path" |
14 | + |
15 | + "github.com/fatih/color" |
16 | + ) |
17 | + |
18 | + const ( |
19 | + defaultDateTimeFmt = "2006-01-02 15:04" |
20 | + ) |
21 | + |
22 | + // Config represents user preferences |
23 | + type Config struct { |
24 | + Colors *ColorMap `json:"colors"` |
25 | + DateTimeFmt string `json:"dateTimeFmt"` |
26 | + DBPath string `json:"dbPath"` |
27 | + } |
28 | + |
29 | + type ColorMap struct { |
30 | + colors map[string]*color.Color |
31 | + tags map[string]string |
32 | + } |
33 | + |
34 | + func (c *ColorMap) Get(name string) *color.Color { |
35 | + if color, ok := c.colors[name]; ok { |
36 | + return color |
37 | + } |
38 | + return nil |
39 | + } |
40 | + |
41 | + func (c *ColorMap) MarshalJSON() ([]byte, error) { |
42 | + return json.Marshal(c.tags) |
43 | + } |
44 | + |
45 | + func (c *ColorMap) UnmarshalJSON(raw []byte) error { |
46 | + lookup := map[string]*color.Color{ |
47 | + "black": color.New(color.FgBlack), |
48 | + "hiblack": color.New(color.FgHiBlack), |
49 | + "blue": color.New(color.FgBlue), |
50 | + "hiblue": color.New(color.FgHiBlue), |
51 | + "cyan": color.New(color.FgCyan), |
52 | + "hicyan": color.New(color.FgHiCyan), |
53 | + "green": color.New(color.FgGreen), |
54 | + "higreen": color.New(color.FgHiGreen), |
55 | + "magenta": color.New(color.FgMagenta), |
56 | + "himagenta": color.New(color.FgHiMagenta), |
57 | + "red": color.New(color.FgRed), |
58 | + "hired": color.New(color.FgHiRed), |
59 | + "white": color.New(color.FgWhite), |
60 | + "hiwrite": color.New(color.FgHiWhite), |
61 | + "yellow": color.New(color.FgYellow), |
62 | + "hiyellow": color.New(color.FgHiYellow), |
63 | + } |
64 | + cm := &ColorMap{ |
65 | + colors: map[string]*color.Color{}, |
66 | + tags: map[string]string{}, |
67 | + } |
68 | + err := json.Unmarshal(raw, &cm.tags) |
69 | + if err != nil { |
70 | + return err |
71 | + } |
72 | + for tag, colorName := range cm.tags { |
73 | + if color, ok := lookup[colorName]; ok { |
74 | + cm.colors[tag] = color |
75 | + } |
76 | + } |
77 | + *c = *cm |
78 | + return nil |
79 | + } |
80 | + |
81 | + func NewConfig(configPath string) (*Config, error) { |
82 | + raw, err := ioutil.ReadFile(configPath) |
83 | + if err != nil { |
84 | + // Create an empty config file |
85 | + // if it does not already exist. |
86 | + if os.IsNotExist(err) { |
87 | + raw, _ := json.Marshal(map[string]string{}) |
88 | + err := ioutil.WriteFile(configPath, raw, 0644) |
89 | + if err != nil { |
90 | + return nil, err |
91 | + } |
92 | + return NewConfig(configPath) |
93 | + } |
94 | + return nil, err |
95 | + } |
96 | + config := &Config{} |
97 | + err = json.Unmarshal(raw, config) |
98 | + if err != nil { |
99 | + return nil, err |
100 | + } |
101 | + if config.DateTimeFmt == "" { |
102 | + config.DateTimeFmt = defaultDateTimeFmt |
103 | + } |
104 | + if config.DBPath == "" { |
105 | + config.DBPath = path.Dir(configPath) + "/pomo.db" |
106 | + } |
107 | + return config, nil |
108 | + } |
109 | diff --git a/main.go b/main.go |
110 | index bbe75b1..e4c1f47 100644 |
111 | --- a/main.go |
112 | +++ b/main.go |
113 | @@ -207,6 +207,17 @@ func _status(path *string) func(*cli.Cmd) { |
114 | } |
115 | } |
116 | |
117 | + func config(path *string) func(*cli.Cmd) { |
118 | + return func(cmd *cli.Cmd) { |
119 | + cmd.Spec = "[OPTIONS]" |
120 | + cmd.Action = func() { |
121 | + config, err := NewConfig(*path + "/config.json") |
122 | + maybe(err) |
123 | + maybe(json.NewEncoder(os.Stdout).Encode(config)) |
124 | + } |
125 | + } |
126 | + } |
127 | + |
128 | func main() { |
129 | app := cli.App("pomo", "Pomodoro CLI") |
130 | app.LongDesc = "Pomo helps you track what you did, how long it took you to do it, and how much effort you expect it to take." |
131 | @@ -217,6 +228,7 @@ func main() { |
132 | app.Version("v version", Version) |
133 | app.Command("start s", "start a new task", start(path)) |
134 | app.Command("init", "initialize the sqlite database", initialize(path)) |
135 | + app.Command("config cf", "display the current configuration", config(path)) |
136 | app.Command("create c", "create a new task without starting", create(path)) |
137 | app.Command("begin b", "begin requested pomodoro", begin(path)) |
138 | app.Command("list l", "list historical tasks", list(path)) |
139 | diff --git a/types.go b/types.go |
140 | index ea36f1b..d248aed 100644 |
141 | --- a/types.go |
142 | +++ b/types.go |
143 | @@ -1,18 +1,11 @@ |
144 | package main |
145 | |
146 | import ( |
147 | - "encoding/json" |
148 | - "fmt" |
149 | "io/ioutil" |
150 | "os" |
151 | "time" |
152 | |
153 | "github.com/0xAX/notificator" |
154 | - "github.com/fatih/color" |
155 | - ) |
156 | - |
157 | - const ( |
158 | - defaultDateTimeFmt = "2006-01-02 15:04" |
159 | ) |
160 | |
161 | type State int |
162 | @@ -59,77 +52,6 @@ func (w *Wheel) String() string { |
163 | return "" |
164 | } |
165 | |
166 | - // Config represents user preferences |
167 | - type Config struct { |
168 | - Colors map[string]*color.Color |
169 | - DateTimeFmt string |
170 | - } |
171 | - |
172 | - var colorMap = map[string]*color.Color{ |
173 | - "black": color.New(color.FgBlack), |
174 | - "hiblack": color.New(color.FgHiBlack), |
175 | - "blue": color.New(color.FgBlue), |
176 | - "hiblue": color.New(color.FgHiBlue), |
177 | - "cyan": color.New(color.FgCyan), |
178 | - "hicyan": color.New(color.FgHiCyan), |
179 | - "green": color.New(color.FgGreen), |
180 | - "higreen": color.New(color.FgHiGreen), |
181 | - "magenta": color.New(color.FgMagenta), |
182 | - "himagenta": color.New(color.FgHiMagenta), |
183 | - "red": color.New(color.FgRed), |
184 | - "hired": color.New(color.FgHiRed), |
185 | - "white": color.New(color.FgWhite), |
186 | - "hiwrite": color.New(color.FgHiWhite), |
187 | - "yellow": color.New(color.FgYellow), |
188 | - "hiyellow": color.New(color.FgHiYellow), |
189 | - } |
190 | - |
191 | - func (c *Config) UnmarshalJSON(raw []byte) error { |
192 | - config := &struct { |
193 | - Colors map[string]string `json:"colors"` |
194 | - DateTimeFmt string `json:"datetimefmt"` |
195 | - }{} |
196 | - err := json.Unmarshal(raw, config) |
197 | - if err != nil { |
198 | - return err |
199 | - } |
200 | - for key, name := range config.Colors { |
201 | - if color, ok := colorMap[name]; ok { |
202 | - c.Colors[key] = color |
203 | - } else { |
204 | - return fmt.Errorf("bad color choice: %s", name) |
205 | - } |
206 | - } |
207 | - if config.DateTimeFmt != "" { |
208 | - c.DateTimeFmt = config.DateTimeFmt |
209 | - } else { |
210 | - c.DateTimeFmt = defaultDateTimeFmt |
211 | - } |
212 | - return nil |
213 | - } |
214 | - |
215 | - func NewConfig(path string) (*Config, error) { |
216 | - raw, err := ioutil.ReadFile(path) |
217 | - if err != nil { |
218 | - // Create an empty config file |
219 | - // if it does not already exist. |
220 | - if os.IsNotExist(err) { |
221 | - raw, _ := json.Marshal(map[string]*color.Color{}) |
222 | - ioutil.WriteFile(path, raw, 0644) |
223 | - return NewConfig(path) |
224 | - } |
225 | - return nil, err |
226 | - } |
227 | - config := &Config{ |
228 | - Colors: map[string]*color.Color{}, |
229 | - } |
230 | - err = json.Unmarshal(raw, config) |
231 | - if err != nil { |
232 | - return nil, err |
233 | - } |
234 | - return config, json.Unmarshal(raw, config) |
235 | - } |
236 | - |
237 | // Task describes some activity |
238 | type Task struct { |
239 | ID int `json:"id"` |
240 | diff --git a/util.go b/util.go |
241 | index b65288a..d49501c 100644 |
242 | --- a/util.go |
243 | +++ b/util.go |
244 | @@ -62,7 +62,7 @@ func summerizeTasks(config *Config, tasks []*Task) { |
245 | fmt.Printf(" ") |
246 | } |
247 | // user specified color mapping exists |
248 | - if color, ok := config.Colors[tag]; ok { |
249 | + if color := config.Colors.Get(tag); color != nil { |
250 | color.Printf("%s", tag) |
251 | } else { |
252 | // no color mapping |