|
| 1 | +// Package design provides reusable UI design components. |
| 2 | +package design |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/charmbracelet/lipgloss" |
| 9 | + "github.com/lucasb-eyer/go-colorful" |
| 10 | +) |
| 11 | + |
| 12 | +const diag = `╱` |
| 13 | + |
| 14 | +// RenderHeader renders a header with gradient text followed by diagonal fill. |
| 15 | +// text is the header text to display. |
| 16 | +// width is the total width to fill. |
| 17 | +// Returns a styled header string with gradient text and diagonal lines. |
| 18 | +func RenderHeader(text string, width int) string { |
| 19 | + return renderHeaderWithFocus(text, width, true) |
| 20 | +} |
| 21 | + |
| 22 | +// RenderHeaderDim renders a dimmed header for unfocused state. |
| 23 | +// text is the header text to display. |
| 24 | +// width is the total width to fill. |
| 25 | +func RenderHeaderDim(text string, width int) string { |
| 26 | + return renderHeaderWithFocus(text, width, false) |
| 27 | +} |
| 28 | + |
| 29 | +// renderHeaderWithFocus renders header with gradient or dim styling based on focus. |
| 30 | +func renderHeaderWithFocus(text string, width int, focused bool) string { |
| 31 | + startHex, endHex := AdaptiveGradientColors() |
| 32 | + |
| 33 | + var title string |
| 34 | + var diagColor string |
| 35 | + |
| 36 | + if focused { |
| 37 | + title = applyHeaderGradient(text, startHex, endHex) |
| 38 | + diagColor = startHex |
| 39 | + } else { |
| 40 | + // Dim style for unfocused |
| 41 | + dimStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#666666", Dark: "#555555"}).Bold(true) |
| 42 | + title = dimStyle.Render(text) |
| 43 | + diagColor = "#555555" |
| 44 | + } |
| 45 | + |
| 46 | + remainingWidth := width - lipgloss.Width(text) - 2 |
| 47 | + if remainingWidth > 0 { |
| 48 | + lines := strings.Repeat(diag, remainingWidth) |
| 49 | + styledLines := lipgloss.NewStyle().Foreground(lipgloss.Color(diagColor)).Render(lines) |
| 50 | + title = fmt.Sprintf("%s %s", title, styledLines) |
| 51 | + } |
| 52 | + return title |
| 53 | +} |
| 54 | + |
| 55 | +// RenderHeaderCentered renders a header with diagonal fills on both sides. |
| 56 | +// text is the header text to display centered. |
| 57 | +// width is the total width to fill. |
| 58 | +func RenderHeaderCentered(text string, width int) string { |
| 59 | + startHex, endHex := AdaptiveGradientColors() |
| 60 | + title := applyHeaderGradient(text, startHex, endHex) |
| 61 | + |
| 62 | + textWidth := lipgloss.Width(text) |
| 63 | + remainingWidth := width - textWidth - 2 // 2 for spaces around text |
| 64 | + if remainingWidth <= 0 { |
| 65 | + return title |
| 66 | + } |
| 67 | + |
| 68 | + leftWidth := remainingWidth / 2 |
| 69 | + rightWidth := remainingWidth - leftWidth |
| 70 | + |
| 71 | + leftLines := strings.Repeat(diag, leftWidth) |
| 72 | + rightLines := strings.Repeat(diag, rightWidth) |
| 73 | + |
| 74 | + styledLeft := lipgloss.NewStyle().Foreground(lipgloss.Color(startHex)).Render(leftLines) |
| 75 | + styledRight := lipgloss.NewStyle().Foreground(lipgloss.Color(endHex)).Render(rightLines) |
| 76 | + |
| 77 | + return fmt.Sprintf("%s %s %s", styledLeft, title, styledRight) |
| 78 | +} |
| 79 | + |
| 80 | +// applyHeaderGradient applies a gradient to a single line of text. |
| 81 | +func applyHeaderGradient(text string, startHex, endHex string) string { |
| 82 | + startColor, err1 := colorful.Hex(startHex) |
| 83 | + endColor, err2 := colorful.Hex(endHex) |
| 84 | + if err1 != nil || err2 != nil { |
| 85 | + return text |
| 86 | + } |
| 87 | + |
| 88 | + runes := []rune(text) |
| 89 | + if len(runes) == 0 { |
| 90 | + return text |
| 91 | + } |
| 92 | + |
| 93 | + var result strings.Builder |
| 94 | + for i, char := range runes { |
| 95 | + if char == ' ' { |
| 96 | + result.WriteRune(' ') |
| 97 | + continue |
| 98 | + } |
| 99 | + ratio := float64(i) / float64(max(len(runes)-1, 1)) |
| 100 | + color := startColor.BlendLab(endColor, ratio) |
| 101 | + hexColor := color.Hex() |
| 102 | + charStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(hexColor)).Bold(true) |
| 103 | + result.WriteString(charStyle.Render(string(char))) |
| 104 | + } |
| 105 | + |
| 106 | + return result.String() |
| 107 | +} |
0 commit comments