This guide explains how to implement color-coded status indicators in a Go TUI application using bubbletea and lipgloss.
You will need the following libraries:
go get github.com/charmbracelet/bubbletea
go get github.com/charmbracelet/lipglossIn a terminal, we cannot use SVG or PNG icons. Instead, we use Unicode symbols and style them using ANSI colors.
For the Airflow look, we use the Small Square symbol:
- Filled Square:
β(UnicodeU+25A0) - Empty Square:
β‘(UnicodeU+25A1)
Create a mapping between your logical state (e.g., Success) and a lipgloss.Style that defines the color.
type State string
const (
Success State = "success"
Failed State = "failed"
)
// Define the styles
var stateStyles = map[State]lipgloss.Style{
Success: lipgloss.NewStyle().Foreground(lipgloss.Color("2")), // Green
Failed: lipgloss.NewStyle().Foreground(lipgloss.Color("1")), // Red
}Combine the symbol and the text label in a map.
var stateIcons = map[State]string{
Success: "β success",
Failed: "β failed",
}When rendering your view, look up the style and the icon text, then use the .Render() method.
func renderState(state State) string {
style := stateStyles[state]
text := stateIcons[state]
return style.Render(text)
}To match the Airflow UI exactly, use these Xterm-256 color codes:
| State | Symbol | Color Code | Lip Gloss Color |
|---|---|---|---|
| Success | β |
Green | lipgloss.Color("2") |
| Running | β |
Light Green | lipgloss.Color("10") |
| Failed | β |
Red | lipgloss.Color("1") |
| Queued | β |
Gray | lipgloss.Color("8") |
| Up for Retry | β |
Yellow | lipgloss.Color("3") |
| Upstream Failed | β |
Orange | lipgloss.Color("208") |
| Skipped | β |
Pink | lipgloss.Color("211") |
| No Status | β‘ |
White | lipgloss.Color("255") |
- Consistency: Always use the same Unicode character for the blocks so they align perfectly in your table.
- Accessibility: Keep the text label (e.g., "success") next to the icon. This ensures that people with color-blindness can still understand the status.
- Alignment: Use
lipgloss.JoinHorizontalor set a fixedWidth()on your columns to ensure the icons line up vertically.