Files
gico/graph/term/term.go

118 lines
3.0 KiB
Go

package term
import (
"os"
"strings"
"sync"
"github.com/muesli/termenv"
sc "github.com/taigrr/simplecolorpalettes/simplecolor"
"github.com/taigrr/gico/graph/common"
)
var (
colorsLoaded sync.Once
colorScheme []sc.SimpleColor
)
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))
}
return drawWeekUnicode(squareColors)
}
func drawWeekUnicode(c []sc.SimpleColor) string {
// o := termenv.NewOutput(os.Stdout)
s := strings.Builder{}
o := termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.TrueColor))
for w, color := range c {
style := o.String(block).Foreground(termenv.TrueColor.Color(color.ToHex()))
s.WriteString(style.String())
// termenv.SetForegroundColor(termenv.ForegroundColor())
if w == len(c)-1 {
s.WriteString("\n")
} else {
s.WriteString(" ")
}
}
return s.String()
}
func GetYearUnicodeSelected(frequencies []int, selected int) string {
squareColors := []sc.SimpleColor{}
min, max := common.MinMax(frequencies)
for _, f := range frequencies {
squareColors = append(squareColors, common.ColorForFrequency(f, min, max))
}
return drawYearUnicodeSelected(squareColors, selected)
}
func GetYearUnicode(frequencies []int) string {
squareColors := []sc.SimpleColor{}
min, max := common.MinMax(frequencies)
for _, f := range frequencies {
squareColors = append(squareColors, common.ColorForFrequency(f, min, max))
}
return drawYearUnicode(squareColors)
}
func drawYearUnicodeSelected(c []sc.SimpleColor, selected int) string {
// o := termenv.NewOutput(os.Stdout)
var s strings.Builder
o := termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.TrueColor))
weekRows := [7][]sc.SimpleColor{{}}
for i := 0; i < 7; i++ {
weekRows[i] = []sc.SimpleColor{}
}
for i := 0; i < len(c); i++ {
weekRows[i%7] = append(weekRows[i%7], c[i])
}
for i, row := range weekRows {
for w, d := range row {
style := o.String(block).Foreground(termenv.TrueColor.Color(d.ToHex()))
if w*7+i == selected {
style = o.String(block).Foreground(termenv.TrueColor.Color("#FFFFFF"))
// style = style.Background(termenv.TrueColor.Color("#FF0000"))
}
s.WriteString(style.String())
if w == len(row)-1 {
s.WriteString("\n")
} else {
s.WriteString(" ")
}
}
}
return s.String()
}
func drawYearUnicode(c []sc.SimpleColor) string {
// o := termenv.NewOutput(os.Stdout)
var s strings.Builder
o := termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.TrueColor))
weekRows := [7][]sc.SimpleColor{{}}
for i := 0; i < 7; i++ {
weekRows[i] = []sc.SimpleColor{}
}
for i := 0; i < len(c); i++ {
weekRows[i%7] = append(weekRows[i%7], c[i])
}
for _, row := range weekRows {
for w, d := range row {
style := o.String(block).Foreground(termenv.TrueColor.Color(d.ToHex()))
s.WriteString(style.String())
if w == len(row)-1 {
s.WriteString("\n")
} else {
s.WriteString(" ")
}
}
}
return s.String()
}