list

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 4 Imported by: 14

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alphabet

func Alphabet(_ Items, i int) string

Alphabet is the enumeration for alphabetical listing.

Example:
  a. Foo
  b. Bar
  c. Baz
  d. Qux.

func Arabic

func Arabic(_ Items, i int) string

Arabic is the enumeration for arabic numerals listing.

Example:
  1. Foo
  2. Bar
  3. Baz
  4. Qux.

func Asterisk

func Asterisk(Items, int) string

Asterisk is an enumeration using asterisks.

Example:
  * Foo
  * Bar
  * Baz
  * Qux.

func Bullet

func Bullet(Items, int) string

Bullet is the enumeration for bullet listing.

Example:
  • Foo
  • Bar
  • Baz
  • Qux.

func Dash

func Dash(Items, int) string

Dash is an enumeration using dashes.

Example:
  - Foo
  - Bar
  - Baz
  - Qux.

func Roman

func Roman(_ Items, i int) string

Roman is the enumeration for roman numerals listing.

Example:
  I. Foo
 II. Bar
III. Baz
 IV. Qux.

Types

type Enumerator

type Enumerator func(items Items, index int) string

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

type Indenter func(items Items, index int) string

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 Items

type Items tree.Children

Items represents the list items.

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

func New(items ...any) *List

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

func (l *List) EnumeratorStyle(style lipgloss.Style) *List

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

func (l *List) EnumeratorStyleFunc(f StyleFunc) *List

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) Hidden

func (l *List) Hidden() bool

Hidden returns whether this list is hidden.

func (*List) Hide

func (l *List) Hide(hide bool) *List

Hide hides this list. If this list is hidden, it will not be shown when rendered.

func (*List) Indenter added in v0.12.0

func (l *List) Indenter(indenter Indenter) *List

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

func (l *List) Item(item any) *List

Item appends an item to the list.

l := list.New().
	Item("Foo").
	Item("Bar").
	Item("Baz")

func (*List) ItemStyle

func (l *List) ItemStyle(style lipgloss.Style) *List

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

func (l *List) ItemStyleFunc(f StyleFunc) *List

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

func (l *List) Items(items ...any) *List

Items appends multiple items to the list.

l := list.New().
	Items("Foo", "Bar", "Baz"),

func (*List) Offset

func (l *List) Offset(start, end int) *List

Offset sets the start and end offset for the list.

Example:
	l := list.New("A", "B", "C", "D").
		Offset(1, -1)

fmt.Println(l)

 • B
 • C
 • D

func (*List) String

func (l *List) String() string

func (*List) Value

func (l *List) Value() string

Value returns the value of this node.

type StyleFunc

type StyleFunc func(items Items, index int) lipgloss.Style

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
		}
})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL