aboutsummaryrefslogtreecommitdiff
path: root/internal/game/ui_bar.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-06-21 03:46:25 -0400
committerhistoria <[not public]>2026-06-21 03:46:25 -0400
commit84072ffe6365ad57c4976e9272762aa6db41de7e (patch)
treed911389fd91e129871c23ec539e67a2666d9f78a /internal/game/ui_bar.go
parente4bc2de6e6aa507044a6a75b4c1954974539a857 (diff)
downloadthehouseoficarus-84072ffe6365ad57c4976e9272762aa6db41de7e.tar.gz
feat: better score output
Diffstat (limited to 'internal/game/ui_bar.go')
-rw-r--r--internal/game/ui_bar.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/internal/game/ui_bar.go b/internal/game/ui_bar.go
new file mode 100644
index 0000000..ca0b721
--- /dev/null
+++ b/internal/game/ui_bar.go
@@ -0,0 +1,104 @@
+package game
+
+import (
+ "math"
+ "strings"
+
+ "thehouseoficarus/internal/color"
+)
+
+type BarStyle struct {
+ FilledColor int
+ EmptyColor int
+ EmptyDim bool
+}
+
+func RenderBar(current, max, width int, unicode bool) string {
+ if max <= 0 {
+ max = 1
+ }
+ if current < 0 {
+ current = 0
+ }
+ if current > max {
+ current = max
+ }
+ filled := int(math.Round(float64(current) * float64(width) / float64(max)))
+ if filled > width {
+ filled = width
+ }
+ if filled < 0 {
+ filled = 0
+ }
+
+ fillRune, emptyRune := barRunes(unicode)
+ return strings.Repeat(fillRune, filled) + strings.Repeat(emptyRune, width-filled)
+}
+
+func RenderBarF(current, max float64, width int, unicode bool) string {
+ if max <= 0 {
+ max = 1
+ }
+ ratio := current / max
+ if ratio > 1 {
+ ratio = 1
+ }
+ if ratio < 0 {
+ ratio = 0
+ }
+ filled := int(math.Round(ratio * float64(width)))
+ if filled > width {
+ filled = width
+ }
+ if filled < 0 {
+ filled = 0
+ }
+
+ fillRune, emptyRune := barRunes(unicode)
+ return strings.Repeat(fillRune, filled) + strings.Repeat(emptyRune, width-filled)
+}
+
+func RenderColoredBar(current, max, width int, unicode bool, style *BarStyle, mode string) string {
+ if max <= 0 {
+ max = 1
+ }
+ if current < 0 {
+ current = 0
+ }
+ if current > max {
+ current = max
+ }
+ filled := int(math.Round(float64(current) * float64(width) / float64(max)))
+ if filled > width {
+ filled = width
+ }
+ if filled < 0 {
+ filled = 0
+ }
+
+ fillRune, emptyRune := barRunes(unicode)
+ fillStr := strings.Repeat(fillRune, filled)
+ emptyStr := strings.Repeat(emptyRune, width-filled)
+
+ if style != nil && mode != "none" && mode != "" {
+ if style.FilledColor >= 0 {
+ fillStr = color.Render(mode, color.ColorSpec{Fg: style.FilledColor}, fillStr)
+ }
+ if style.EmptyColor >= 0 {
+ spec := color.ColorSpec{Fg: style.EmptyColor}
+ if style.EmptyDim {
+ spec.Dim = true
+ }
+ emptyStr = color.Render(mode, spec, emptyStr)
+ }
+ }
+
+ return fillStr + emptyStr
+}
+
+func barRunes(unicode bool) (fill, empty string) {
+ if unicode {
+ return "█", "░"
+ }
+ return "#", "."
+}