kevinschoon-pomo/pkg/internal/types.go

151 lines
3.1 KiB
Go
Raw Normal View History

2020-09-08 18:35:47 +02:00
package pomo
2018-01-16 10:50:08 +01:00
import (
2018-01-20 17:51:27 +01:00
"io/ioutil"
"os"
2018-01-16 10:50:08 +01:00
"time"
"github.com/0xAX/notificator"
2018-02-03 19:28:29 +01:00
)
2018-01-26 16:07:38 +01:00
type State int
func (s State) String() string {
switch s {
case CREATED:
return "CREATED"
2018-01-26 16:07:38 +01:00
case RUNNING:
return "RUNNING"
case BREAKING:
return "BREAKING"
case COMPLETE:
return "COMPLETE"
case PAUSED:
return "PAUSED"
}
return ""
}
const (
CREATED State = iota
RUNNING
2018-01-26 16:07:38 +01:00
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-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 }
// After returns tasks that were started after the
// provided start time.
func After(start time.Time, tasks []*Task) []*Task {
filtered := []*Task{}
for _, task := range tasks {
if len(task.Pomodoros) > 0 {
if start.Before(task.Pomodoros[0].Start) {
filtered = append(filtered, task)
}
}
}
return filtered
}
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))
}
// Status is used to communicate the state
// of a running Pomodoro session
type Status struct {
TaskID int `json:"task_id"`
TaskMessage string `json:"task_message"`
State State `json:"state"`
Remaining time.Duration `json:"remaining"`
Pauseduration time.Duration `json:"pauseduration"`
Count int `json:"count"`
NPomodoros int `json:"n_pomodoros"`
}
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)
}