Skip to content

Commit

Permalink
Updated keylist
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmulvaney committed Jan 3, 2025
1 parent 08a3374 commit e8cea28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/keylist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func New() (m Model) {
keylist.WithItems(keyData),
keylist.WithGrid(true),
keylist.WithSeparator(":"),
keylist.WithMaxRows(3),
)

return m
Expand Down
60 changes: 43 additions & 17 deletions keylist/keylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Styles struct {
// TODO: Should this be shared
func DefaultStyles() Styles {
return Styles{
Key: lipgloss.NewStyle().Foreground(lipgloss.Color("214")).Bold(true).PaddingRight(0),
Key: lipgloss.NewStyle().Foreground(lipgloss.Color("214")).Bold(true),
Value: lipgloss.NewStyle(),
}
}
Expand Down Expand Up @@ -86,31 +86,57 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

func (m Model) View() string {
var items []string
keyWidth := 0
var keylist string
separatorWidth := runewidth.StringWidth(m.separator) + 1
var keySets [][]string
if m.maxRows > 0 {
for i := 0; i < len(m.data); i += m.maxRows {
end := i + m.maxRows
if end > len(m.data) {
end = len(m.data)
}
set := m.renderKeySet(m.data[i:end])
keySets = append(keySets, set)
}
} else {
keySets = append(keySets, m.renderKeySet(m.data))
}
var renderedKeyList []string
for _, set := range keySets {
list := lipgloss.NewStyle().PaddingRight(1).Render(lipgloss.JoinVertical(lipgloss.Top, set...))
renderedKeyList = append(renderedKeyList, list)
}

return lipgloss.JoinHorizontal(lipgloss.Left, renderedKeyList...)
}

for _, item := range m.data {
kw := runewidth.StringWidth(item[0])
func (m *Model) renderKeySet(items [][]string) []string {
var keySet []string
sepWidth := 0
if m.separator != "" {
sepWidth = runewidth.StringWidth(m.separator)
}
keyWidth := 0
for _, item := range items {
kw := runewidth.StringWidth(item[0]) + sepWidth + 1
if kw > keyWidth-1 {
keyWidth = kw + 1 + separatorWidth
keyWidth = kw
}
}

if m.maxRows > 0 {
} else {
for _, item := range m.data {
if !m.grid {
keyWidth = runewidth.StringWidth(item[0]) + 1 + separatorWidth
}
for _, item := range items {
if !m.grid {
keyWidth = runewidth.StringWidth(item[0]) + sepWidth + 1
}

if m.separator != "" {
key := m.styles.Key.Width(keyWidth).Render(fmt.Sprintf("%s%s", item[0], m.separator))
value := m.styles.Value.Render(item[1])
items = append(items, lipgloss.JoinHorizontal(lipgloss.Left, key, value))
keySet = append(keySet, lipgloss.JoinHorizontal(lipgloss.Left, key, value))
} else {
key := m.styles.Key.Width(keyWidth).Render(item[0])
value := m.styles.Value.Render(item[1])
keySet = append(keySet, lipgloss.JoinHorizontal(lipgloss.Left, key, value))
}
keylist = lipgloss.JoinVertical(lipgloss.Top, items...)
}

return keylist
return keySet
}

0 comments on commit e8cea28

Please sign in to comment.