kevinschoon-pomo/types.go

173 lines
3.5 KiB
Go
Raw Normal View History

2018-01-16 10:50:08 +01:00
package main
import (
2018-01-20 17:51:27 +01:00
"encoding/json"
"fmt"
"io/ioutil"
"os"
2018-01-16 10:50:08 +01:00
"time"
"github.com/0xAX/notificator"
"github.com/fatih/color"
2018-01-16 10:50:08 +01:00
)
2018-01-26 16:07:38 +01:00
type State int
func (s State) String() string {
switch s {
case RUNNING:
return "RUNNING"
case BREAKING:
return "BREAKING"
case COMPLETE:
return "COMPLETE"
case PAUSED:
return "PAUSED"
}
return ""
}
const (
RUNNING State = iota + 1
BREAKING
COMPLETE
PAUSED
)
2018-01-20 18:49:07 +01:00
// Wheel keeps track of an ASCII spinner
2018-01-27 16:58:56 +01:00
type Wheel int
2018-01-20 18:49:07 +01:00
func (w *Wheel) String() string {
2018-01-27 16:58:56 +01:00
switch int(*w) {
2018-01-20 18:49:07 +01:00
case 0:
2018-01-27 16:58:56 +01:00
*w++
2018-01-20 18:49:07 +01:00
return "|"
case 1:
2018-01-27 16:58:56 +01:00
*w++
2018-01-20 18:49:07 +01:00
return "/"
case 2:
2018-01-27 16:58:56 +01:00
*w++
2018-01-20 18:49:07 +01:00
return "-"
case 3:
2018-01-27 16:58:56 +01:00
*w = 0
2018-01-20 18:49:07 +01:00
return "\\"
}
return ""
2018-01-20 13:03:23 +01:00
}
2018-01-20 17:51:27 +01:00
// Config represents user preferences
type Config struct {
Colors map[string]*color.Color
}
var colorMap = map[string]*color.Color{
"red": color.New(color.FgRed),
"blue": color.New(color.FgBlue),
"green": color.New(color.FgGreen),
"white": color.New(color.FgWhite),
}
func (c *Config) UnmarshalJSON(raw []byte) error {
config := &struct {
Colors map[string]string `json:"colors"`
}{}
err := json.Unmarshal(raw, config)
if err != nil {
return err
}
for key, name := range config.Colors {
if color, ok := colorMap[name]; ok {
c.Colors[key] = color
} else {
return fmt.Errorf("bad color choice: %s", name)
}
}
return nil
}
func NewConfig(path string) (*Config, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
// Create an empty config file
// if it does not already exist.
if os.IsNotExist(err) {
raw, _ := json.Marshal(map[string]*color.Color{})
ioutil.WriteFile(path, raw, 0644)
return NewConfig(path)
}
2018-01-20 17:51:27 +01:00
return nil, err
}
config := &Config{
Colors: map[string]*color.Color{},
}
return config, json.Unmarshal(raw, config)
}
2018-01-16 10:50:08 +01:00
// Task describes some activity
type Task struct {
ID int `json:"id"`
Message string `json:"message"`
// Array of completed pomodoros
2018-01-20 13:17:58 +01:00
Pomodoros []*Pomodoro `json:"pomodoros"`
2018-01-20 11:01:53 +01:00
// Free-form tags associated with this task
2018-01-20 13:03:23 +01:00
Tags []string `json:"tags"`
2018-01-20 13:17:58 +01:00
// Number of pomodoros for this task
NPomodoros int `json:"n_pomodoros"`
// Duration of each pomodoro
Duration time.Duration `json:"duration"`
2018-01-16 10:50:08 +01:00
}
2018-01-20 18:20:01 +01:00
// ByID is a sortable array of tasks
type ByID []*Task
func (b ByID) Len() int { return len(b) }
func (b ByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByID) Less(i, j int) bool { return b[i].ID < b[j].ID }
2018-01-20 13:17:58 +01:00
// Pomodoro is a unit of time to spend working
// on a single task.
type Pomodoro struct {
2018-01-16 13:02:35 +01:00
Start time.Time `json:"start"`
End time.Time `json:"end"`
2018-01-16 10:50:08 +01:00
}
2018-01-23 15:47:40 +01:00
// Duration returns the runtime of the pomodoro
func (p Pomodoro) Duration() time.Duration {
return (p.End.Sub(p.Start))
}
2018-01-31 13:43:41 +01:00
// Notifier sends a system notification
type Notifier interface {
2018-01-26 16:07:38 +01:00
Notify(string, string) error
2018-01-16 10:50:08 +01:00
}
2018-01-27 17:42:13 +01:00
// NoopNotifier does nothing
type NoopNotifier struct{}
func (n NoopNotifier) Notify(string, string) error { return nil }
2018-01-31 13:43:41 +01:00
// Xnotifier can push notifications to mac, linux and windows.
type Xnotifier struct {
*notificator.Notificator
iconPath string
}
2018-01-31 13:43:41 +01:00
func NewXnotifier(iconPath string) Notifier {
// Write the built-in tomato icon if it
// doesn't already exist.
_, err := os.Stat(iconPath)
if os.IsNotExist(err) {
raw := MustAsset("tomato-icon.png")
_ = ioutil.WriteFile(iconPath, raw, 0644)
}
2018-01-31 13:43:41 +01:00
return Xnotifier{
Notificator: notificator.New(notificator.Options{}),
iconPath: iconPath,
}
}
// Notify sends a notification to the OS.
2018-01-31 13:43:41 +01:00
func (n Xnotifier) Notify(title, body string) error {
return n.Push(title, body, n.iconPath, notificator.UR_NORMAL)
}