| 1 |
// Package main demonstrates usage. |
| 2 |
package main |
| 3 |
|
| 4 |
// This example demonstrates various Lip Gloss style and layout features. |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"image/color" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/charmbracelet/colorprofile" |
| 14 |
"github.com/charmbracelet/lipgloss/v2" |
| 15 |
"github.com/charmbracelet/x/ansi" |
| 16 |
"github.com/charmbracelet/x/cellbuf" |
| 17 |
"github.com/charmbracelet/x/input" |
| 18 |
"github.com/charmbracelet/x/term" |
| 19 |
"github.com/lucasb-eyer/go-colorful" |
| 20 |
"github.com/rivo/uniseg" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
// In real life situations we'd adjust the document to fit the width we've |
| 25 |
// detected. In the case of this example we're hardcoding the width, and |
| 26 |
// later using the detected width only to truncate in order to avoid jaggy |
| 27 |
// wrapping. |
| 28 |
width = 96 |
| 29 |
|
| 30 |
// How wide to render various columns in the layout. |
| 31 |
columnWidth = 30 |
| 32 |
) |
| 33 |
|
| 34 |
var ( |
| 35 |
// Whether the detected background color is dark. We detect this in init(). |
| 36 |
hasDarkBG bool |
| 37 |
|
| 38 |
// A helper function for choosing either a light or dark color based on the |
| 39 |
// detected background color. We create this in init(). |
| 40 |
lightDark lipgloss.LightDarkFunc |
| 41 |
) |
| 42 |
|
| 43 |
func init() { |
| 44 |
// Detect the background color. |
| 45 |
hasDarkBG = lipgloss.HasDarkBackground(os.Stdin, os.Stdout) |
| 46 |
|
| 47 |
// Create a new helper function for choosing either a light or dark color |
| 48 |
// based on the detected background color. |
| 49 |
lightDark = lipgloss.LightDark(hasDarkBG) |
| 50 |
} |
| 51 |
|
| 52 |
func main() { |
| 53 |
// Style definitions. |
| 54 |
var ( |
| 55 |
|
| 56 |
// General. |
| 57 |
|
| 58 |
subtle = lightDark(lipgloss.Color("#D9DCCF"), lipgloss.Color("#383838")) |
| 59 |
highlight = lightDark(lipgloss.Color("#874BFD"), lipgloss.Color("#7D56F4")) |
| 60 |
special = lightDark(lipgloss.Color("#43BF6D"), lipgloss.Color("#73F59F")) |
| 61 |
|
| 62 |
divider = lipgloss.NewStyle(). |
| 63 |
SetString("•"). |
| 64 |
Padding(0, 1). |
| 65 |
Foreground(subtle). |
| 66 |
String() |
| 67 |
|
| 68 |
url = lipgloss.NewStyle().Foreground(special).Render |
| 69 |
|
| 70 |
// Tabs. |
| 71 |
|
| 72 |
activeTabBorder = lipgloss.Border{ |
| 73 |
Top: "─", |
| 74 |
Bottom: " ", |
| 75 |
Left: "│", |
| 76 |
Right: "│", |
| 77 |
TopLeft: "╭", |
| 78 |
TopRight: "╮", |
| 79 |
BottomLeft: "┘", |
| 80 |
BottomRight: "└", |
| 81 |
} |
| 82 |
|
| 83 |
tabBorder = lipgloss.Border{ |
| 84 |
Top: "─", |
| 85 |
Bottom: "─", |
| 86 |
Left: "│", |
| 87 |
Right: "│", |
| 88 |
TopLeft: "╭", |
| 89 |
TopRight: "╮", |
| 90 |
BottomLeft: "┴", |
| 91 |
BottomRight: "┴", |
| 92 |
} |
| 93 |
|
| 94 |
tab = lipgloss.NewStyle(). |
| 95 |
Border(tabBorder, true). |
| 96 |
BorderForeground(highlight). |
| 97 |
Padding(0, 1) |
| 98 |
|
| 99 |
activeTab = tab.Border(activeTabBorder, true) |
| 100 |
|
| 101 |
tabGap = tab. |
| 102 |
BorderTop(false). |
| 103 |
BorderLeft(false). |
| 104 |
BorderRight(false) |
| 105 |
|
| 106 |
// Title. |
| 107 |
|
| 108 |
titleStyle = lipgloss.NewStyle(). |
| 109 |
MarginLeft(1). |
| 110 |
MarginRight(5). |
| 111 |
Padding(0, 1). |
| 112 |
Italic(true). |
| 113 |
Foreground(lipgloss.Color("#FFF7DB")). |
| 114 |
SetString("Lip Gloss") |
| 115 |
|
| 116 |
descStyle = lipgloss.NewStyle().MarginTop(1) |
| 117 |
|
| 118 |
infoStyle = lipgloss.NewStyle(). |
| 119 |
BorderStyle(lipgloss.NormalBorder()). |
| 120 |
BorderTop(true). |
| 121 |
BorderForeground(subtle) |
| 122 |
|
| 123 |
// Dialog. |
| 124 |
|
| 125 |
dialogBoxStyle = lipgloss.NewStyle(). |
| 126 |
Border(lipgloss.RoundedBorder()). |
| 127 |
BorderForeground(lipgloss.Color("#874BFD")). |
| 128 |
Padding(1, 0). |
| 129 |
BorderTop(true). |
| 130 |
BorderLeft(true). |
| 131 |
BorderRight(true). |
| 132 |
BorderBottom(true) |
| 133 |
|
| 134 |
buttonStyle = lipgloss.NewStyle(). |
| 135 |
Foreground(lipgloss.Color("#FFF7DB")). |
| 136 |
Background(lipgloss.Color("#888B7E")). |
| 137 |
Padding(0, 3). |
| 138 |
MarginTop(1) |
| 139 |
|
| 140 |
activeButtonStyle = buttonStyle. |
| 141 |
Foreground(lipgloss.Color("#FFF7DB")). |
| 142 |
Background(lipgloss.Color("#F25D94")). |
| 143 |
MarginRight(2). |
| 144 |
Underline(true) |
| 145 |
|
| 146 |
// List. |
| 147 |
|
| 148 |
list = lipgloss.NewStyle(). |
| 149 |
Border(lipgloss.NormalBorder(), false, true, false, false). |
| 150 |
BorderForeground(subtle). |
| 151 |
MarginRight(2). |
| 152 |
Height(8). |
| 153 |
Width(columnWidth + 1) |
| 154 |
|
| 155 |
listHeader = lipgloss.NewStyle(). |
| 156 |
BorderStyle(lipgloss.NormalBorder()). |
| 157 |
BorderBottom(true). |
| 158 |
BorderForeground(subtle). |
| 159 |
MarginRight(2). |
| 160 |
Render |
| 161 |
|
| 162 |
listItem = lipgloss.NewStyle().PaddingLeft(2).Render |
| 163 |
|
| 164 |
checkMark = lipgloss.NewStyle().SetString("✓"). |
| 165 |
Foreground(special). |
| 166 |
PaddingRight(1). |
| 167 |
String() |
| 168 |
|
| 169 |
listDone = func(s string) string { |
| 170 |
return checkMark + lipgloss.NewStyle(). |
| 171 |
Strikethrough(true). |
| 172 |
Foreground(lightDark(lipgloss.Color("#969B86"), lipgloss.Color("#696969"))). |
| 173 |
Render(s) |
| 174 |
} |
| 175 |
|
| 176 |
// Paragraphs/History. |
| 177 |
|
| 178 |
historyStyle = lipgloss.NewStyle(). |
| 179 |
Align(lipgloss.Left). |
| 180 |
Foreground(lipgloss.Color("#FAFAFA")). |
| 181 |
Background(highlight). |
| 182 |
Margin(1, 3, 0, 0). |
| 183 |
Padding(1, 2). |
| 184 |
Height(19). |
| 185 |
Width(columnWidth) |
| 186 |
|
| 187 |
// Status Bar. |
| 188 |
|
| 189 |
statusNugget = lipgloss.NewStyle(). |
| 190 |
Foreground(lipgloss.Color("#FFFDF5")). |
| 191 |
Padding(0, 1) |
| 192 |
|
| 193 |
statusBarStyle = lipgloss.NewStyle(). |
| 194 |
Foreground(lightDark(lipgloss.Color("#343433"), lipgloss.Color("#C1C6B2"))). |
| 195 |
Background(lightDark(lipgloss.Color("#D9DCCF"), lipgloss.Color("#353533"))) |
| 196 |
|
| 197 |
statusStyle = lipgloss.NewStyle(). |
| 198 |
Inherit(statusBarStyle). |
| 199 |
Foreground(lipgloss.Color("#FFFDF5")). |
| 200 |
Background(lipgloss.Color("#FF5F87")). |
| 201 |
Padding(0, 1). |
| 202 |
MarginRight(1) |
| 203 |
|
| 204 |
encodingStyle = statusNugget. |
| 205 |
Background(lipgloss.Color("#A550DF")). |
| 206 |
Align(lipgloss.Right) |
| 207 |
|
| 208 |
statusText = lipgloss.NewStyle().Inherit(statusBarStyle) |
| 209 |
|
| 210 |
fishCakeStyle = statusNugget.Background(lipgloss.Color("#6124DF")) |
| 211 |
|
| 212 |
// Page. |
| 213 |
|
| 214 |
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2) |
| 215 |
) |
| 216 |
|
| 217 |
physicalWidth, physicalHeight, _ := term.GetSize(os.Stdout.Fd()) |
| 218 |
doc := strings.Builder{} |
| 219 |
|
| 220 |
// Tabs. |
| 221 |
{ |
| 222 |
row := lipgloss.JoinHorizontal( |
| 223 |
lipgloss.Top, |
| 224 |
activeTab.Render("Lip Gloss"), |
| 225 |
tab.Render("Blush"), |
| 226 |
tab.Render("Eye Shadow"), |
| 227 |
tab.Render("Mascara"), |
| 228 |
tab.Render("Foundation"), |
| 229 |
) |
| 230 |
gap := tabGap.Render(strings.Repeat(" ", max(0, width-lipgloss.Width(row)-2))) |
| 231 |
row = lipgloss.JoinHorizontal(lipgloss.Bottom, row, gap) |
| 232 |
doc.WriteString(row + "\n\n") |
| 233 |
} |
| 234 |
|
| 235 |
// Title. |
| 236 |
{ |
| 237 |
var ( |
| 238 |
colors = colorGrid(1, 5) |
| 239 |
title strings.Builder |
| 240 |
) |
| 241 |
|
| 242 |
for i, v := range colors { |
| 243 |
const offset = 2 |
| 244 |
c := lipgloss.Color(v[0]) |
| 245 |
fmt.Fprint(&title, titleStyle.MarginLeft(i*offset).Background(c)) |
| 246 |
if i < len(colors)-1 { |
| 247 |
title.WriteRune('\n') |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
desc := lipgloss.JoinVertical(lipgloss.Left, |
| 252 |
descStyle.Render("Style Definitions for Nice Terminal Layouts"), |
| 253 |
infoStyle.Render("From Charm"+divider+url("https://github.com/charmbracelet/lipgloss")), |
| 254 |
) |
| 255 |
|
| 256 |
row := lipgloss.JoinHorizontal(lipgloss.Top, title.String(), desc) |
| 257 |
doc.WriteString(row + "\n\n") |
| 258 |
} |
| 259 |
|
| 260 |
// Dialog. |
| 261 |
okButton := activeButtonStyle.Render("Yes") |
| 262 |
cancelButton := buttonStyle.Render("Maybe") |
| 263 |
|
| 264 |
grad := applyGradient( |
| 265 |
lipgloss.NewStyle(), |
| 266 |
"Are you sure you want to eat marmalade?", |
| 267 |
lipgloss.Color("#EDFF82"), |
| 268 |
lipgloss.Color("#F25D94"), |
| 269 |
) |
| 270 |
|
| 271 |
question := lipgloss.NewStyle(). |
| 272 |
Width(50). |
| 273 |
Align(lipgloss.Center). |
| 274 |
Render(grad) |
| 275 |
|
| 276 |
buttons := lipgloss.JoinHorizontal(lipgloss.Top, okButton, cancelButton) |
| 277 |
dialogUI := lipgloss.JoinVertical(lipgloss.Center, question, buttons) |
| 278 |
|
| 279 |
dialog := lipgloss.Place(width, 9, |
| 280 |
lipgloss.Center, lipgloss.Center, |
| 281 |
"", |
| 282 |
// dialogBoxStyle.Render(dialogUi), |
| 283 |
lipgloss.WithWhitespaceChars("猫咪"), |
| 284 |
lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(subtle)), |
| 285 |
) |
| 286 |
|
| 287 |
doc.WriteString(dialog + "\n\n") |
| 288 |
|
| 289 |
// Color grid. |
| 290 |
colors := func() string { |
| 291 |
colors := colorGrid(14, 8) |
| 292 |
|
| 293 |
b := strings.Builder{} |
| 294 |
for _, x := range colors { |
| 295 |
for _, y := range x { |
| 296 |
s := lipgloss.NewStyle().SetString(" ").Background(lipgloss.Color(y)) |
| 297 |
b.WriteString(s.String()) |
| 298 |
} |
| 299 |
b.WriteRune('\n') |
| 300 |
} |
| 301 |
|
| 302 |
return b.String() |
| 303 |
}() |
| 304 |
|
| 305 |
lists := lipgloss.JoinHorizontal(lipgloss.Top, |
| 306 |
list.Render( |
| 307 |
lipgloss.JoinVertical(lipgloss.Left, |
| 308 |
listHeader("Citrus Fruits to Try"), |
| 309 |
listDone("Grapefruit"), |
| 310 |
listDone("Yuzu"), |
| 311 |
listItem("Citron"), |
| 312 |
listItem("Kumquat"), |
| 313 |
listItem("Pomelo"), |
| 314 |
), |
| 315 |
), |
| 316 |
list.Width(columnWidth).Render( |
| 317 |
lipgloss.JoinVertical(lipgloss.Left, |
| 318 |
listHeader("Actual Lip Gloss Vendors"), |
| 319 |
listItem("Glossier"), |
| 320 |
listItem("Claire‘s Boutique"), |
| 321 |
listDone("Nyx"), |
| 322 |
listItem("Mac"), |
| 323 |
listDone("Milk"), |
| 324 |
), |
| 325 |
), |
| 326 |
) |
| 327 |
|
| 328 |
doc.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lists, colors)) |
| 329 |
|
| 330 |
// Marmalade history. |
| 331 |
{ |
| 332 |
const ( |
| 333 |
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." |
| 334 |
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." |
| 335 |
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." |
| 336 |
) |
| 337 |
|
| 338 |
doc.WriteString(lipgloss.JoinHorizontal( |
| 339 |
lipgloss.Top, |
| 340 |
historyStyle.Align(lipgloss.Right).Render(historyA), |
| 341 |
historyStyle.Align(lipgloss.Center).Render(historyB), |
| 342 |
historyStyle.MarginRight(0).Render(historyC), |
| 343 |
)) |
| 344 |
|
| 345 |
doc.WriteString("\n\n") |
| 346 |
} |
| 347 |
|
| 348 |
// Status bar. |
| 349 |
{ |
| 350 |
w := lipgloss.Width |
| 351 |
|
| 352 |
lightDarkState := "Light" |
| 353 |
if hasDarkBG { |
| 354 |
lightDarkState = "Dark" |
| 355 |
} |
| 356 |
|
| 357 |
statusKey := statusStyle.Render("STATUS") |
| 358 |
encoding := encodingStyle.Render("UTF-8") |
| 359 |
fishCake := fishCakeStyle.Render("🍥 Fish Cake") |
| 360 |
statusVal := statusText. |
| 361 |
Width(width - w(statusKey) - w(encoding) - w(fishCake)). |
| 362 |
Render("Ravishingly " + lightDarkState + "!") |
| 363 |
|
| 364 |
bar := lipgloss.JoinHorizontal(lipgloss.Top, |
| 365 |
statusKey, |
| 366 |
statusVal, |
| 367 |
encoding, |
| 368 |
fishCake, |
| 369 |
) |
| 370 |
|
| 371 |
doc.WriteString(statusBarStyle.Width(width).Render(bar)) |
| 372 |
} |
| 373 |
|
| 374 |
if physicalWidth > 0 { |
| 375 |
docStyle = docStyle.MaxWidth(physicalWidth) |
| 376 |
} |
| 377 |
|
| 378 |
termType := os.Getenv("TERM") |
| 379 |
scr := cellbuf.NewScreen(os.Stdout, physicalWidth, physicalHeight, &cellbuf.ScreenOptions{ |
| 380 |
Term: termType, |
| 381 |
Profile: colorprofile.Detect(os.Stdout, os.Environ()), |
| 382 |
AltScreen: true, |
| 383 |
}) |
| 384 |
|
| 385 |
defer scr.Close() //nolint:errcheck |
| 386 |
|
| 387 |
// Enable mouse events. |
| 388 |
modes := []ansi.Mode{ |
| 389 |
ansi.ButtonEventMouseMode, |
| 390 |
ansi.SgrExtMouseMode, |
| 391 |
} |
| 392 |
|
| 393 |
os.Stdout.WriteString(ansi.SetMode(modes...)) //nolint:errcheck,gosec |
| 394 |
defer os.Stdout.WriteString(ansi.ResetMode(modes...)) //nolint:errcheck |
| 395 |
|
| 396 |
state, err := term.MakeRaw(os.Stdin.Fd()) |
| 397 |
if err != nil { |
| 398 |
log.Fatalf("making raw: %v", err) |
| 399 |
} |
| 400 |
|
| 401 |
defer term.Restore(os.Stdin.Fd(), state) //nolint:errcheck |
| 402 |
|
| 403 |
drv, err := input.NewReader(os.Stdin, termType, 0) |
| 404 |
if err != nil { |
| 405 |
log.Fatalf("creating input driver: %v", err) |
| 406 |
} |
| 407 |
|
| 408 |
dialogWidth := lipgloss.Width(dialogUI) + dialogBoxStyle.GetHorizontalFrameSize() |
| 409 |
dialogHeight := lipgloss.Height(dialogUI) + dialogBoxStyle.GetVerticalFrameSize() |
| 410 |
dialogX, dialogY := physicalWidth/2-dialogWidth/2-docStyle.GetVerticalFrameSize()-1, 12 |
| 411 |
scrw := cellbuf.NewScreenWriter(scr) |
| 412 |
render := func() { |
| 413 |
scr.Clear() |
| 414 |
scrw.SetContent(docStyle.Render(doc.String())) |
| 415 |
box := cellbuf.Rect(dialogX, dialogY, dialogWidth, dialogHeight) |
| 416 |
scrw.SetContentRect(dialogBoxStyle.Render(dialogUI), box) |
| 417 |
scr.Render() |
| 418 |
scr.Flush() //nolint:errcheck,gosec |
| 419 |
} |
| 420 |
|
| 421 |
// First render |
| 422 |
render() |
| 423 |
|
| 424 |
for { |
| 425 |
evs, err := drv.ReadEvents() |
| 426 |
if err != nil { |
| 427 |
log.Fatalf("reading events: %v", err) |
| 428 |
} |
| 429 |
|
| 430 |
for _, ev := range evs { |
| 431 |
switch ev := ev.(type) { |
| 432 |
case input.WindowSizeEvent: |
| 433 |
scr.Resize(ev.Width, ev.Height) |
| 434 |
case input.MouseClickEvent: |
| 435 |
dialogX, dialogY = ev.X, ev.Y |
| 436 |
case input.KeyPressEvent: |
| 437 |
switch ev.String() { |
| 438 |
case "ctrl+c", "q": |
| 439 |
return |
| 440 |
case "left", "h": |
| 441 |
dialogX-- |
| 442 |
case "down", "j": |
| 443 |
dialogY++ |
| 444 |
case "up", "k": |
| 445 |
dialogY-- |
| 446 |
case "right", "l": |
| 447 |
dialogX++ |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
render() |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
func colorGrid(xSteps, ySteps int) [][]string { |
| 457 |
x0y0, _ := colorful.Hex("#F25D94") |
| 458 |
x1y0, _ := colorful.Hex("#EDFF82") |
| 459 |
x0y1, _ := colorful.Hex("#643AFF") |
| 460 |
x1y1, _ := colorful.Hex("#14F9D5") |
| 461 |
|
| 462 |
x0 := make([]colorful.Color, ySteps) |
| 463 |
for i := range x0 { |
| 464 |
x0[i] = x0y0.BlendLuv(x0y1, float64(i)/float64(ySteps)) |
| 465 |
} |
| 466 |
|
| 467 |
x1 := make([]colorful.Color, ySteps) |
| 468 |
for i := range x1 { |
| 469 |
x1[i] = x1y0.BlendLuv(x1y1, float64(i)/float64(ySteps)) |
| 470 |
} |
| 471 |
|
| 472 |
grid := make([][]string, ySteps) |
| 473 |
for x := range ySteps { |
| 474 |
y0 := x0[x] |
| 475 |
grid[x] = make([]string, xSteps) |
| 476 |
for y := range xSteps { |
| 477 |
grid[x][y] = y0.BlendLuv(x1[x], float64(y)/float64(xSteps)).Hex() |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
return grid |
| 482 |
} |
| 483 |
|
| 484 |
// applyGradient applies a gradient to the given string string. |
| 485 |
func applyGradient(base lipgloss.Style, input string, from, to color.Color) string { |
| 486 |
// We want to get the graphemes of the input string, which is the number of |
| 487 |
// characters as a human would see them. |
| 488 |
// |
| 489 |
// We definitely don't want to use len(), because that returns the |
| 490 |
// bytes. The rune count would get us closer but there are times, like with |
| 491 |
// emojis, where the rune count is greater than the number of actual |
| 492 |
// characters. |
| 493 |
g := uniseg.NewGraphemes(input) |
| 494 |
var chars []string |
| 495 |
for g.Next() { |
| 496 |
chars = append(chars, g.Str()) |
| 497 |
} |
| 498 |
|
| 499 |
// Genrate the blend. |
| 500 |
a, _ := colorful.MakeColor(to) |
| 501 |
b, _ := colorful.MakeColor(from) |
| 502 |
var output strings.Builder |
| 503 |
var hex string |
| 504 |
for i := range chars { |
| 505 |
hex = a.BlendLuv(b, float64(i)/float64(len(chars)-1)).Hex() |
| 506 |
output.WriteString(base.Foreground(lipgloss.Color(hex)).Render(chars[i])) |
| 507 |
} |
| 508 |
|
| 509 |
return output.String() |
| 510 |
} |
| 511 |
|
| 512 |
func init() { |
| 513 |
f, err := os.OpenFile("layout.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666) //nolint:gosec |
| 514 |
if err != nil { |
| 515 |
log.Fatal(err) |
| 516 |
} |
| 517 |
log.SetOutput(f) |
| 518 |
} |