Simplified TUI stuff

This commit is contained in:
マリウス 2020-10-16 17:02:41 +01:00
parent 79dfe4ed4a
commit 510f9f9648
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F
4 changed files with 32 additions and 127 deletions

View file

@ -55,36 +55,8 @@ func (calendar *Calendar) GetCalendarWeek(timestamp time.Time) (int) {
return cw return cw
} }
// func (calendar *Calendar) GetBufferForWeekCalendar(cw int, data map[string]decimal.Decimal) ([][]string) { func (calendar *Calendar) GetOutputForWeekCalendar(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 = "" var output string = ""
buffer := TuiBuffer{}
var bars [][]string var bars [][]string
var totalHours = decimal.NewFromInt(0) 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", 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]) output, days[0], days[1], days[2], days[3], days[4], days[5], days[6])
fmt.Printf("%s\n", output) return output
row := 0
col := 0
for _, chr := range output {
if(chr == '\n') {
row++
col = 0
continue
}
buffer[row][col] = chr
col++
}
return buffer
} }

View file

@ -1,6 +1,7 @@
package z package z
import ( import (
"fmt"
"os/user" "os/user"
"regexp" "regexp"
"strconv" "strconv"
@ -98,3 +99,28 @@ func ParseTime(timeStr string) (time.Time, error) {
return time.Now(), errors.New("No match") 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
}

View file

@ -5,7 +5,6 @@ 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{
@ -37,21 +36,10 @@ var statsCmd = &cobra.Command{
data["Sa"], _ = decimal.NewFromString("18.2") data["Sa"], _ = decimal.NewFromString("18.2")
data["Su"], _ = decimal.NewFromString("1.0") data["Su"], _ = decimal.NewFromString("1.0")
buff := cal.GetTuiBufferForWeekCalendar(1, data) out := cal.GetOutputForWeekCalendar(1, data)
buff2 := cal.GetTuiBufferForWeekCalendar(2, data) out2 := cal.GetOutputForWeekCalendar(2, data)
r := []rune(color.FgLightWhite.Render("A")) fmt.Printf("%s\n", OutputAppendRight(out, out2, 10))
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 return
}, },

View file

@ -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
}