Files

171 lines
4.9 KiB
Go

package tui
import (
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/lipgloss"
"sort"
"strings"
)
// Nested ANSI styles reset their backgrounds when they end. Restore the card's
// base style after those resets, so colored labels don't leave terminal-colored
// holes in an otherwise continuous surface. Plain/no-color output is unchanged.
func surfaceContent(text string, background lipgloss.TerminalColor) string {
paint := lipgloss.NewStyle().Foreground(themeText).Background(background).Render(" ")
open := paint[:strings.IndexByte(paint, ' ')]
if open == "" {
return text
}
for _, reset := range []string{"\x1b[0m", "\x1b[m"} {
text = strings.ReplaceAll(text, reset, reset+open)
}
return text
}
// Semantic accents use the terminal palette. Surfaces adapt to light/dark
// terminals and are automatically reduced on terminals with fewer colors.
var (
themeAccent = lipgloss.Color("6")
themePrimary = lipgloss.Color("4")
themeMuted = lipgloss.Color("8")
themeSuccess = lipgloss.Color("2")
themeError = lipgloss.Color("1")
themeSurface = lipgloss.AdaptiveColor{Light: "#F3F4F7", Dark: "#1D2129"}
themeElevated = lipgloss.AdaptiveColor{Light: "#EAEDF3", Dark: "#252B35"}
themeSelection = lipgloss.AdaptiveColor{Light: "#DCE7F5", Dark: "#2B3A4C"}
themeText = lipgloss.AdaptiveColor{Light: "#303744", Dark: "#D9DEE7"}
)
// Names keep their colors when stages are reordered or another version is opened.
func nameColor(name string, palette []lipgloss.Color) lipgloss.Color {
var hash uint32 = 2166136261
for _, b := range []byte(name) {
hash ^= uint32(b)
hash *= 16777619
}
return palette[int(hash%uint32(len(palette)))]
}
func stageColor(key string, graphs ...protocol.Graph) lipgloss.Color {
if key == protocol.SuccessEnd {
return themeSuccess
}
if key == protocol.FailureEnd {
return themeError
}
if key == "#" {
return themeMuted
}
// Red/green (1/2 and bright 9/10) are reserved for error/success outcomes.
palette := []lipgloss.Color{"4", "6", "3", "5", "7", "12", "14", "11", "13"}
if len(graphs) > 0 {
keys := []string{}
seen := map[string]bool{}
for _, stage := range graphs[0].Stages {
if !seen[stage.Key] {
keys = append(keys, stage.Key)
seen[stage.Key] = true
}
}
sort.Strings(keys)
if len(keys) <= 5 {
palette = palette[:5]
}
used := map[lipgloss.Color]bool{}
for _, name := range keys {
preferred := nameColor(name, palette)
index := 0
for palette[index] != preferred {
index++
}
for offset := 0; offset < len(palette); offset++ {
candidate := palette[(index+offset)%len(palette)]
if !used[candidate] {
preferred = candidate
break
}
}
used[preferred] = true
if name == key {
return preferred
}
}
}
return nameColor(key, palette)
}
func nodeLabel(key string) string {
if key == protocol.SuccessEnd {
return "SUCCESS"
}
if key == protocol.FailureEnd {
return "FAILURE"
}
return key
}
func outcomeColor(outcome string) lipgloss.Color {
if outcome == "error" {
return themeError
}
if outcome == "success" {
return themeSuccess
}
return nameColor(outcome, []lipgloss.Color{"3", "5", "6", "11", "13", "14"})
}
func stageTitle(key, label string, selected bool, graphs ...protocol.Graph) string {
color := stageColor(key, graphs...)
s := lipgloss.NewStyle().Foreground(color).Bold(true)
if selected {
s = s.Background(themeSelection)
}
return s.Render(label)
}
func stageTab(key, label string, selected bool, graphs ...protocol.Graph) string {
s := lipgloss.NewStyle().Foreground(stageColor(key, graphs...)).Bold(true)
if selected {
s = s.Background(themeSelection).Underline(true)
}
return s.Render(label)
}
func outcomeLabel(outcome, label string) string {
return lipgloss.NewStyle().Foreground(outcomeColor(outcome)).Bold(true).Render(label)
}
func action(label string, selected bool) string {
s := lipgloss.NewStyle().Foreground(themeMuted)
if selected {
s = s.Foreground(themeAccent).Background(themeSelection).Bold(true)
}
return s.Render(" " + label + " ")
}
func menuOption(label string, selected bool) string {
s := lipgloss.NewStyle()
if selected {
s = s.Foreground(themeAccent).Background(themeSelection).Bold(true)
}
return s.Render(label)
}
func newHelp() help.Model {
h := help.New()
key := lipgloss.NewStyle().Foreground(themeAccent).Bold(true)
muted := lipgloss.NewStyle().Foreground(themeMuted)
h.Styles = help.Styles{Ellipsis: muted, ShortKey: key, ShortDesc: muted, ShortSeparator: muted, FullKey: key, FullDesc: muted, FullSeparator: muted}
return h
}
func newInput() textinput.Model {
field := textinput.New()
field.PromptStyle = lipgloss.NewStyle().Foreground(themeAccent)
field.PlaceholderStyle = lipgloss.NewStyle().Foreground(themeMuted)
field.Cursor.Style = lipgloss.NewStyle().Foreground(themeAccent)
return field
}