diff --git a/list/list.go b/list/list.go index 6808142b..133e30ec 100644 --- a/list/list.go +++ b/list/list.go @@ -193,6 +193,11 @@ type Model struct { filteredItems filteredItems delegate ItemDelegate + + // Handling of empty list. This allows you + // to override the default message that is + // shown for an empty list + EmptyItemsNote func(string) string } // New returns a new model with sensible defaults. @@ -221,9 +226,9 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model { showStatusBar: true, showPagination: true, showHelp: true, + filteringEnabled: true, itemNameSingular: "item", itemNamePlural: "items", - filteringEnabled: true, KeyMap: DefaultKeyMap(), Filter: DefaultFilter, Styles: styles, @@ -238,6 +243,10 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model { Paginator: p, spinner: sp, Help: help.New(), + + EmptyItemsNote: func(itemsPlural string) string { + return fmt.Sprintf("No %s", itemsPlural) + }, } m.updatePagination() @@ -375,6 +384,12 @@ func (m Model) Items() []Item { return m.items } +// RemoveEmptyItemsNote will disable any message handler +// that is set for EmptyItemsNote +func (m *Model) RemoveEmptyItemsNote() { + m.EmptyItemsNote = nil +} + // SetItems sets the items available in the list. This returns a command. func (m *Model) SetItems(i []Item) tea.Cmd { var cmd tea.Cmd @@ -1160,9 +1175,9 @@ func (m Model) statusView() string { } else { status = itemsDisplay } - } else if len(m.items) == 0 { + } else if len(m.items) == 0 && m.EmptyItemsNote != nil { // Not filtering: no items. - status = m.Styles.StatusEmpty.Render("No " + m.itemNamePlural) + status = m.Styles.StatusEmpty.Render(m.EmptyItemsNote(m.itemNamePlural)) } else { // Normal filtered := m.FilterState() == FilterApplied @@ -1214,10 +1229,10 @@ func (m Model) populatedView() string { // Empty states if len(items) == 0 { - if m.filterState == Filtering { + if m.filterState == Filtering || m.EmptyItemsNote == nil { return "" } - return m.Styles.NoItems.Render("No " + m.itemNamePlural + ".") + return m.Styles.NoItems.Render(m.EmptyItemsNote(m.itemNamePlural + ".")) } if len(items) > 0 {