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 hide empty #673

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down