Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Renderer #295

Closed
wants to merge 3 commits into from
Closed

List Renderer #295

wants to merge 3 commits into from

Commits on May 15, 2024

  1. feat: list renderer

    Lip Gloss ships with a list rendering sub-package.
    
    ```go
    import "github.com/charmbracelet/lipgloss/list"
    ```
    
    Define a new list.
    
    ```go
    l := list.New("A", "B", "C")
    ```
    
    Print the list.
    
    ```go
    fmt.Println(l)
    
    // • A
    // • B
    // • C
    ```
    
    <!--
    
    Lists have the ability to nest.
    
    ```go
    l := list.New(
      "A", list.New("Artichoke"),
      "B", list.New("Baking Flour", "Bananas", "Barley", "Bean Sprouts"),
      "C", list.New("Cashew Apple", "Cashews", "Coconut Milk", "Curry Paste", "Currywurst"),
      "D", list.New("Dill", "Dragonfruit", "Dried Shrimp"),
      "E", list.New("Eggs"),
      "F", list.New("Fish Cake", "Furikake"),
      "J", list.New("Jicama"),
      "K", list.New("Kohlrabi"),
      "L", list.New("Leeks", "Lentils", "Licorice Root"),
    )
    ```
    
    Print the list.
    
    ```go
    fmt.Println(l)
    ```
    
    <p align="center">
    <img width="600" alt="image" src="https://github.com/charmbracelet/lipgloss/assets/42545625/0dc9f440-0748-4151-a3b0-7dcf29dfcdb0">
    </p>
    
    -->
    
    Lists can be customized via their enumeration function as well as using
    `lipgloss.Style`s.
    
    ```go
    enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
    itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
    
    l := list.New(
      "Glossier",
      "Claire’s Boutique",
      "Nyx",
      "Mac",
      "Milk",
    ).
      Enumerator(list.Roman).
      EnumeratorStyle(enumeratorStyle).
      ItemStyle(itemStyle)
    ```
    
    Print the list.
    
    <p align="center">
    <img width="600" alt="List example" src="https://github.com/charmbracelet/lipgloss/assets/42545625/360494f1-57fb-4e13-bc19-0006efe01561">
    </p>
    
    In addition to the predefined enumerators (`Arabic`, `Alphabet`, `Roman`, `Bullet`, `Tree`),
    you may also define your own custom enumerator:
    
    ```go
    var DuckDuckGooseEnumerator Enumerator = func(l *List, i int) string {
        if l.At(i) == "Goose" {
            return "Honk →"
        }
        return ""
    }
    ```
    
    Use it in a list:
    
    ```go
    l := list.New("Duck", "Duck", "Duck", "Duck", "Goose", "Duck", "Duck")
    l.Enumerator(DuckDuckGooseEnumerator)
    ```
    
    Print the list:
    
    <p align="center">
    <img width="600" alt="image" src="https://github.com/charmbracelet/lipgloss/assets/42545625/157aaf30-140d-4948-9bb4-dfba46e5b87e">
    </p>
    
    If you need, you can also build lists incrementally:
    
    ```go
    l := list.New()
    
    for i := 0; i < repeat; i++ {
        l.Item("Lip Gloss")
    }
    ```
    maaslalani committed May 15, 2024
    Configuration menu
    Copy the full SHA
    74a5b96 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    a9f7bc2 View commit details
    Browse the repository at this point in the history
  3. fix: apply ItemStyle

    maaslalani committed May 15, 2024
    Configuration menu
    Copy the full SHA
    a8bfc36 View commit details
    Browse the repository at this point in the history