Documentation ¶
Overview ¶
Package list allows you to build lists, as simple or complicated as you need.
Simply, define a list with some items and set it's rendering properties, like enumerator and styling:
groceries := list.New( "Bananas", "Barley", "Cashews", "Milk", list.New( "Almond Milk" "Coconut Milk" "Full Fat Milk" ) "Eggs", "Fish Cake", "Leeks", "Papaya", ) fmt.Println(groceries)
Index ¶
- func Alphabet(_ Items, i int) string
- func Arabic(_ Items, i int) string
- func Asterisk(Items, int) string
- func Bullet(Items, int) string
- func Dash(Items, int) string
- func Roman(_ Items, i int) string
- type Enumerator
- type Indenter
- type Items
- type List
- func (l *List) Enumerator(enumerator Enumerator) *List
- func (l *List) EnumeratorStyle(style lipgloss.Style) *List
- func (l *List) EnumeratorStyleFunc(f StyleFunc) *List
- func (l *List) Hidden() bool
- func (l *List) Hide(hide bool) *List
- func (l *List) Indenter(indenter Indenter) *List
- func (l *List) Item(item any) *List
- func (l *List) ItemStyle(style lipgloss.Style) *List
- func (l *List) ItemStyleFunc(f StyleFunc) *List
- func (l *List) Items(items ...any) *List
- func (l *List) Offset(start, end int) *List
- func (l *List) String() string
- func (l *List) Value() string
- type StyleFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alphabet ¶
Alphabet is the enumeration for alphabetical listing.
Example: a. Foo b. Bar c. Baz d. Qux.
func Arabic ¶
Arabic is the enumeration for arabic numerals listing.
Example: 1. Foo 2. Bar 3. Baz 4. Qux.
Types ¶
type Enumerator ¶
Enumerator enumerates a list. Given a list of items and the index of the current enumeration, it returns the prefix that should be displayed for the current item.
For example, a simple Arabic numeral enumeration would be:
func Arabic(_ Items, i int) string { return fmt.Sprintf("%d.", i+1) }
There are several predefined enumerators:
- Alphabet
- Arabic
- Bullet
- Dash
- Roman
Or, define your own.
type Indenter ¶ added in v0.12.0
Indenter indents the children of a tree.
Indenters allow for displaying nested tree items with connecting borders to sibling nodes.
For example, the default indenter would be:
func TreeIndenter(children Children, index int) string { if children.Length()-1 == index { return "│ " } return " " }
type List ¶
type List struct {
// contains filtered or unexported fields
}
List represents a list of items that can be displayed. Lists can contain lists as items, they will be rendered as nested (sub)lists.
In fact, lists can contain anything as items, like lipgloss.Table or lipgloss.Tree.
func New ¶
New returns a new list with the given items.
alphabet := list.New( "A", "B", "C", "D", "E", "F", ... )
Items can be other lists, trees, tables, rendered markdown; anything you want, really.
func (*List) Enumerator ¶
func (l *List) Enumerator(enumerator Enumerator) *List
Enumerator sets the list enumerator.
There are several predefined enumerators: • Alphabet • Arabic • Bullet • Dash • Roman
Or, define your own.
func echoEnumerator(items list.Items, i int) string { return items.At(i).Value() + ". " } l := list.New("Foo", "Bar", "Baz").Enumerator(echoEnumerator) fmt.Println(l) Foo. Foo Bar. Bar Baz. Baz
func (*List) EnumeratorStyle ¶
EnumeratorStyle sets the enumerator style for all enumerators.
To set the enumerator style conditionally based on the item value or index, use [EnumeratorStyleFunc].
func (*List) EnumeratorStyleFunc ¶
EnumeratorStyleFunc sets the enumerator style function for the list items.
Use this to conditionally set different styles based on the current items, sibling items, or index values (i.e. even or odd).
Example:
l := list.New(). EnumeratorStyleFunc(func(_ list.Items, i int) lipgloss.Style { if selected == i { return lipgloss.NewStyle().Foreground(brightPink) } return lipgloss.NewStyle() })
func (*List) Hide ¶
Hide hides this list. If this list is hidden, it will not be shown when rendered.
func (*List) Indenter ¶ added in v0.12.0
Indenter sets the indenter implementation. This is used to change the way the tree is indented. The default indentor places a border connecting sibling elements and no border for the last child.
└── Foo └── Bar └── Baz └── Qux └── Quux
You can define your own indenter.
func ArrowIndenter(children tree.Children, index int) string { return "→ " } → Foo → → Bar → → → Baz → → → → Qux → → → → → Quux
func (*List) Item ¶
Item appends an item to the list.
l := list.New(). Item("Foo"). Item("Bar"). Item("Baz")
func (*List) ItemStyle ¶
ItemStyle sets the item style for all items.
To set the item style conditionally based on the item value or index, use [ItemStyleFunc].
func (*List) ItemStyleFunc ¶
ItemStyleFunc sets the item style function for the list items.
Use this to conditionally set different styles based on the current items, sibling items, or index values.
Example:
l := list.New(). ItemStyleFunc(func(_ list.Items, i int) lipgloss.Style { if selected == i { return lipgloss.NewStyle().Foreground(brightPink) } return lipgloss.NewStyle() })
func (*List) Items ¶
Items appends multiple items to the list.
l := list.New(). Items("Foo", "Bar", "Baz"),
type StyleFunc ¶
StyleFunc is the style function that determines the style of an item.
It takes the list items and index of the list and determines the lipgloss Style to use for that index.
Example:
l := list.New(). Item("Red"). Item("Green"). Item("Blue"). ItemStyleFunc(func(items list.Items, i int) lipgloss.Style { switch { case i == 0: return RedStyle case i == 1: return GreenStyle case i == 2: return BlueStyle } })