@@ -455,114 +455,9 @@ fmt.Println(t)
455
455
456
456
For more on tables see [ the docs] ( https://pkg.go.dev/github.com/charmbracelet/lipgloss?tab=doc ) and [ examples] ( https://github.com/charmbracelet/lipgloss/tree/master/examples/table ) .
457
457
458
- ## Rendering Trees
459
-
460
- Lip Gloss ships with a tree rendering sub-package.
461
-
462
- ``` go
463
- import " github.com/charmbracelet/lipgloss/tree"
464
- ```
465
-
466
- Define a new tree.
467
-
468
- ``` go
469
- t := tree.New (" root" , " child 1" , " child 2" , tree.New (" child 3" , " child 3.1" ))
470
- ```
471
-
472
- Print the tree.
473
-
474
- ``` go
475
- fmt.Println (t)
476
-
477
- // root
478
- // ├── child 1
479
- // ├── child 2
480
- // └── child 3
481
- // └── child 3.1
482
- ```
483
-
484
- ### Customization
485
-
486
- Trees can be customized via their enumeration function as well as using
487
- ` lipgloss.Style ` s.
488
-
489
- ``` go
490
- style1 := lipgloss.NewStyle ().Foreground (lipgloss.Color (" 99" )).MarginRight (1 )
491
- style2 := lipgloss.NewStyle ().Foreground (lipgloss.Color (" 10" )).MarginRight (1 )
492
-
493
- t := tree.New ().
494
- Items (
495
- " Glossier" ,
496
- " Claire’s Boutique" ,
497
- tree.New ().
498
- Root (" Nyx" ).
499
- Items (" Qux" , " Quux" ).
500
- EnumeratorStyle (style2),
501
- " Mac" ,
502
- " Milk" ,
503
- ).
504
- EnumeratorStyle (style1)
505
- ```
506
-
507
- Print the tree:
508
-
509
- <p align =" center " >
510
- <img
511
- width="600"
512
- alt="Tree example"
513
- src="https://github.com/charmbracelet/lipgloss/assets/245435/5a875269-f6d6-43fa-9916-5d8360e66964 "
514
- />
515
- </p >
516
- You may also define custom enumerator implementations:
517
-
518
- ``` go
519
- t := tree.New ().
520
- Items (
521
- " Glossier" ,
522
- " Claire’s Boutique" ,
523
- tree.New ().
524
- Root (" Nyx" ).
525
- Items (
526
- " Qux" ,
527
- " Quux" ,
528
- ),
529
- " Mac" ,
530
- " Milk" ,
531
- ).
532
- Enumerator (func (tree .Data , int ) (string , string ) {
533
- return " ->" , " ->"
534
- })
535
- ```
536
-
537
- Print the tree.
538
-
539
- <p align =" center " >
540
- <img
541
- width="600"
542
- alt="Tree example"
543
- src="https://github.com/charmbracelet/lipgloss/assets/245435/811e8b39-124f-48bb-b3dd-e015a65b1065 "
544
- />
545
- </p >
546
-
547
- ### Building
548
-
549
- If you need, you can also build trees incrementally:
550
-
551
- ``` go
552
- t := tree.New (" " )
553
-
554
- for i := 0 ; i < repeat; i++ {
555
- t.Item (" Lip Gloss" )
556
- }
557
- ```
558
-
559
-
560
458
## Rendering Lists
561
459
562
460
Lip Gloss ships with a list rendering sub-package.
563
- Implementation-wise, lists are still trees.
564
- The ` list ` package provides many common ` Enumerator ` implementations, as well as
565
- some syntactic sugar.
566
461
567
462
``` go
568
463
import " github.com/charmbracelet/lipgloss/list"
@@ -584,77 +479,89 @@ fmt.Println(l)
584
479
// • C
585
480
```
586
481
482
+ Lists have the ability to nest.
483
+
484
+ ``` go
485
+ l := list.New (
486
+ " A" , list.New (" Artichoke" ),
487
+ " B" , list.New (" Baking Flour" , " Bananas" , " Barley" , " Bean Sprouts" ),
488
+ " C" , list.New (" Cashew Apple" , " Cashews" , " Coconut Milk" , " Curry Paste" , " Currywurst" ),
489
+ " D" , list.New (" Dill" , " Dragonfruit" , " Dried Shrimp" ),
490
+ " E" , list.New (" Eggs" ),
491
+ " F" , list.New (" Fish Cake" , " Furikake" ),
492
+ " J" , list.New (" Jicama" ),
493
+ " K" , list.New (" Kohlrabi" ),
494
+ " L" , list.New (" Leeks" , " Lentils" , " Licorice Root" ),
495
+ )
496
+ ```
497
+
498
+ Print the list.
499
+
500
+ ``` go
501
+ fmt.Println (l)
502
+ ```
587
503
588
- ### Customization
504
+ <p align =" center " >
505
+ <img width =" 600 " alt =" image " src =" https://github.com/charmbracelet/lipgloss/assets/42545625/0dc9f440-0748-4151-a3b0-7dcf29dfcdb0 " >
506
+ </p >
589
507
590
508
Lists can be customized via their enumeration function as well as using
591
509
` lipgloss.Style ` s.
592
510
593
511
``` go
594
512
enumeratorStyle := lipgloss.NewStyle ().Foreground (lipgloss.Color (" 99" )).MarginRight (1 )
595
- itemStyle := lipgloss.NewStyle ().Foreground (lipgloss.Color (" 10 " )).MarginRight (1 )
513
+ itemStyle := lipgloss.NewStyle ().Foreground (lipgloss.Color (" 212 " )).MarginRight (1 )
596
514
597
515
l := list.New (
598
- " Glossier" ,
599
- " Claire’s Boutique" ,
600
- " Nyx" ,
601
- " Mac" ,
602
- " Milk" ,
516
+ " Glossier" ,
517
+ " Claire’s Boutique" ,
518
+ " Nyx" ,
519
+ " Mac" ,
520
+ " Milk" ,
603
521
).
604
- Enumerator (list.Roman ).
605
- EnumeratorStyle (enumeratorStyle).
606
- ItemStyle (itemStyle)
522
+ Enumerator (list.Roman ).
523
+ EnumeratorStyle (enumeratorStyle).
524
+ ItemStyle (itemStyle)
607
525
```
608
526
609
527
Print the list.
610
528
611
529
<p align =" center " >
612
- <img
613
- width="600"
614
- alt="List example"
615
- src="https://github.com/charmbracelet/lipgloss/assets/245435/8f5e5e0b-7bf9-4e3b-a8ba-3af10825320e "
616
- />
530
+ <img width =" 600 " alt =" List example " src =" https://github.com/charmbracelet/lipgloss/assets/42545625/360494f1-57fb-4e13-bc19-0006efe01561 " >
617
531
</p >
532
+
618
533
In addition to the predefined enumerators (` Arabic ` , ` Alphabet ` , ` Roman ` , ` Bullet ` , ` Tree ` ),
619
534
you may also define your own custom enumerator:
620
535
621
536
``` go
622
- var DuckDuckGooseEnumerator Enumerator = func (l *List, i int ) string {
623
- if l.At (i) == " Goose" {
624
- return " Honk →"
625
- }
626
- return " "
627
- }
628
- ```
537
+ l := list.New (" Duck" , " Duck" , " Duck" , " Duck" , " Goose" , " Duck" , " Duck" )
629
538
630
- Use it in a list:
539
+ func DuckDuckGooseEnumerator (l list .Items , i int ) string {
540
+ if l.At (i).Value () == " Goose" {
541
+ return " Honk →"
542
+ }
543
+ return " "
544
+ }
631
545
632
- ``` go
633
- l := list.New (" Duck" , " Duck" , " Duck" , " Duck" , " Goose" , " Duck" , " Duck" )
634
- l.Enumerator (DuckDuckGooseEnumerator)
546
+ l = l.Enumerator (DuckDuckGooseEnumerator)
635
547
```
636
548
637
549
Print the list:
638
550
639
551
<p align =" center " >
640
- <img
641
- width="600"
642
- alt="image"
643
- src="https://github.com/charmbracelet/lipgloss/assets/245435/44e37a5b-5124-4f49-a332-1756a355002e "
644
- />
552
+ <img width =" 600 " alt =" image " src =" https://github.com/charmbracelet/lipgloss/assets/42545625/157aaf30-140d-4948-9bb4-dfba46e5b87e " >
645
553
</p >
646
554
647
- ### Building
648
-
649
- If you need, you can also build trees incrementally:
555
+ If you need, you can also build lists incrementally:
650
556
651
557
``` go
652
558
l := list.New ()
653
559
654
560
for i := 0 ; i < repeat; i++ {
655
- l.Item (" Lip Gloss" )
561
+ l.Item (" Lip Gloss" )
656
562
}
657
563
```
564
+
658
565
---
659
566
660
567
## FAQ
0 commit comments