tiki/util/points.go

62 lines
1.8 KiB
Go
Raw Normal View History

2026-01-17 16:08:53 +00:00
package util
2026-04-10 20:07:34 +00:00
import (
"strings"
"github.com/boolean-maybe/tiki/config"
)
2026-03-04 00:32:38 +00:00
// GeneratePointsVisual formats points as a visual representation using a styled bar.
2026-01-17 16:08:53 +00:00
// Points are scaled to a 0-10 display range based on maxPoints configuration.
//
// Parameters:
// - points: The task's point value
// - maxPoints: The configured maximum points value (for scaling)
2026-04-10 20:07:34 +00:00
// - filledColor: Color for filled segments
// - unfilledColor: Color for unfilled segments
2026-03-04 00:32:38 +00:00
//
// Returns: A string with colored filled (❚) and unfilled (❘) segments representing the points value.
2026-01-17 16:08:53 +00:00
//
2026-03-04 00:32:38 +00:00
// Uses tview color tag format for proper rendering in the TUI.
2026-01-17 16:08:53 +00:00
//
// Example:
//
2026-04-10 20:07:34 +00:00
// GeneratePointsVisual(7, 10, filledColor, unfilledColor) returns a bar with 7 blue segments and 3 gray segments
func GeneratePointsVisual(points int, maxPoints int, filledColor config.Color, unfilledColor config.Color) string {
2026-03-04 00:32:38 +00:00
const displaySegments = 10
const filledChar = "❚"
const unfilledChar = "❘"
const resetColor = "[-]" // Reset to default in tview format
2026-01-17 16:08:53 +00:00
// Scale points to 0-10 range based on configured max
2026-03-04 00:32:38 +00:00
// Formula: displayPoints = (points * displaySegments) / maxPoints
displayPoints := (points * displaySegments) / maxPoints
2026-01-17 16:08:53 +00:00
// Clamp to 0-10 range
2026-03-04 00:32:38 +00:00
displayPoints = max(0, min(displayPoints, displaySegments))
2026-01-17 16:08:53 +00:00
2026-03-04 00:32:38 +00:00
var result strings.Builder
result.Grow(50) // Pre-allocate for color tags + characters
// Add filled segments
if displayPoints > 0 {
2026-04-10 20:07:34 +00:00
result.WriteString(filledColor.Tag().String())
2026-03-04 00:32:38 +00:00
for i := 0; i < displayPoints; i++ {
result.WriteString(filledChar)
}
2026-01-17 16:08:53 +00:00
}
2026-03-04 00:32:38 +00:00
// Add unfilled segments
if displayPoints < displaySegments {
2026-04-10 20:07:34 +00:00
result.WriteString(unfilledColor.Tag().String())
2026-03-04 00:32:38 +00:00
for i := displayPoints; i < displaySegments; i++ {
result.WriteString(unfilledChar)
}
2026-01-17 16:08:53 +00:00
}
2026-03-04 00:32:38 +00:00
// Reset color at the end
result.WriteString(resetColor)
return result.String()
2026-01-17 16:08:53 +00:00
}