From 2d8b17d1f4a3692fd09da167bb06ece5391168e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Fri, 16 Oct 2020 18:11:33 +0100 Subject: [PATCH] Further TUI testing & improvements --- z/calendar.go | 81 ++++++++++++++++++++++++++++++++++++++------------- z/helpers.go | 8 +++-- z/stats.go | 38 +++++++++++++++++++----- 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/z/calendar.go b/z/calendar.go index 4e2acaf..4b7dfcd 100644 --- a/z/calendar.go +++ b/z/calendar.go @@ -3,33 +3,40 @@ package z import ( "fmt" "time" + "github.com/gookit/color" "github.com/shopspring/decimal" ) +type Statistic struct { + Hours decimal.Decimal + Project string + Color (func(...interface {}) string) +} + type Calendar struct { } -func GetOutputBoxForNumber(number int) (string) { +func GetOutputBoxForNumber(number int, clr (func(...interface {}) string) ) (string) { switch(number) { - case 0: return " " - case 1: return " ▄" - case 2: return "▄▄" - case 3: return "▄█" - case 4: return "██" + case 0: return clr(" ") + case 1: return clr(" ▄") + case 2: return clr("▄▄") + case 3: return clr("▄█") + case 4: return clr("██") } - return " " + return clr(" ") } -func GetOutputBarForHours(hours decimal.Decimal) ([]string) { +func GetOutputBarForHours(hours decimal.Decimal, stats []Statistic) ([]string) { var bar = []string{ - "····", - "····", - "····", - "····", - "····", - "····", + color.FgGray.Render("····"), + color.FgGray.Render("····"), + color.FgGray.Render("····"), + color.FgGray.Render("····"), + color.FgGray.Render("····"), + color.FgGray.Render("····"), } hoursInt := int((hours.Round(0)).IntPart()) @@ -39,12 +46,41 @@ func GetOutputBarForHours(hours decimal.Decimal) ([]string) { divisible := hoursInt - restInt fullparts := divisible / 4 + 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 for i := (len(bar) - 1); i > (len(bar) - 1 - fullparts); i-- { - bar[i] = " " + GetOutputBoxForNumber(4) + " " + if iColor < colorsFullIdx { + bar[i] = " " + GetOutputBoxForNumber(4, colorsFull[iColor]) + " " + iColor++ + } else { + bar[i] = " " + GetOutputBoxForNumber(4, color.FgWhite.Render) + " " + } } if(restInt > 0) { - bar[(len(bar) - 1 - fullparts)] = " " + GetOutputBoxForNumber(restInt) + " " + bar[(len(bar) - 1 - fullparts)] = " " + GetOutputBoxForNumber(restInt, colorFraction) + " " } return bar @@ -55,16 +91,21 @@ func (calendar *Calendar) GetCalendarWeek(timestamp time.Time) (int) { return cw } -func (calendar *Calendar) GetOutputForWeekCalendar(cw int, data map[string]decimal.Decimal) (string) { +func (calendar *Calendar) GetOutputForWeekCalendar(cw int, data map[string][]Statistic) (string) { 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 { - hours := data[day] - totalHours = totalHours.Add(hours) - bar := GetOutputBarForHours(hours) + 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]) bars = append(bars, bar) } diff --git a/z/helpers.go b/z/helpers.go index 7198bfd..7e86313 100644 --- a/z/helpers.go +++ b/z/helpers.go @@ -105,12 +105,14 @@ func OutputAppendRight(leftStr string, rightStr string, pad int) (string) { var rpos int = 0 left := []rune(leftStr) + leftLen := len(left) right := []rune(rightStr) + rightLen := len(right) - for lpos := 0; lpos < len(left); lpos++ { - if left[lpos] == '\n' || lpos == (len(left) - 1) { + for lpos := 0; lpos < leftLen; lpos++ { + if left[lpos] == '\n' || lpos == (leftLen - 1) { output = fmt.Sprintf("%s%*s", output, pad, "") - for rpos = rpos; rpos < len(right); rpos++ { + for rpos = rpos; rpos < rightLen; rpos++ { output = fmt.Sprintf("%s%c", output, right[rpos]) if right[rpos] == '\n' { rpos++ diff --git a/z/stats.go b/z/stats.go index 5c347e6..695cda2 100644 --- a/z/stats.go +++ b/z/stats.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" "github.com/shopspring/decimal" + "github.com/gookit/color" ) var statsCmd = &cobra.Command{ @@ -26,15 +27,36 @@ var statsCmd = &cobra.Command{ var cal Calendar - var data = make(map[string]decimal.Decimal) + var data = make(map[string][]Statistic) - data["Mo"], _ = decimal.NewFromString("15.00") - data["Tu"], _ = decimal.NewFromString("4.0") - data["We"], _ = decimal.NewFromString("10.0") - data["Th"], _ = decimal.NewFromString("1.0") - data["Fr"], _ = decimal.NewFromString("0.0") - data["Sa"], _ = decimal.NewFromString("18.2") - data["Su"], _ = decimal.NewFromString("1.0") + data["Mo"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(12.0), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(3.5), Project: "blog", Color: color.FgGreen.Render }, + } + data["Tu"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(2.25), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(4.0), Project: "blog", Color: color.FgGreen.Render }, + } + data["We"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(10.0), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(1.5), Project: "blog", Color: color.FgGreen.Render }, + } + data["Th"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(4.0), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(4.5), Project: "blog", Color: color.FgGreen.Render }, + } + data["Fr"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(0.5), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(3.5), Project: "blog", Color: color.FgGreen.Render }, + } + data["Sa"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(1.0), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(1.0), Project: "blog", Color: color.FgGreen.Render }, + } + data["Su"] = []Statistic { + Statistic{ Hours: decimal.NewFromFloat(10.0), Project: "zeit", Color: color.FgRed.Render }, + Statistic{ Hours: decimal.NewFromFloat(0.5), Project: "blog", Color: color.FgGreen.Render }, + } out := cal.GetOutputForWeekCalendar(1, data) out2 := cal.GetOutputForWeekCalendar(2, data)