From 510f9f9648f8e54dca5ee6781263352bb484d876 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 17:02:41 +0100 Subject: [PATCH] Simplified TUI stuff --- z/calendar.go | 48 +++--------------------------------- z/helpers.go | 26 ++++++++++++++++++++ z/stats.go | 18 +++----------- z/tui.go | 67 --------------------------------------------------- 4 files changed, 32 insertions(+), 127 deletions(-) delete mode 100644 z/tui.go diff --git a/z/calendar.go b/z/calendar.go index 242c8ea..4e2acaf 100644 --- a/z/calendar.go +++ b/z/calendar.go @@ -55,36 +55,8 @@ func (calendar *Calendar) GetCalendarWeek(timestamp time.Time) (int) { 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) { +func (calendar *Calendar) GetOutputForWeekCalendar(cw int, data map[string]decimal.Decimal) (string) { var output string = "" - buffer := TuiBuffer{} var bars [][]string var totalHours = decimal.NewFromInt(0) @@ -104,22 +76,8 @@ func (calendar *Calendar) GetTuiBufferForWeekCalendar(cw int, data map[string]de } output = fmt.Sprintf("%s\n", output) } - output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s", + output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s\n", 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 + return output } diff --git a/z/helpers.go b/z/helpers.go index 0d38cd4..7198bfd 100644 --- a/z/helpers.go +++ b/z/helpers.go @@ -1,6 +1,7 @@ package z import ( + "fmt" "os/user" "regexp" "strconv" @@ -98,3 +99,28 @@ func ParseTime(timeStr string) (time.Time, error) { return time.Now(), errors.New("No match") } } + +func OutputAppendRight(leftStr string, rightStr string, pad int) (string) { + var output string = "" + var rpos int = 0 + + left := []rune(leftStr) + right := []rune(rightStr) + + for lpos := 0; lpos < len(left); lpos++ { + if left[lpos] == '\n' || lpos == (len(left) - 1) { + output = fmt.Sprintf("%s%*s", output, pad, "") + for rpos = rpos; rpos < len(right); rpos++ { + output = fmt.Sprintf("%s%c", output, right[rpos]) + if right[rpos] == '\n' { + rpos++ + break + } + } + continue + } + output = fmt.Sprintf("%s%c", output, left[lpos]) + } + + return output +} diff --git a/z/stats.go b/z/stats.go index 27af697..5c347e6 100644 --- a/z/stats.go +++ b/z/stats.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/spf13/cobra" "github.com/shopspring/decimal" - "github.com/gookit/color" ) var statsCmd = &cobra.Command{ @@ -37,21 +36,10 @@ var statsCmd = &cobra.Command{ data["Sa"], _ = decimal.NewFromString("18.2") data["Su"], _ = decimal.NewFromString("1.0") - buff := cal.GetTuiBufferForWeekCalendar(1, data) - buff2 := cal.GetTuiBufferForWeekCalendar(2, data) + out := cal.GetOutputForWeekCalendar(1, data) + out2 := cal.GetOutputForWeekCalendar(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)) + fmt.Printf("%s\n", OutputAppendRight(out, out2, 10)) return }, diff --git a/z/tui.go b/z/tui.go deleted file mode 100644 index 157dbf1..0000000 --- a/z/tui.go +++ /dev/null @@ -1,67 +0,0 @@ -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 -}