zeit/z/calendar.go

125 lines
3.3 KiB
Go
Raw Normal View History

2020-10-16 15:36:44 +00:00
package z
import (
"fmt"
"time"
2020-10-16 17:11:33 +00:00
"github.com/gookit/color"
2020-10-16 15:36:44 +00:00
"github.com/shopspring/decimal"
)
2020-10-16 17:11:33 +00:00
type Statistic struct {
Hours decimal.Decimal
Project string
Color (func(...interface {}) string)
}
2020-10-16 15:36:44 +00:00
type Calendar struct {
}
2020-10-16 17:11:33 +00:00
func GetOutputBoxForNumber(number int, clr (func(...interface {}) string) ) (string) {
2020-10-16 15:36:44 +00:00
switch(number) {
2020-10-16 17:11:33 +00:00
case 0: return clr(" ")
case 1: return clr(" ▄")
case 2: return clr("▄▄")
case 3: return clr("▄█")
case 4: return clr("██")
2020-10-16 15:36:44 +00:00
}
2020-10-16 17:11:33 +00:00
return clr(" ")
2020-10-16 15:36:44 +00:00
}
2020-10-16 17:11:33 +00:00
func GetOutputBarForHours(hours decimal.Decimal, stats []Statistic) ([]string) {
2020-10-16 15:36:44 +00:00
var bar = []string{
2020-10-16 17:11:33 +00:00
color.FgGray.Render("····"),
color.FgGray.Render("····"),
color.FgGray.Render("····"),
color.FgGray.Render("····"),
color.FgGray.Render("····"),
color.FgGray.Render("····"),
2020-10-16 15:36:44 +00:00
}
hoursInt := int((hours.Round(0)).IntPart())
rest := ((hours.Round(0)).Mod(decimal.NewFromInt(4))).Round(0)
restInt := int(rest.IntPart())
divisible := hoursInt - restInt
fullparts := divisible / 4
2020-10-16 17:11:33 +00:00
colorsFull := make(map[int](func(...interface {}) string))
colorsFullIdx := 0
colorFraction := color.FgWhite.Render
colorFractionPrevAmount := 0.0
for _, stat := range stats {
statHoursInt := int((stat.Hours.Round(0)).IntPart())
statRest := (stat.Hours.Round(0)).Mod(decimal.NewFromInt(4))
statRestFloat, _ := statRest.Float64()
if statRestFloat > colorFractionPrevAmount {
colorFractionPrevAmount = statRestFloat
colorFraction = stat.Color
}
fullColoredParts := int(statHoursInt / 4)
for i := 0; i < fullColoredParts; i++ {
colorsFull[colorsFullIdx] = stat.Color
colorsFullIdx++
}
}
iColor := 0
2020-10-16 15:36:44 +00:00
for i := (len(bar) - 1); i > (len(bar) - 1 - fullparts); i-- {
2020-10-16 17:11:33 +00:00
if iColor < colorsFullIdx {
bar[i] = " " + GetOutputBoxForNumber(4, colorsFull[iColor]) + " "
iColor++
} else {
bar[i] = " " + GetOutputBoxForNumber(4, color.FgWhite.Render) + " "
}
2020-10-16 15:36:44 +00:00
}
if(restInt > 0) {
2020-10-16 17:11:33 +00:00
bar[(len(bar) - 1 - fullparts)] = " " + GetOutputBoxForNumber(restInt, colorFraction) + " "
2020-10-16 15:36:44 +00:00
}
return bar
}
func (calendar *Calendar) GetCalendarWeek(timestamp time.Time) (int) {
var _, cw = timestamp.ISOWeek()
return cw
}
2020-10-16 17:11:33 +00:00
func (calendar *Calendar) GetOutputForWeekCalendar(cw int, data map[string][]Statistic) (string) {
2020-10-16 15:36:44 +00:00
var output string = ""
var bars [][]string
var totalHours = decimal.NewFromInt(0)
var days = []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}
for _, day := range days {
2020-10-16 17:11:33 +00:00
var dayHours = decimal.NewFromInt(0)
for _, stat := range data[day] {
dayHours = dayHours.Add(stat.Hours)
totalHours = totalHours.Add(stat.Hours)
}
bar := GetOutputBarForHours(dayHours, data[day])
2020-10-16 15:36:44 +00:00
bars = append(bars, bar)
}
output = fmt.Sprintf("CW %02d %s H\n", cw, totalHours.StringFixed(2))
for row := 0; row < len(bars[0]); row++ {
output = fmt.Sprintf("%s%2d │", output, ((6 - row) * 4))
for col := 0; col < len(bars); col++ {
output = fmt.Sprintf("%s%s", output, bars[col][row])
}
output = fmt.Sprintf("%s\n", output)
}
2020-10-16 16:02:41 +00:00
output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s\n",
2020-10-16 15:36:44 +00:00
output, days[0], days[1], days[2], days[3], days[4], days[5], days[6])
2020-10-16 16:02:41 +00:00
return output
2020-10-16 15:36:44 +00:00
}