I3Status Op and default root op

This commit is contained in:
Brian Buller 2022-01-19 15:20:29 -06:00
parent c957d43d84
commit 9e67002b0d
3 changed files with 108 additions and 6 deletions

View File

@ -144,3 +144,22 @@ func (p *Program) GetFilteredTimerList(args []string) *timertxt.TimerList {
}
return list.Filter(doFilters)
}
func (p *Program) GetMostRecentTimer() (*timertxt.Timer, error) {
work, wErr := p.TimerList.GetMostRecentTimer()
if wErr == nil && work.FinishDate.IsZero() {
return work, nil
}
if err := p.LoadDoneList(); err != nil {
return nil, err
}
done, dErr := p.DoneList.GetMostRecentTimer()
if dErr != nil {
return nil, dErr
}
if !done.FinishDate.IsZero() && work == nil || done.FinishDate.After(work.FinishDate) {
return done, nil
}
return work, nil
}

View File

@ -6,23 +6,25 @@ 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: "A brief description of your command",
Short: "",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("i3status called")
},
RunE: opI3Status,
}
func init() {
@ -38,3 +40,85 @@ func init() {
// 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 := wrk.Duration().Round(time.Minute * 15)
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 {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
isActive = true
}
total += dur
}
total = total.Round(util.GetRoundToDuration())
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
}

View File

@ -23,8 +23,7 @@ var (
Use: "gime",
Short: "Timer.txt Client",
Long: "Gime is a timer.txt client to make all aspects of timekeeping simpler.",
Run: func(cmd *cobra.Command, args []string) {
},
RunE: opStatus,
}
)