151 lines
4.0 KiB
Go
151 lines
4.0 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"
|
|
)
|
|
|
|
// ANSI colors refer to the terminal's configured palette, not fixed RGB values.
|
|
// Unset backgrounds/foregrounds retain terminal defaults for light/dark themes.
|
|
var (
|
|
themeAccent = lipgloss.Color("6")
|
|
themePrimary = lipgloss.Color("4")
|
|
themeMuted = lipgloss.Color("8")
|
|
themeSuccess = lipgloss.Color("2")
|
|
themeError = lipgloss.Color("1")
|
|
)
|
|
|
|
// 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.Foreground(lipgloss.Color("0")).Background(color)
|
|
}
|
|
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.Reverse(true).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(themeAccent).Bold(true)
|
|
if selected {
|
|
s = s.Foreground(themePrimary).Reverse(true)
|
|
}
|
|
return s.Render(" " + label + " ")
|
|
}
|
|
|
|
func menuOption(label string, selected bool) string {
|
|
s := lipgloss.NewStyle()
|
|
if selected {
|
|
s = s.Foreground(themePrimary).Bold(true).Reverse(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
|
|
}
|