| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"image/color" |
| 7 |
"log" |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/charmbracelet/lipgloss/v2" |
| 12 |
uv "github.com/charmbracelet/ultraviolet" |
| 13 |
"github.com/charmbracelet/ultraviolet/screen" |
| 14 |
"github.com/charmbracelet/x/ansi" |
| 15 |
"github.com/lucasb-eyer/go-colorful" |
| 16 |
"github.com/rivo/uniseg" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
width = 96 |
| 25 |
|
| 26 |
|
| 27 |
columnWidth = 30 |
| 28 |
) |
| 29 |
|
| 30 |
var ( |
| 31 |
|
| 32 |
hasDarkBG bool |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
lightDark lipgloss.LightDarkFunc |
| 37 |
) |
| 38 |
|
| 39 |
func init() { |
| 40 |
|
| 41 |
hasDarkBG = lipgloss.HasDarkBackground(os.Stdin, os.Stdout) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
lightDark = lipgloss.LightDark(hasDarkBG) |
| 46 |
} |
| 47 |
|
| 48 |
func main() { |
| 49 |
|
| 50 |
var ( |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
subtle = lightDark(lipgloss.Color("#D9DCCF"), lipgloss.Color("#383838")) |
| 55 |
highlight = lightDark(lipgloss.Color("#874BFD"), lipgloss.Color("#7D56F4")) |
| 56 |
special = lightDark(lipgloss.Color("#43BF6D"), lipgloss.Color("#73F59F")) |
| 57 |
|
| 58 |
divider = lipgloss.NewStyle(). |
| 59 |
SetString("•"). |
| 60 |
Padding(0, 1). |
| 61 |
Foreground(subtle). |
| 62 |
String() |
| 63 |
|
| 64 |
url = lipgloss.NewStyle().Foreground(special).Render |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
activeTabBorder = lipgloss.Border{ |
| 69 |
Top: "─", |
| 70 |
Bottom: " ", |
| 71 |
Left: "│", |
| 72 |
Right: "│", |
| 73 |
TopLeft: "╭", |
| 74 |
TopRight: "╮", |
| 75 |
BottomLeft: "┘", |
| 76 |
BottomRight: "└", |
| 77 |
} |
| 78 |
|
| 79 |
tabBorder = lipgloss.Border{ |
| 80 |
Top: "─", |
| 81 |
Bottom: "─", |
| 82 |
Left: "│", |
| 83 |
Right: "│", |
| 84 |
TopLeft: "╭", |
| 85 |
TopRight: "╮", |
| 86 |
BottomLeft: "┴", |
| 87 |
BottomRight: "┴", |
| 88 |
} |
| 89 |
|
| 90 |
tab = lipgloss.NewStyle(). |
| 91 |
Border(tabBorder, true). |
| 92 |
BorderForeground(highlight). |
| 93 |
Padding(0, 1) |
| 94 |
|
| 95 |
activeTab = tab.Border(activeTabBorder, true) |
| 96 |
|
| 97 |
tabGap = tab. |
| 98 |
BorderTop(false). |
| 99 |
BorderLeft(false). |
| 100 |
BorderRight(false) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
titleStyle = lipgloss.NewStyle(). |
| 105 |
MarginLeft(1). |
| 106 |
MarginRight(5). |
| 107 |
Padding(0, 1). |
| 108 |
Italic(true). |
| 109 |
Foreground(lipgloss.Color("#FFF7DB")). |
| 110 |
SetString("Lip Gloss") |
| 111 |
|
| 112 |
descStyle = lipgloss.NewStyle().MarginTop(1) |
| 113 |
|
| 114 |
infoStyle = lipgloss.NewStyle(). |
| 115 |
BorderStyle(lipgloss.NormalBorder()). |
| 116 |
BorderTop(true). |
| 117 |
BorderForeground(subtle) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
dialogBoxStyle = lipgloss.NewStyle(). |
| 122 |
Border(lipgloss.RoundedBorder()). |
| 123 |
BorderForeground(lipgloss.Color("#874BFD")). |
| 124 |
Padding(1, 0). |
| 125 |
BorderTop(true). |
| 126 |
BorderLeft(true). |
| 127 |
BorderRight(true). |
| 128 |
BorderBottom(true) |
| 129 |
|
| 130 |
buttonStyle = lipgloss.NewStyle(). |
| 131 |
Foreground(lipgloss.Color("#FFF7DB")). |
| 132 |
Background(lipgloss.Color("#888B7E")). |
| 133 |
Padding(0, 3). |
| 134 |
MarginTop(1) |
| 135 |
|
| 136 |
activeButtonStyle = buttonStyle. |
| 137 |
Foreground(lipgloss.Color("#FFF7DB")). |
| 138 |
Background(lipgloss.Color("#F25D94")). |
| 139 |
MarginRight(2). |
| 140 |
Underline(true) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
list = lipgloss.NewStyle(). |
| 145 |
Border(lipgloss.NormalBorder(), false, true, false, false). |
| 146 |
BorderForeground(subtle). |
| 147 |
MarginRight(2). |
| 148 |
Height(8). |
| 149 |
Width(columnWidth + 1) |
| 150 |
|
| 151 |
listHeader = lipgloss.NewStyle(). |
| 152 |
BorderStyle(lipgloss.NormalBorder()). |
| 153 |
BorderBottom(true). |
| 154 |
BorderForeground(subtle). |
| 155 |
MarginRight(2). |
| 156 |
Render |
| 157 |
|
| 158 |
listItem = lipgloss.NewStyle().PaddingLeft(2).Render |
| 159 |
|
| 160 |
checkMark = lipgloss.NewStyle().SetString("✓"). |
| 161 |
Foreground(special). |
| 162 |
PaddingRight(1). |
| 163 |
String() |
| 164 |
|
| 165 |
listDone = func(s string) string { |
| 166 |
return checkMark + lipgloss.NewStyle(). |
| 167 |
Strikethrough(true). |
| 168 |
Foreground(lightDark(lipgloss.Color("#969B86"), lipgloss.Color("#696969"))). |
| 169 |
Render(s) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
historyStyle = lipgloss.NewStyle(). |
| 175 |
Align(lipgloss.Left). |
| 176 |
Foreground(lipgloss.Color("#FAFAFA")). |
| 177 |
Background(highlight). |
| 178 |
Margin(1, 3, 0, 0). |
| 179 |
Padding(1, 2). |
| 180 |
Height(19). |
| 181 |
Width(columnWidth) |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
statusNugget = lipgloss.NewStyle(). |
| 186 |
Foreground(lipgloss.Color("#FFFDF5")). |
| 187 |
Padding(0, 1) |
| 188 |
|
| 189 |
statusBarStyle = lipgloss.NewStyle(). |
| 190 |
Foreground(lightDark(lipgloss.Color("#343433"), lipgloss.Color("#C1C6B2"))). |
| 191 |
Background(lightDark(lipgloss.Color("#D9DCCF"), lipgloss.Color("#353533"))) |
| 192 |
|
| 193 |
statusStyle = lipgloss.NewStyle(). |
| 194 |
Inherit(statusBarStyle). |
| 195 |
Foreground(lipgloss.Color("#FFFDF5")). |
| 196 |
Background(lipgloss.Color("#FF5F87")). |
| 197 |
Padding(0, 1). |
| 198 |
MarginRight(1) |
| 199 |
|
| 200 |
encodingStyle = statusNugget. |
| 201 |
Background(lipgloss.Color("#A550DF")). |
| 202 |
Align(lipgloss.Right) |
| 203 |
|
| 204 |
statusText = lipgloss.NewStyle().Inherit(statusBarStyle) |
| 205 |
|
| 206 |
fishCakeStyle = statusNugget.Background(lipgloss.Color("#6124DF")) |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2) |
| 211 |
) |
| 212 |
|
| 213 |
doc := strings.Builder{} |
| 214 |
|
| 215 |
|
| 216 |
{ |
| 217 |
row := lipgloss.JoinHorizontal( |
| 218 |
lipgloss.Top, |
| 219 |
activeTab.Render("Lip Gloss"), |
| 220 |
tab.Render("Blush"), |
| 221 |
tab.Render("Eye Shadow"), |
| 222 |
tab.Render("Mascara"), |
| 223 |
tab.Render("Foundation"), |
| 224 |
) |
| 225 |
gap := tabGap.Render(strings.Repeat(" ", max(0, width-lipgloss.Width(row)-2))) |
| 226 |
row = lipgloss.JoinHorizontal(lipgloss.Bottom, row, gap) |
| 227 |
doc.WriteString(row + "\n\n") |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
{ |
| 232 |
var ( |
| 233 |
colors = colorGrid(1, 5) |
| 234 |
title strings.Builder |
| 235 |
) |
| 236 |
|
| 237 |
for i, v := range colors { |
| 238 |
const offset = 2 |
| 239 |
c := lipgloss.Color(v[0]) |
| 240 |
fmt.Fprint(&title, titleStyle.MarginLeft(i*offset).Background(c)) |
| 241 |
if i < len(colors)-1 { |
| 242 |
title.WriteRune('\n') |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
desc := lipgloss.JoinVertical(lipgloss.Left, |
| 247 |
descStyle.Render("Style Definitions for Nice Terminal Layouts"), |
| 248 |
infoStyle.Render("From Charm"+divider+url("https://github.com/charmbracelet/lipgloss")), |
| 249 |
) |
| 250 |
|
| 251 |
row := lipgloss.JoinHorizontal(lipgloss.Top, title.String(), desc) |
| 252 |
doc.WriteString(row + "\n\n") |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
okButton := activeButtonStyle.Render("Yes") |
| 257 |
cancelButton := buttonStyle.Render("Maybe") |
| 258 |
|
| 259 |
grad := applyGradient( |
| 260 |
lipgloss.NewStyle(), |
| 261 |
"Are you sure you want to eat marmalade?", |
| 262 |
lipgloss.Color("#EDFF82"), |
| 263 |
lipgloss.Color("#F25D94"), |
| 264 |
) |
| 265 |
|
| 266 |
question := lipgloss.NewStyle(). |
| 267 |
Width(50). |
| 268 |
Align(lipgloss.Center). |
| 269 |
Render(grad) |
| 270 |
|
| 271 |
buttons := lipgloss.JoinHorizontal(lipgloss.Top, okButton, cancelButton) |
| 272 |
dialogUI := lipgloss.JoinVertical(lipgloss.Center, question, buttons) |
| 273 |
|
| 274 |
dialog := lipgloss.Place(width, 9, |
| 275 |
lipgloss.Center, lipgloss.Center, |
| 276 |
"", |
| 277 |
|
| 278 |
lipgloss.WithWhitespaceChars("猫咪"), |
| 279 |
lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(subtle)), |
| 280 |
) |
| 281 |
|
| 282 |
doc.WriteString(dialog + "\n\n") |
| 283 |
|
| 284 |
|
| 285 |
colors := func() string { |
| 286 |
colors := colorGrid(14, 8) |
| 287 |
|
| 288 |
b := strings.Builder{} |
| 289 |
for _, x := range colors { |
| 290 |
for _, y := range x { |
| 291 |
s := lipgloss.NewStyle().SetString(" ").Background(lipgloss.Color(y)) |
| 292 |
b.WriteString(s.String()) |
| 293 |
} |
| 294 |
b.WriteRune('\n') |
| 295 |
} |
| 296 |
|
| 297 |
return b.String() |
| 298 |
}() |
| 299 |
|
| 300 |
lists := lipgloss.JoinHorizontal(lipgloss.Top, |
| 301 |
list.Render( |
| 302 |
lipgloss.JoinVertical(lipgloss.Left, |
| 303 |
listHeader("Citrus Fruits to Try"), |
| 304 |
listDone("Grapefruit"), |
| 305 |
listDone("Yuzu"), |
| 306 |
listItem("Citron"), |
| 307 |
listItem("Kumquat"), |
| 308 |
listItem("Pomelo"), |
| 309 |
), |
| 310 |
), |
| 311 |
list.Width(columnWidth).Render( |
| 312 |
lipgloss.JoinVertical(lipgloss.Left, |
| 313 |
listHeader("Actual Lip Gloss Vendors"), |
| 314 |
listItem("Glossier"), |
| 315 |
listItem("Claire‘s Boutique"), |
| 316 |
listDone("Nyx"), |
| 317 |
listItem("Mac"), |
| 318 |
listDone("Milk"), |
| 319 |
), |
| 320 |
), |
| 321 |
) |
| 322 |
|
| 323 |
doc.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lists, colors)) |
| 324 |
|
| 325 |
|
| 326 |
{ |
| 327 |
const ( |
| 328 |
historyA = "The Romans learned from the Greeks that quinces slowly cooked with honey would “set” when cool. The Apicius gives a recipe for preserving whole quinces, stems and leaves attached, in a bath of honey diluted with defrutum: Roman marmalade. Preserves of quince and lemon appear (along with rose, apple, plum and pear) in the Book of ceremonies of the Byzantine Emperor Constantine VII Porphyrogennetos." |
| 329 |
historyB = "Medieval quince preserves, which went by the French name cotignac, produced in a clear version and a fruit pulp version, began to lose their medieval seasoning of spices in the 16th century. In the 17th century, La Varenne provided recipes for both thick and clear cotignac." |
| 330 |
historyC = "In 1524, Henry VIII, King of England, received a “box of marmalade” from Mr. Hull of Exeter. This was probably marmelada, a solid quince paste from Portugal, still made and sold in southern Europe today. It became a favourite treat of Anne Boleyn and her ladies in waiting." |
| 331 |
) |
| 332 |
|
| 333 |
doc.WriteString(lipgloss.JoinHorizontal( |
| 334 |
lipgloss.Top, |
| 335 |
historyStyle.Align(lipgloss.Right).Render(historyA), |
| 336 |
historyStyle.Align(lipgloss.Center).Render(historyB), |
| 337 |
historyStyle.MarginRight(0).Render(historyC), |
| 338 |
)) |
| 339 |
|
| 340 |
doc.WriteString("\n\n") |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
{ |
| 345 |
w := lipgloss.Width |
| 346 |
|
| 347 |
lightDarkState := "Light" |
| 348 |
if hasDarkBG { |
| 349 |
lightDarkState = "Dark" |
| 350 |
} |
| 351 |
|
| 352 |
statusKey := statusStyle.Render("STATUS") |
| 353 |
encoding := encodingStyle.Render("UTF-8") |
| 354 |
fishCake := fishCakeStyle.Render("🍥 Fish Cake") |
| 355 |
statusVal := statusText. |
| 356 |
Width(width - w(statusKey) - w(encoding) - w(fishCake)). |
| 357 |
Render("Ravishingly " + lightDarkState + "!") |
| 358 |
|
| 359 |
bar := lipgloss.JoinHorizontal(lipgloss.Top, |
| 360 |
statusKey, |
| 361 |
statusVal, |
| 362 |
encoding, |
| 363 |
fishCake, |
| 364 |
) |
| 365 |
|
| 366 |
doc.WriteString(statusBarStyle.Width(width).Render(bar)) |
| 367 |
} |
| 368 |
|
| 369 |
t := uv.DefaultTerminal() |
| 370 |
|
| 371 |
if err := t.Start(); err != nil { |
| 372 |
log.Fatalf("starting program: %v", err) |
| 373 |
} |
| 374 |
|
| 375 |
t.SetLogger(log.Default()) |
| 376 |
|
| 377 |
physicalWidth, _, err := t.GetSize() |
| 378 |
if err != nil { |
| 379 |
log.Fatalf("getting size: %v", err) |
| 380 |
} |
| 381 |
|
| 382 |
if physicalWidth > 0 { |
| 383 |
docStyle = docStyle.MaxWidth(physicalWidth) |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
t.EnterAltScreen() |
| 388 |
|
| 389 |
|
| 390 |
t.WriteString(ansi.SetButtonEventMouseMode + ansi.SetSgrExtMouseMode) |
| 391 |
|
| 392 |
|
| 393 |
dialogWidth := lipgloss.Width(dialogUI) + dialogBoxStyle.GetHorizontalFrameSize() |
| 394 |
dialogHeight := lipgloss.Height(dialogUI) + dialogBoxStyle.GetVerticalFrameSize() |
| 395 |
dialogX, dialogY := physicalWidth/2-dialogWidth/2-docStyle.GetVerticalFrameSize()-1, 12 |
| 396 |
mainDoc := docStyle.Render(doc.String()) |
| 397 |
|
| 398 |
display := func() { |
| 399 |
screen.Clear(t) |
| 400 |
mainSs := uv.NewStyledString(mainDoc) |
| 401 |
mainSs.Draw(t, t.Bounds()) |
| 402 |
boxArea := uv.Rect(dialogX, dialogY, dialogWidth, dialogHeight) |
| 403 |
box := uv.NewStyledString(dialogBoxStyle.Render(dialogUI)) |
| 404 |
box.Draw(t, boxArea) |
| 405 |
t.Display() |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
display() |
| 410 |
|
| 411 |
LOOP: |
| 412 |
for ev := range t.Events() { |
| 413 |
log.Printf("event: %T", ev) |
| 414 |
switch ev := ev.(type) { |
| 415 |
case uv.WindowSizeEvent: |
| 416 |
t.Resize(ev.Width, ev.Height) |
| 417 |
t.Erase() |
| 418 |
case uv.MouseClickEvent: |
| 419 |
dialogX, dialogY = ev.X-dialogWidth/2, ev.Y-dialogHeight/2 |
| 420 |
case uv.KeyPressEvent: |
| 421 |
log.Printf("%T %q %q", ev, ev.String(), ev.Keystroke()) |
| 422 |
switch { |
| 423 |
case ev.MatchString("ctrl+c", "q"): |
| 424 |
break LOOP |
| 425 |
case ev.MatchString("left", "h"): |
| 426 |
dialogX-- |
| 427 |
case ev.MatchString("down", "j"): |
| 428 |
dialogY++ |
| 429 |
case ev.MatchString("up", "k"): |
| 430 |
dialogY-- |
| 431 |
case ev.MatchString("right", "l"): |
| 432 |
dialogX++ |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
display() |
| 438 |
} |
| 439 |
|
| 440 |
t.WriteString(ansi.ResetButtonEventMouseMode + ansi.ResetSgrExtMouseMode) |
| 441 |
|
| 442 |
|
| 443 |
if err := t.Shutdown(context.Background()); err != nil { |
| 444 |
log.Fatalf("shutting down program: %v", err) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
func colorGrid(xSteps, ySteps int) [][]string { |
| 449 |
x0y0, _ := colorful.Hex("#F25D94") |
| 450 |
x1y0, _ := colorful.Hex("#EDFF82") |
| 451 |
x0y1, _ := colorful.Hex("#643AFF") |
| 452 |
x1y1, _ := colorful.Hex("#14F9D5") |
| 453 |
|
| 454 |
x0 := make([]colorful.Color, ySteps) |
| 455 |
for i := range x0 { |
| 456 |
x0[i] = x0y0.BlendLuv(x0y1, float64(i)/float64(ySteps)) |
| 457 |
} |
| 458 |
|
| 459 |
x1 := make([]colorful.Color, ySteps) |
| 460 |
for i := range x1 { |
| 461 |
x1[i] = x1y0.BlendLuv(x1y1, float64(i)/float64(ySteps)) |
| 462 |
} |
| 463 |
|
| 464 |
grid := make([][]string, ySteps) |
| 465 |
for x := 0; x < ySteps; x++ { |
| 466 |
y0 := x0[x] |
| 467 |
grid[x] = make([]string, xSteps) |
| 468 |
for y := 0; y < xSteps; y++ { |
| 469 |
grid[x][y] = y0.BlendLuv(x1[x], float64(y)/float64(xSteps)).Hex() |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
return grid |
| 474 |
} |
| 475 |
|
| 476 |
func max(a, b int) int { |
| 477 |
if a > b { |
| 478 |
return a |
| 479 |
} |
| 480 |
return b |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
func applyGradient(base lipgloss.Style, input string, from, to color.Color) string { |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
g := uniseg.NewGraphemes(input) |
| 493 |
var chars []string |
| 494 |
for g.Next() { |
| 495 |
chars = append(chars, g.Str()) |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
a, _ := colorful.MakeColor(to) |
| 500 |
b, _ := colorful.MakeColor(from) |
| 501 |
var output strings.Builder |
| 502 |
var hex string |
| 503 |
for i := 0; i < len(chars); i++ { |
| 504 |
hex = a.BlendLuv(b, float64(i)/float64(len(chars)-1)).Hex() |
| 505 |
output.WriteString(base.Foreground(lipgloss.Color(hex)).Render(chars[i])) |
| 506 |
} |
| 507 |
|
| 508 |
return output.String() |
| 509 |
} |
| 510 |
|
| 511 |
func init() { |
| 512 |
f, err := os.OpenFile("layout.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666) |
| 513 |
if err != nil { |
| 514 |
log.Fatal(err) |
| 515 |
} |
| 516 |
log.SetOutput(f) |
| 517 |
} |