gime/cmd/i3status.go

119 lines
3.5 KiB
Go
Raw Permalink Normal View History

2022-01-19 18:56:31 +00:00
/*
2022-01-19 21:30:30 +00:00
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
2022-01-19 18:56:31 +00:00
*/
package cmd
import (
"fmt"
2022-01-19 21:20:29 +00:00
"time"
2022-01-19 18:56:31 +00:00
2022-01-19 21:20:29 +00:00
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"git.bullercodeworks.com/brian/go-timertxt"
2022-01-19 18:56:31 +00:00
"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,
2022-01-19 18:56:31 +00:00
}
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")
}
2022-01-19 21:20:29 +00:00
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"
2023-04-14 16:02:16 +00:00
wrk, err := p.GetActiveOrMostRecent()
2022-01-19 21:20:29 +00:00
if err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error loading timer entry\"}")
return nil
}
var text string
if wrk.FinishDate.IsZero() {
2022-10-04 17:37:11 +00:00
wrkDur := util.Round(wrk.Duration())
2022-01-19 21:20:29 +00:00
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
2023-01-12 12:04:17 +00:00
for _, v := range list.GetTimerSlice() {
2022-01-19 21:20:29 +00:00
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
isActive = true
}
total += dur
}
2022-10-04 17:37:11 +00:00
total = util.Round(total)
2022-01-19 21:20:29 +00:00
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
}