Skip to content

Commit

Permalink
cmd: Add workday command
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphexion committed Oct 5, 2021
1 parent ada3049 commit 123da8c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func init() {
rootCmd.AddCommand(NewTimerCmd())
rootCmd.AddCommand(NewProjectCmd())
rootCmd.AddCommand(NewEntryCmd())
rootCmd.AddCommand(NewWorkdayCmd())
}

func initConfig() {
Expand Down
78 changes: 78 additions & 0 deletions cmd/workday.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"fmt"
"log"
"nina/backend"
"nina/noko"
"time"

"github.com/spf13/cobra"
)

func NewWorkdayCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "workday",
Short: "Summarize your workday",
Run: BackendRunCmd(workdayCmd),
}

return rootCmd
}

func workdayCmd(back backend.Backend) {
entries, err := back.GetMyEntries()
if err != nil {
log.Fatal(err)
}

timers, err := back.GetTimers()
if err != nil {
log.Fatal(err)
}

currentTime := time.Now()
today := currentTime.Format("2006-01-02")

total_minutes := 0

for _, entry := range entries {
if entry.Date == today {

outputWorkdayEntry(back, &entry)
total_minutes += entry.Minutes
}
}

for _, timer := range timers {
outputWorkdayTimer(back, &timer)
total_minutes += timer.Seconds / 60
}

outputWorkdayTotal(back, total_minutes)
}

var workdayFormatString = "%-50s %2dh%02d %s\n"

func outputWorkdayEntry(m backend.Backend, entry *noko.Entry) {
minutes := entry.Minutes
hours := minutes / 60
minutes -= hours * 60
fmt.Fprintf(m, workdayFormatString, entry.Project.Name, hours, minutes, entry.Description)
}

func outputWorkdayTimer(m backend.Backend, timer *noko.Timer) {
minutes := timer.Seconds / 60
hours := minutes / 60
minutes -= hours * 60

fmt.Fprintf(m, workdayFormatString, timer.Project.Name, hours, minutes, timer.Description)
}

func outputWorkdayTotal(m backend.Backend, minutes int) {
hours := minutes / 60
minutes -= hours * 60

fmt.Fprintf(m, "\n")
fmt.Fprintf(m, workdayFormatString, "", hours, minutes, "")
}

0 comments on commit 123da8c

Please sign in to comment.