From 79dfe4ed4a68132fe3a92f11971707d82d6a958f 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 16:36:44 +0100 Subject: [PATCH] Testing TUI implementation --- z/calendar.go | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++ z/stats.go | 69 ++++++++++++++++++++++++++++ z/tui.go | 67 +++++++++++++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 z/calendar.go create mode 100644 z/stats.go create mode 100644 z/tui.go diff --git a/z/calendar.go b/z/calendar.go new file mode 100644 index 0000000..242c8ea --- /dev/null +++ b/z/calendar.go @@ -0,0 +1,125 @@ +package z + +import ( + "fmt" + "time" + "github.com/shopspring/decimal" +) + +type Calendar struct { + +} + +func GetOutputBoxForNumber(number int) (string) { + switch(number) { + case 0: return " " + case 1: return " ▄" + case 2: return "▄▄" + case 3: return "▄█" + case 4: return "██" + } + + return " " +} + +func GetOutputBarForHours(hours decimal.Decimal) ([]string) { + var bar = []string{ + "····", + "····", + "····", + "····", + "····", + "····", + } + + 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 + + for i := (len(bar) - 1); i > (len(bar) - 1 - fullparts); i-- { + bar[i] = " " + GetOutputBoxForNumber(4) + " " + } + + if(restInt > 0) { + bar[(len(bar) - 1 - fullparts)] = " " + GetOutputBoxForNumber(restInt) + " " + } + + return bar +} + +func (calendar *Calendar) GetCalendarWeek(timestamp time.Time) (int) { + var _, cw = timestamp.ISOWeek() + return cw +} + +// func (calendar *Calendar) GetBufferForWeekCalendar(cw int, data map[string]decimal.Decimal) ([][]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) +// 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) +// } +// output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s", +// output, days[0], days[1], days[2], days[3], days[4], days[5], days[6]) + +// return output +// } + +func (calendar *Calendar) GetTuiBufferForWeekCalendar(cw int, data map[string]decimal.Decimal) (TuiBuffer) { + var output string = "" + buffer := TuiBuffer{} + 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) + 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) + } + output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s", + output, days[0], days[1], days[2], days[3], days[4], days[5], days[6]) + + fmt.Printf("%s\n", output) + + row := 0 + col := 0 + for _, chr := range output { + if(chr == '\n') { + row++ + col = 0 + continue + } + + buffer[row][col] = chr + col++ + } + return buffer +} diff --git a/z/stats.go b/z/stats.go new file mode 100644 index 0000000..27af697 --- /dev/null +++ b/z/stats.go @@ -0,0 +1,69 @@ +package z + +import ( + "os" + "fmt" + "github.com/spf13/cobra" + "github.com/shopspring/decimal" + "github.com/gookit/color" +) + +var statsCmd = &cobra.Command{ + Use: "stats", + Short: "Display activity statistics", + Long: "Display statistics on all tracked activities.", + Run: func(cmd *cobra.Command, args []string) { + // user := GetCurrentUser() + + // entries, err := database.ListEntries(user) + // if err != nil { + // fmt.Printf("%s %+v\n", CharError, err) + // os.Exit(1) + // } + + // for _, entry := range entries { + // fmt.Printf("%s\n", entry.GetOutput()) + // } + + var cal Calendar + + var data = make(map[string]decimal.Decimal) + + 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") + + buff := cal.GetTuiBufferForWeekCalendar(1, data) + buff2 := cal.GetTuiBufferForWeekCalendar(2, data) + + r := []rune(color.FgLightWhite.Render("A")) + for _, bla := range r { + fmt.Printf("Char: %c", bla) + } + + tui := Tui{} + // tui.Init() + fmt.Printf("%s\n", tui.Render(100, 10)) + tui.Merge(buff, 0, 0) + tui.Merge(buff2, 0, 48) + fmt.Printf("---\n") + fmt.Printf("%s\n", tui.Render(100, 10)) + + return + }, +} + +func init() { + rootCmd.AddCommand(statsCmd) + + var err error + database, err = InitDatabase() + if err != nil { + fmt.Printf("%s %+v\n", CharError, err) + os.Exit(1) + } +} diff --git a/z/tui.go b/z/tui.go new file mode 100644 index 0000000..157dbf1 --- /dev/null +++ b/z/tui.go @@ -0,0 +1,67 @@ +package z + +import ( + "fmt" +) + +const( + TUI_ROWS = 200 + TUI_COLS = 200 +) + +type TuiBuffer [TUI_ROWS][TUI_COLS]rune + +type Tui struct { + Buffer TuiBuffer +} + +func (tui *Tui) Init() { + for row := 0; row < TUI_ROWS; row++ { + for col := 0; col < TUI_COLS; col++ { + tui.Buffer[row][col] = 0 + } + } +} + +func (tui *Tui) Merge(buffer TuiBuffer, x int, y int) (bool) { + inputRow := 0 + inputCol := 0 + + for row := x; row < TUI_ROWS; row++ { + for col := y; col < TUI_COLS; col++ { + if buffer[inputRow][inputCol] != 0 { + tui.Buffer[row][col] = buffer[inputRow][inputCol] + } + inputCol++ + } + inputRow++ + inputCol = 0 + } + + return true +} + +func (tui *Tui) Render(cols int, rows int) (string) { + var output string = "" + var emptyRow bool = true + + for row := 0; row < rows; row++ { + for col := 0; col < cols; col++ { + var chr rune = ' ' + + if tui.Buffer[row][col] != 0 { + chr = tui.Buffer[row][col] + emptyRow = false + } else { + chr = ' ' + } + output = fmt.Sprintf("%s%c", output, chr) + } + if emptyRow == false { + output = fmt.Sprintf("%s\n", output) + emptyRow = true + } + } + + return output +}