Updated TimerList struct

This commit is contained in:
2023-01-12 06:04:17 -06:00
parent c98a4dea0c
commit c412f54294
10 changed files with 260 additions and 27 deletions

View File

@@ -91,7 +91,7 @@ func opI3Status(cmd *cobra.Command, args []string) error {
getListTotal := func(list *timertxt.TimerList) string {
var isActive bool
var total time.Duration
for _, v := range *list {
for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)

View File

@@ -76,7 +76,7 @@ func opListTimers(cmd *cobra.Command, args []string) error {
list = list.Filter(filter)
dayTotals := make(map[string]time.Duration)
for _, v := range *list {
for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
@@ -84,7 +84,7 @@ func opListTimers(cmd *cobra.Command, args []string) error {
dayTotals[v.StartDate.Format("2006/01/02")] += dur
}
var oldDayStr, dayStr string
for _, v := range *list {
for _, v := range list.GetTimerSlice() {
oldDayStr = dayStr
dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr {

View File

@@ -42,8 +42,9 @@ func opMod(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
// We didn't have a timer id, so try to modify the first active timer
if len(*p.TimerList.GetActiveTimers()) > 0 {
timer = (*p.TimerList.GetActiveTimers())[0]
active := p.TimerList.GetActiveTimers().GetTimerSlice()
if len(active) > 0 {
timer = active[0]
} else {
// And we don't have any active timers
return fmt.Errorf("No active timers, 'id' must be provided: %w", err)

View File

@@ -34,12 +34,13 @@ func opStatus(cmd *cobra.Command, args []string) error {
if err := p.LoadTimerList(); err != nil {
return fmt.Errorf("Error loading timer list: %w", err)
}
if len(*p.TimerList.GetActiveTimers()) == 0 {
active := p.TimerList.GetActiveTimers().GetTimerSlice()
if len(active) == 0 {
fmt.Println("No timers running")
return nil
}
var currDur time.Duration
for _, v := range *p.TimerList {
for _, v := range active {
if v.ActiveToday() {
currDur += v.Duration()
}
@@ -47,7 +48,7 @@ func opStatus(cmd *cobra.Command, args []string) error {
d := util.Round(currDur)
fmt.Printf("%s ( %.2f hrs )\n", time.Now().Format(time.Stamp), util.DurationToDecimal(d))
for _, v := range *p.TimerList.GetActiveTimers() {
for _, v := range active {
fmt.Println(util.TimerToFriendlyString(v))
}
return nil

View File

@@ -51,7 +51,7 @@ func opStop(cmd *cobra.Command, args []string) error {
fmt.Println("Stopping at : " + end.Format(time.RFC3339))
var timerIds []int
if id == -1 {
for _, v := range *p.TimerList.GetActiveTimers() {
for _, v := range p.TimerList.GetActiveTimers().GetTimerSlice() {
timerIds = append(timerIds, v.Id)
}
} else {

View File

@@ -38,7 +38,7 @@ func opSwitch(cmd *cobra.Command, args []string) error {
var timerIds []int
end := time.Now()
// Stop all running timers and start a new one with the given args
for _, v := range *p.TimerList.GetActiveTimers() {
for _, v := range p.TimerList.GetActiveTimers().GetTimerSlice() {
timerIds = append(timerIds, v.Id)
}
fmt.Print("Stopping ", timerIds, "\n")

View File

@@ -37,7 +37,7 @@ func opShowTimers(cmd *cobra.Command, args []string) error {
list := p.GetFilteredTimerList(args)
var isActive bool
var total time.Duration
for _, v := range *list {
for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)