Skip to content

Commit

Permalink
feat: added a listStyle for the simplelist
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jan 6, 2023
1 parent df1ba68 commit b3d967b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
31 changes: 9 additions & 22 deletions internal/model/simplelist/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,19 @@ import (

// Model contains state of the list
type Model struct {
title string
height int
items []list.Item
selected int
titleStyle lipgloss.Style
noItemsStyle lipgloss.Style
itemStyle lipgloss.Style
title string
height int
items []list.Item
selected int
style listStyle
}

// New creates a new list
func New(title string, height int) Model {
return Model{
title: title,
height: height,
titleStyle: lipgloss.NewStyle().
Foreground(style.GlobalColorscheme.Color1).
MarginLeft(3).
PaddingBottom(1),
noItemsStyle: lipgloss.NewStyle().
MarginLeft(3).
Foreground(style.GlobalColorscheme.Color2).
Italic(true),
itemStyle: lipgloss.NewStyle().
MarginLeft(3).
MarginRight(3).
Foreground(style.GlobalColorscheme.Color2),
style: newDefaultListStyle(),
}
}

Expand Down Expand Up @@ -76,19 +63,19 @@ func (m Model) View() string {
sections := make([]string, 1)

// Create the title
sections = append(sections, m.titleStyle.Render(m.title))
sections = append(sections, m.style.titleStyle.Render(m.title))

// If the list is empty show a message
if len(m.items) == 0 {
sections = append(sections, m.noItemsStyle.Render("<no items>"))
sections = append(sections, m.style.noItemsStyle.Render("<no items>"))
return lipgloss.JoinVertical(lipgloss.Top, sections...)
}

// If the list has items, style them
for i, item := range m.items {
isSelected := i == m.selected
itemText := fmt.Sprintf("%s %s", style.Index(i, isSelected), item.FilterValue())
itemLine := m.itemStyle.Render(itemText)
itemLine := m.style.itemStyle.Render(itemText)
sections = append(sections, itemLine)
}

Expand Down
40 changes: 40 additions & 0 deletions internal/model/simplelist/style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package simplelist

import (
"github.com/TypicalAM/goread/internal/style"
"github.com/charmbracelet/lipgloss"
)

// listStyle is the style of the list.
type listStyle struct {
titleStyle lipgloss.Style
noItemsStyle lipgloss.Style
itemStyle lipgloss.Style
}

// newDefaultListStyle returns a new list style with default values.
func newDefaultListStyle() listStyle {
// Create the new style
newStyle := listStyle{}

// titleStyle is used to style the title of the list
newStyle.titleStyle = lipgloss.NewStyle().
Foreground(style.GlobalColorscheme.Color1).
MarginLeft(3).
PaddingBottom(1)

// newItemsStyle is used to style the message when there are no items
newStyle.noItemsStyle = lipgloss.NewStyle().
MarginLeft(3).
Foreground(style.GlobalColorscheme.Color2).
Italic(true)

// newItemsStyle is used to style the items in the list
newStyle.itemStyle = lipgloss.NewStyle().
MarginLeft(3).
MarginRight(3).
Foreground(style.GlobalColorscheme.Color2)

// Return the new style
return newStyle
}

0 comments on commit b3d967b

Please sign in to comment.