Replies: 2 comments
|
Yes! You have two simple ways to do this depending on whether you want to use Option 1: Use
|
0 replies
|
If you want dynamic colors based on stat levels (e.g., Red for low, Yellow for medium, Green for high) like Pokémon stat bars, you don't need a Bubble Tea sub-model at all. A simple Lipgloss helper function with block characters ( package main
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
func renderStatBar(current, max int, totalWidth int) string {
ratio := float64(current) / float64(max)
if ratio > 1.0 {
ratio = 1.0
}
if ratio < 0.0 {
ratio = 0.0
}
filledLen := int(ratio * float64(totalWidth))
emptyLen := totalWidth - filledLen
// Determine color based on threshold
var barColor string
switch {
case ratio <= 0.20:
barColor = "#e74c3c" // Red
case ratio <= 0.50:
barColor = "#f1c40f" // Yellow
default:
barColor = "#2ecc71" // Green
}
filledStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(barColor))
emptyStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
filled := filledStyle.Render(strings.Repeat("█", filledLen))
empty := emptyStyle.Render(strings.Repeat("░", emptyLen))
return filled + empty
}
type model struct{}
func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg, ok := msg.(tea.KeyMsg); ok && (msg.String() == "q" || msg.String() == "ctrl+c") {
return m, tea.Quit
}
return m, nil
}
func (m model) View() string {
hpBar := renderStatBar(15, 100, 20) // Red
atkBar := renderStatBar(45, 100, 20) // Yellow
defBar := renderStatBar(85, 100, 20) // Green
return fmt.Sprintf(
"HP: [%s] 15/100\nATK: [%s] 45/100\nDEF: [%s] 85/100\n\nPress q to quit",
hpBar, atkBar, defBar,
)
}
func main() {
p := tea.NewProgram(model{})
if _, err := p.Run(); err != nil {
panic(err)
}
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I see that there are some progress bar options but I’m looking for an option for a “bar” but one that doesn’t move.
For example, if you take a look at the stats of a Pokémon, it shows its stats with bars and on a scale of red, yellow, green.
Is this possible with BubbleTea?
I guess I could just set one number on the progress bar but I’d like to have to implement the
init(),update(), andview()since nothing is moving or updating…All reactions