Debian Codegraph @

examples/charmtone/guide.go

raw
1 package main
2
3 import (
4 "fmt"
5 "image/color"
6 "io"
7 "iter"
8 "os"
9 "strings"
10
11 "github.com/charmbracelet/lipgloss/v2"
12 "github.com/charmbracelet/x/exp/charmtone"
13 )
14
15 func renderGuide() {
16 // Find the longest key name.
17 var widestKeyName int
18 for _, k := range charmtone.Keys() {
19 if w := lipgloss.Width(k.String()); w > widestKeyName {
20 widestKeyName = w
21 }
22 }
23
24 // Styles.
25 hasDarkBG := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
26 lightDark := lipgloss.LightDark(hasDarkBG)
27 logo := lipgloss.NewStyle().
28 Foreground(charmtone.Ash).
29 Background(charmtone.Charple).
30 Padding(0, 1).
31 SetString("Charm™")
32 title := lipgloss.NewStyle().
33 Foreground(lightDark(charmtone.Charcoal, charmtone.Smoke))
34 subdued := lipgloss.NewStyle().
35 Foreground(lightDark(charmtone.Squid, charmtone.Oyster))
36 fg := lipgloss.NewStyle().
37 MarginLeft(2).
38 Width(widestKeyName).
39 Align(lipgloss.Right)
40 bg := lipgloss.NewStyle().
41 Width(8)
42 hex := lipgloss.NewStyle().
43 Foreground(lightDark(charmtone.Smoke, charmtone.Charcoal))
44 legend := subdued.
45 BorderStyle(lipgloss.RoundedBorder()).
46 BorderForeground(charmtone.Charcoal).
47 Padding(0, 2).
48 MarginLeft(2)
49 primaryMark := lipgloss.NewStyle().
50 Foreground(lightDark(charmtone.Squid, charmtone.Smoke)).
51 SetString(blackCircle)
52 secondaryMark := primaryMark.
53 Foreground(lightDark(charmtone.Squid, charmtone.Oyster)).
54 SetString(blackCircle)
55 tertiaryMark := primaryMark.
56 Foreground(lightDark(charmtone.Squid, charmtone.Oyster)).
57 SetString(whiteCircle)
58 rightArrowMark := lipgloss.NewStyle().
59 Foreground(lightDark(charmtone.Squid, charmtone.Oyster)).
60 Margin(0, 1).
61 SetString(rightArrow)
62
63 var b strings.Builder
64
65 // Render title and description.
66 fmt.Fprintf(
67 &b,
68 "\n %s %s %s\n\n",
69 logo.String(),
70 title.Render("CharmTone"),
71 subdued.Render("• Formula Guide"),
72 )
73
74 // Render a swatch and its metadata.
75 renderSwatch := func(w io.Writer, k charmtone.Key) {
76 mark := " "
77 switch {
78 case k.IsPrimary():
79 mark = primaryMark.String()
80 case k.IsSecondary():
81 mark = secondaryMark.String()
82 case k.IsTertiary():
83 mark = tertiaryMark.String()
84 }
85 _, _ = fmt.Fprintf(w,
86 "%s %s %s %s",
87 fg.Foreground(k).Render(k.String()),
88 mark,
89 bg.Background(k).Render(),
90 hex.Render(k.Hex()),
91 )
92 }
93
94 // Render main color block.
95 for i := charmtone.Cumin; i < charmtone.Pepper; i++ {
96 k := charmtone.Keys()[i]
97 renderSwatch(&b, k)
98 if i%3 == 2 {
99 b.WriteRune('\n')
100 } else {
101 b.WriteRune(' ')
102 }
103 }
104
105 // Get total block width so far.
106 var totalWidth int
107 for l := range SplitSeq(b.String(), "\n") {
108 if w := lipgloss.Width(l); w > totalWidth {
109 totalWidth = w
110 }
111 }
112
113 // Grayscale block.
114 var grays strings.Builder
115 for i := charmtone.Pepper; i <= charmtone.Butter; i++ {
116 k := charmtone.Keys()[i]
117 renderSwatch(&grays, k)
118 if i < charmtone.Butter {
119 grays.WriteRune('\n')
120 }
121 }
122
123 // Get width of grayscale block.
124 var grayWidth int
125 for l := range SplitSeq(grays.String(), "\n") {
126 if w := lipgloss.Width(l); w > grayWidth {
127 grayWidth = w
128 }
129 }
130
131 fmt.Fprint(&b, "\n")
132
133 // Build legend.
134 legendBlock := legend.Render(
135 strings.Join([]string{
136 primaryMark.String() + subdued.Render(" Primary"),
137 secondaryMark.String() + subdued.Render(" Secondary"),
138 tertiaryMark.String() + subdued.Render(" Tertiary"),
139 }, " "),
140 )
141
142 // Build gradients.
143 var grads strings.Builder
144 gap := " "
145 gapWidth := lipgloss.Width(gap)
146 {
147 fullWidth := (totalWidth - grayWidth) - lipgloss.Width(gap)
148 if fullWidth%2 != 0 {
149 fullWidth--
150 }
151 halfWidth := fullWidth / gapWidth
152
153 block := lipgloss.NewStyle().
154 Align(lipgloss.Center).
155 Width(halfWidth)
156 s := subdued.
157 Foreground(charmtone.Squid)
158
159 // Gradient:
160 // Hazy -> Blush, Bok -> Zest
161 left := blendKeys(halfWidth, charmtone.Hazy, charmtone.Blush)
162 left += "\n" + block.Render(s.Render("Hazy")+rightArrowMark.String()+s.Render("Blush"))
163 right := blendKeys(halfWidth, charmtone.Bok, charmtone.Zest)
164 right += "\n" + block.Render(s.Render("Bok")+rightArrowMark.String()+s.Render("Zest"))
165 fmt.Fprint(&grads, "\n", lipgloss.JoinHorizontal(lipgloss.Top, gap, left, right))
166
167 // Gradient:
168 // Uni -> Coral -> Tuna -> Violet -> Malibu -> Turtle
169 block = block.Width(fullWidth)
170 buf := strings.Builder{}
171 fmt.Fprint(&buf, blendKeys(fullWidth, charmtone.Uni,
172 charmtone.Coral, charmtone.Tuna, charmtone.Violet,
173 charmtone.Malibu, charmtone.Turtle,
174 ))
175 fmt.Fprint(&buf, "\n",
176 block.Render(
177 s.Render("Uni")+rightArrowMark.String()+
178 s.Render("Coral")+rightArrowMark.String()+
179 s.Render("Tuna")+rightArrowMark.String()+
180 s.Render("Violet")+rightArrowMark.String()+
181 s.Render("Malibu")+rightArrowMark.String()+
182 s.Render("Turtle"),
183 ),
184 )
185 fmt.Fprint(&grads, "\n\n", lipgloss.JoinHorizontal(lipgloss.Top, gap, buf.String()))
186 }
187
188 // Join Greys and legend.
189 fmt.Fprint(&b, lipgloss.JoinHorizontal(lipgloss.Top, grays.String(), " ", grads.String()))
190
191 fmt.Fprint(&b, "\n\n", legendBlock, "\n\n")
192
193 // Flush.
194 _, err := lipgloss.Print(b.String())
195 if err != nil {
196 fmt.Fprintf(os.Stderr, "Error printing: %v\n", err)
197 os.Exit(1)
198 }
199 }
200
201 func blendKeys(size int, keys ...charmtone.Key) string {
202 stops := make([]color.Color, len(keys))
203 for i := range keys {
204 stops[i] = color.Color(keys[i])
205 }
206
207 var w strings.Builder
208 for _, c := range lipgloss.Blend1D(size, stops...) {
209 fmt.Fprint(&w, lipgloss.NewStyle().Background(c).Render(" "))
210 }
211 return w.String()
212 }
213
214 // SplitSeq returns an iterator over the substrings of string separated by a separator.
215 //
216 // This is a Go 1.23 compatible version of strings.Split. Once we're
217 // supporting Go 1.24 this can be removed in favor of the strings.SplitSeq in
218 // the standard library.
219 func SplitSeq(s, sep string) iter.Seq[string] {
220 return func(yield func(string) bool) {
221 if sep == "" {
222 for _, r := range s {
223 if !yield(string(r)) {
224 return
225 }
226 }
227 return
228 }
229
230 start := 0
231 for {
232 i := strings.Index(s[start:], sep)
233 if i == -1 {
234 if !yield(s[start:]) {
235 return
236 }
237 break
238 }
239 if !yield(s[start : start+i]) {
240 return
241 }
242 start += i + len(sep)
243 }
244 }
245 }