From 89a4d01f21db4fbdc29ef63db21282aeb9537391 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 28 Jan 2023 02:23:25 -0800 Subject: [PATCH] make term return strings instead of writing directly --- cmd/cli/cli.go | 24 +++++++++++++++--------- gitgraph/term/term.go | 15 ++++++++------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 2fcbaf9..77e0bcf 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -5,6 +5,7 @@ import ( "math/rand" "time" + "github.com/taigrr/gico/commits" "github.com/taigrr/gico/gitgraph/term" ) @@ -13,19 +14,24 @@ func init() { } func main() { - freq := []int{} - for i := 0; i < 7; i++ { - freq = append(freq, rand.Int()) + wfreq := []int{} + yd := time.Now().YearDay() + freq, _ := commits.GlobalFrequency(time.Now().Year(), []string{""}) + if yd < 7 { + // xfreq, _ := commits.GlobalFrequency(time.Now().Year()-1, []string{""}) + } else { + // TODO fix bug for negative in first week of Jan + for i := 0; i < 7; i++ { + d := time.Now().YearDay() - 1 - 6 + i + wfreq = append(wfreq, freq[d]) + } } fmt.Println("week:") - term.GetWeekUnicode(freq) + fmt.Println(term.GetWeekUnicode(wfreq)) fmt.Println() fmt.Println() fmt.Println() - freq = []int{} - for i := 0; i < 365; i++ { - freq = append(freq, rand.Int()) - } + freq, _ = commits.GlobalFrequency(time.Now().Year(), []string{""}) fmt.Println("year:") - term.GetYearUnicode(freq) + fmt.Println(term.GetYearUnicode(freq)) } diff --git a/gitgraph/term/term.go b/gitgraph/term/term.go index 3e91a7b..4062f2e 100644 --- a/gitgraph/term/term.go +++ b/gitgraph/term/term.go @@ -1,7 +1,6 @@ package term import ( - "fmt" "os" "strings" "sync" @@ -17,28 +16,30 @@ var ( colorScheme []sc.SimpleColor ) -func GetWeekUnicode(frequencies []int) { +func GetWeekUnicode(frequencies []int) string { squareColors := []sc.SimpleColor{} min, max := common.MinMax(frequencies) for _, f := range frequencies { squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) } - drawWeekUnicode(squareColors) + return drawWeekUnicode(squareColors) } -func drawWeekUnicode(c []sc.SimpleColor) { +func drawWeekUnicode(c []sc.SimpleColor) string { // o := termenv.NewOutput(os.Stdout) + s := strings.Builder{} o := termenv.NewOutputWithProfile(os.Stdout, termenv.TrueColor) for w, color := range c { style := o.String(block).Foreground(termenv.TrueColor.Color(color.ToHex())) - fmt.Print(style.String()) + s.WriteString(style.String()) // termenv.SetForegroundColor(termenv.ForegroundColor()) if w == len(c)-1 { - fmt.Println() + s.WriteString("\n") } else { - fmt.Print(" ") + s.WriteString(" ") } } + return s.String() } func GetYearUnicode(frequencies []int) string {