Further TUI testing & improvements

This commit is contained in:
マリウス 2020-10-16 18:11:33 +01:00
parent 510f9f9648
commit 2d8b17d1f4
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F
3 changed files with 96 additions and 31 deletions

View file

@ -3,33 +3,40 @@ package z
import ( import (
"fmt" "fmt"
"time" "time"
"github.com/gookit/color"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
type Statistic struct {
Hours decimal.Decimal
Project string
Color (func(...interface {}) string)
}
type Calendar struct { type Calendar struct {
} }
func GetOutputBoxForNumber(number int) (string) { func GetOutputBoxForNumber(number int, clr (func(...interface {}) string) ) (string) {
switch(number) { switch(number) {
case 0: return " " case 0: return clr(" ")
case 1: return " ▄" case 1: return clr(" ▄")
case 2: return "▄▄" case 2: return clr("▄▄")
case 3: return "▄█" case 3: return clr("▄█")
case 4: return "██" case 4: return clr("██")
} }
return " " return clr(" ")
} }
func GetOutputBarForHours(hours decimal.Decimal) ([]string) { func GetOutputBarForHours(hours decimal.Decimal, stats []Statistic) ([]string) {
var bar = []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()) hoursInt := int((hours.Round(0)).IntPart())
@ -39,12 +46,41 @@ func GetOutputBarForHours(hours decimal.Decimal) ([]string) {
divisible := hoursInt - restInt divisible := hoursInt - restInt
fullparts := divisible / 4 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-- { 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) { if(restInt > 0) {
bar[(len(bar) - 1 - fullparts)] = " " + GetOutputBoxForNumber(restInt) + " " bar[(len(bar) - 1 - fullparts)] = " " + GetOutputBoxForNumber(restInt, colorFraction) + " "
} }
return bar return bar
@ -55,16 +91,21 @@ func (calendar *Calendar) GetCalendarWeek(timestamp time.Time) (int) {
return cw 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 output string = ""
var bars [][]string var bars [][]string
var totalHours = decimal.NewFromInt(0) var totalHours = decimal.NewFromInt(0)
var days = []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"} var days = []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}
for _, day := range days { for _, day := range days {
hours := data[day] var dayHours = decimal.NewFromInt(0)
totalHours = totalHours.Add(hours)
bar := GetOutputBarForHours(hours) for _, stat := range data[day] {
dayHours = dayHours.Add(stat.Hours)
totalHours = totalHours.Add(stat.Hours)
}
bar := GetOutputBarForHours(dayHours, data[day])
bars = append(bars, bar) bars = append(bars, bar)
} }

View file

@ -105,12 +105,14 @@ func OutputAppendRight(leftStr string, rightStr string, pad int) (string) {
var rpos int = 0 var rpos int = 0
left := []rune(leftStr) left := []rune(leftStr)
leftLen := len(left)
right := []rune(rightStr) right := []rune(rightStr)
rightLen := len(right)
for lpos := 0; lpos < len(left); lpos++ { for lpos := 0; lpos < leftLen; lpos++ {
if left[lpos] == '\n' || lpos == (len(left) - 1) { if left[lpos] == '\n' || lpos == (leftLen - 1) {
output = fmt.Sprintf("%s%*s", output, pad, "") 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]) output = fmt.Sprintf("%s%c", output, right[rpos])
if right[rpos] == '\n' { if right[rpos] == '\n' {
rpos++ rpos++

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"github.com/gookit/color"
) )
var statsCmd = &cobra.Command{ var statsCmd = &cobra.Command{
@ -26,15 +27,36 @@ var statsCmd = &cobra.Command{
var cal Calendar var cal Calendar
var data = make(map[string]decimal.Decimal) var data = make(map[string][]Statistic)
data["Mo"], _ = decimal.NewFromString("15.00") data["Mo"] = []Statistic {
data["Tu"], _ = decimal.NewFromString("4.0") Statistic{ Hours: decimal.NewFromFloat(12.0), Project: "zeit", Color: color.FgRed.Render },
data["We"], _ = decimal.NewFromString("10.0") Statistic{ Hours: decimal.NewFromFloat(3.5), Project: "blog", Color: color.FgGreen.Render },
data["Th"], _ = decimal.NewFromString("1.0") }
data["Fr"], _ = decimal.NewFromString("0.0") data["Tu"] = []Statistic {
data["Sa"], _ = decimal.NewFromString("18.2") Statistic{ Hours: decimal.NewFromFloat(2.25), Project: "zeit", Color: color.FgRed.Render },
data["Su"], _ = decimal.NewFromString("1.0") 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) out := cal.GetOutputForWeekCalendar(1, data)
out2 := cal.GetOutputForWeekCalendar(2, data) out2 := cal.GetOutputForWeekCalendar(2, data)