From 5d14089290f3255e274b8faeb05d99976450e075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Zywert?= Date: Mon, 25 Mar 2024 07:36:15 +0100 Subject: [PATCH] add hour diff in summary --- gojira/summary.go | 31 +++++++++++++++++++++++++++++-- gojira/utils.go | 32 -------------------------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/gojira/summary.go b/gojira/summary.go index b4da2db..14cf41b 100644 --- a/gojira/summary.go +++ b/gojira/summary.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/rivo/tview" "strings" + "time" ) type Summary struct { @@ -16,7 +17,7 @@ func NewSummary() *Summary { app.ui.app.Draw() }), } - summary.SetText("Calendar ?h/?h") + summary.SetText("Loading...") summary.SetTextAlign(tview.AlignCenter) return summary } @@ -26,6 +27,32 @@ func (s *Summary) update() { // that's a hack to remove spaces between hours and minutes totalTimeSpent = strings.Join(strings.Fields(totalTimeSpent), "") workingHours := workingHoursInMonthToPresentDay(app.time.Year(), app.time.Month()) - s.SetText(fmt.Sprintf("Total %s/%dh", totalTimeSpent, workingHours)) + difference := workingHoursAbsoluteDiff(workingHours) + status := fmt.Sprintf("Total %s/%dh", totalTimeSpent, workingHours) + if difference != 0 { + status = fmt.Sprintf("Total %s/%dh (%s)", totalTimeSpent, workingHours, FormatTimeSpent(difference)) + } + s.SetText(status) s.SetTextColor(GetTimeSpentColor(app.workLogs.TotalTimeSpentToPresentDay(), workingHours)) } + +func workingHoursInMonthToPresentDay(year int, month time.Month) int { + t := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC) + totalWorkHours := 0 + + for t.Month() == month && t.Before(time.Now().Local()) { + if t.Weekday() != time.Saturday && t.Weekday() != time.Sunday { + totalWorkHours += 8 + } + t = t.AddDate(0, 0, 1) + } + return totalWorkHours +} + +func workingHoursAbsoluteDiff(workingHours int) int { + difference := workingHours*60*60 - app.workLogs.TotalTimeSpentToPresentDay() + if difference < 0 { + difference = -difference + } + return difference +} diff --git a/gojira/utils.go b/gojira/utils.go index 7a43db4..6aa980a 100644 --- a/gojira/utils.go +++ b/gojira/utils.go @@ -112,40 +112,8 @@ func OpenURL(url string) { } } -func WeekRange(today time.Time) (time.Time, time.Time) { - y, w := today.ISOWeek() - firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC) - for firstDay.Weekday() != time.Monday { - firstDay = firstDay.AddDate(0, 0, -1) - } - - for { - y1, w1 := firstDay.ISOWeek() - if y1 == y && w1 == w { - break - } - firstDay = firstDay.AddDate(0, 0, 1) - } - - lastDay := firstDay.AddDate(0, 0, 6) // Adding 6 days to get to Sunday - return firstDay.Truncate(24 * time.Hour), lastDay.Truncate(24 * time.Hour) -} - func MonthRange(t *time.Time) (time.Time, time.Time) { firstDayOfCurrentMonth := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()) firstDayOfNextMonth := firstDayOfCurrentMonth.AddDate(0, 1, 0) return firstDayOfCurrentMonth, firstDayOfNextMonth } - -func workingHoursInMonthToPresentDay(year int, month time.Month) int { - t := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC) - totalWorkHours := 0 - - for t.Month() == month && t.Before(time.Now().Local()) { - if t.Weekday() != time.Saturday && t.Weekday() != time.Sunday { - totalWorkHours += 8 - } - t = t.AddDate(0, 0, 1) - } - return totalWorkHours -}