Toggle Timers & i3status-rust support

This commit is contained in:
Brian Buller 2021-03-02 11:05:44 -06:00
parent fd0b0cfb56
commit ccdf797a1a
4 changed files with 87 additions and 10 deletions

View File

@ -6,7 +6,7 @@ import (
"strings"
timertxt "github.com/br0xen/go-timertxt"
"github.com/br0xen/user-config"
userConfig "github.com/br0xen/user-config"
)
type AppState struct {
@ -110,6 +110,18 @@ func (a *AppState) initialize() {
}
a.ValidOperations = make(map[string][]string)
a.OpFuncs = make(map[string]func([]string) int)
a.addOperation("i3status",
[]string{
"i3status - Output JSON for i3status-rust to consume",
},
a.opI3Status,
)
a.addOperation("toggle",
[]string{
"toggle - Pause/Resume the most recent timer",
},
a.opToggleTimer,
)
a.addOperation("time",
[]string{
"time [contexts] [projects] - Only output the total time value",

View File

@ -78,7 +78,7 @@ func getProjectsFromSlice(args []string) ([]string, []string) {
}
// getAdditionalTagsFromSlice pulls all '*:*' (tags) out of the
// string slice and returns those projects and the remaining
// string slice and returns those tags and the remaining
// strings from the slice
func getAdditionalTagsFromSlice(args []string) ([]string, []string) {
return splitSlice(args, func(v string) bool {

View File

@ -7,6 +7,22 @@ import (
timertxt "github.com/br0xen/go-timertxt"
)
func (a *AppState) getMostRecentTimer() (*timertxt.Timer, error) {
wrk, err := a.TimerList.GetMostRecentTimer()
if err != nil {
// Couldn't find a timer in the 'timer.txt'... Check the 'done' file
if err = a.LoadDoneList(); err != nil {
return nil, err
}
wrk, err = a.DoneList.GetMostRecentTimer()
if err != nil {
return nil, err
}
}
return wrk, nil
}
func (a *AppState) SetTimerFinished(id int, end time.Time) error {
var t *timertxt.Timer
var err error

View File

@ -10,6 +10,37 @@ import (
timertxt "github.com/br0xen/go-timertxt"
)
func (a *AppState) opI3Status(args []string) int {
state := "Idle"
wrk, err := a.getMostRecentTimer()
if err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error loading timer entry\"}")
return 0
}
var text string
if wrk.Finished {
text = fmt.Sprint(wrk.Duration().Round(time.Minute * 15))
} else {
text = fmt.Sprint(wrk.Duration().Round(time.Second))
// If the current time is before 7AM, after 5PM, or a weekend, use a Warning state
cTime := time.Now()
if cTime.Weekday() == time.Sunday || cTime.Weekday() == time.Saturday || cTime.Hour() < 7 || cTime.Hour() > 17 {
state = "Warning"
} else {
state = "Good"
}
}
for _, ctx := range wrk.Contexts {
text = fmt.Sprintf("%s @%s", text, ctx)
}
for _, prj := range wrk.Projects {
text = fmt.Sprintf("%s +%s", text, prj)
}
fmt.Printf("{\"icon\":\"time\",\"state\":\"%s\", \"text\": \"%s\"}", state, text)
return 0
}
func (a *AppState) opEditor(args []string) int {
editor := os.Getenv("EDITOR")
if editor == "" {
@ -18,14 +49,6 @@ func (a *AppState) opEditor(args []string) int {
}
fmt.Println(editor, a.directory+a.fileTimer)
return 0
/*
err := exec.Command(editor, a.directory+a.fileTimer).Run()
if err != nil {
fmt.Println("Error opening editor: " + err.Error())
return 1
}
return 0
*/
}
func (a *AppState) opStatus(args []string) int {
@ -290,6 +313,32 @@ func (a *AppState) opStartTimer(args []string) int {
return 0
}
func (a *AppState) opToggleTimer(args []string) int {
wrk, err := a.getMostRecentTimer()
if err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error loading timer entry\"}")
return 0
}
var startArgs []string
if wrk.Finished {
// Start a new timer with the same data
for _, v := range wrk.Contexts {
startArgs = append(startArgs, "@"+v)
}
for _, v := range wrk.Projects {
startArgs = append(startArgs, "+"+v)
}
for k, v := range wrk.AdditionalTags {
startArgs = append(startArgs, k+":"+v)
}
a.opStartTimer(startArgs)
} else {
// Stop the active timer
a.opStopTimer([]string{})
}
return 0
}
func (a *AppState) opStopTimer(args []string) int {
var err error
var wrk time.Time