kevinschoon-pomo/types.go

188 lines
3.8 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-02-03 19:28:29 +01:00
const (
defaultDateTimeFmt = "2006-01-02 15:04"
)
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 {
2018-02-03 19:28:29 +01:00
Colors map[string]*color.Color
DateTimeFmt string
2018-01-20 17:51:27 +01:00
}
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 {
2018-02-03 19:28:29 +01:00
Colors map[string]string `json:"colors"`
DateTimeFmt string `json:"datetimefmt"`
2018-01-20 17:51:27 +01:00
}{}
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)
}
}
2018-02-03 19:28:29 +01:00
if config.DateTimeFmt != "" {
c.DateTimeFmt = config.DateTimeFmt
} else {
c.DateTimeFmt = defaultDateTimeFmt
}
2018-01-20 17:51:27 +01:00
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{},
}
2018-02-03 19:28:29 +01:00
err = json.Unmarshal(raw, config)
if err != nil {
return nil, err
}
2018-01-20 17:51:27 +01:00
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)
}