Files
gico/graph/term/term.go
Tai Groot 8f8735d5a8 chore: modernize Go 1.26, update deps, fix lint issues
- Update Go version from 1.19 to 1.26
- Update all dependencies to latest versions
- Fix staticcheck warnings (unused vars, types, functions)
- Remove unused svgToPng function
- Add GitHub Actions CI workflow
2026-02-23 17:27:51 +00:00

112 lines
3.0 KiB
Go

package term
import (
"os"
"strings"
"github.com/muesli/termenv"
sc "github.com/taigrr/simplecolorpalettes/simplecolor"
"github.com/taigrr/gico/graph/common"
)
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()
}