gime/cmd/i3status.go

119 lines
3.5 KiB
Go

/*
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"git.bullercodeworks.com/brian/go-timertxt"
"github.com/spf13/cobra"
)
// i3statusCmd represents the i3status command
var i3statusCmd = &cobra.Command{
Use: "i3status",
Short: "Output your timer.txt status in json for the i3 status bar",
RunE: opI3Status,
}
func init() {
rootCmd.AddCommand(i3statusCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// i3statusCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// i3statusCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func opI3Status(cmd *cobra.Command, args []string) error {
p := cli.Program{}
if err := p.Initialize(); err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error initializing gime\"}")
return nil
}
if err := p.LoadTimerList(); err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error loading timer list\"}")
return nil
}
state := "Idle"
wrk, err := p.GetMostRecentTimer()
if err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error loading timer entry\"}")
return nil
}
var text string
if wrk.FinishDate.IsZero() {
wrkDur := util.Round(wrk.Duration())
hrs := int(wrkDur.Hours())
mins := int(wrkDur.Minutes()) - hrs*60
if hrs > 0 {
text = fmt.Sprintf("%dh%dm", hrs, mins)
} else {
text = fmt.Sprintf("%dm", mins)
}
if !wrk.Finished {
// 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)
}
} else {
text = "("
for _, ctx := range wrk.Contexts {
text = fmt.Sprintf("%s@%s ", text, ctx)
}
for _, prj := range wrk.Projects {
text = fmt.Sprintf("%s+%s ", text, prj)
}
if text[len(text)-1] == ' ' {
text = text[:len(text)-1]
}
text = text + ")"
getListTotal := func(list *timertxt.TimerList) string {
var isActive bool
var total time.Duration
for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
isActive = true
}
total += dur
}
total = util.Round(total)
if isActive {
return fmt.Sprintf("%.2f+", util.DurationToDecimal(total))
} else {
return fmt.Sprintf("%.2f", util.DurationToDecimal(total))
}
}
dayList := p.GetFilteredTimerList([]string{"--a", util.BeginningOfDay().Format("2006-01-02"), "@bcw"})
text = text + " d:" + getListTotal(dayList)
weekList := p.GetFilteredTimerList([]string{"--a", util.BeginningOfWeek().Format("2006-01-02"), "@bcw"})
text = text + " w:" + getListTotal(weekList)
monthList := p.GetFilteredTimerList([]string{"--a", util.BeginningOfMonth().Format("2006-01-02"), "@bcw"})
text = text + " m:" + getListTotal(monthList)
}
fmt.Printf("{\"icon\":\"time\",\"state\":\"%s\", \"text\": \"%s\"}", state, text)
return nil
}