chore: conform to the proposed bubble tea API

This commit is contained in:
Ayman Bagabas
2024-05-13 14:39:34 -04:00
parent f4f674440a
commit d115a6d4d6
17 changed files with 183 additions and 194 deletions
+7 -7
View File
@@ -32,15 +32,15 @@ type DefaultItemStyles struct {
// NewDefaultItemStyles returns style definitions for a default item. See
// DefaultItemView for when these come into play.
func NewDefaultItemStyles() (s DefaultItemStyles) {
s.NormalTitle = lipgloss.NewStyle().
func NewDefaultItemStyles(ctx tea.Context) (s DefaultItemStyles) {
s.NormalTitle = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"}).
Padding(0, 0, 0, 2)
s.NormalDesc = s.NormalTitle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"})
s.SelectedTitle = lipgloss.NewStyle().
s.SelectedTitle = ctx.NewStyle().
Border(lipgloss.NormalBorder(), false, false, false, true).
BorderForeground(lipgloss.AdaptiveColor{Light: "#F793FF", Dark: "#AD58B4"}).
Foreground(lipgloss.AdaptiveColor{Light: "#EE6FF8", Dark: "#EE6FF8"}).
@@ -49,14 +49,14 @@ func NewDefaultItemStyles() (s DefaultItemStyles) {
s.SelectedDesc = s.SelectedTitle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#F793FF", Dark: "#AD58B4"})
s.DimmedTitle = lipgloss.NewStyle().
s.DimmedTitle = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"}).
Padding(0, 0, 0, 2)
s.DimmedDesc = s.DimmedTitle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#C2B8C2", Dark: "#4D4D4D"})
s.FilterMatch = lipgloss.NewStyle().Underline(true)
s.FilterMatch = ctx.NewStyle().Underline(true)
return s
}
@@ -92,10 +92,10 @@ type DefaultDelegate struct {
}
// NewDefaultDelegate creates a new delegate with default styles.
func NewDefaultDelegate() DefaultDelegate {
func NewDefaultDelegate(ctx tea.Context) DefaultDelegate {
return DefaultDelegate{
ShowDescription: true,
Styles: NewDefaultItemStyles(),
Styles: NewDefaultItemStyles(ctx),
height: 2,
spacing: 1,
}
+20 -17
View File
@@ -49,7 +49,7 @@ type ItemDelegate interface {
// loop will pass through here except when the user is setting a filter.
// Use this method to perform item-level updates appropriate to this
// delegate.
Update(msg tea.Msg, m *Model) tea.Cmd
Update(ctx tea.Context, msg tea.Msg, m *Model) tea.Cmd
}
type filteredItem struct {
@@ -192,24 +192,26 @@ type Model struct {
filteredItems filteredItems
delegate ItemDelegate
ctx tea.Context
}
// New returns a new model with sensible defaults.
func New(items []Item, delegate ItemDelegate, width, height int) Model {
styles := DefaultStyles()
func New(ctx tea.Context, items []Item, delegate ItemDelegate, width, height int) Model {
styles := DefaultStyles(ctx)
sp := spinner.New()
sp := spinner.New(ctx)
sp.Spinner = spinner.Line
sp.Style = styles.Spinner
filterInput := textinput.New()
filterInput := textinput.New(ctx)
filterInput.Prompt = "Filter: "
filterInput.PromptStyle = styles.FilterPrompt
filterInput.Cursor.Style = styles.FilterCursor
filterInput.CharLimit = 64
filterInput.Focus()
p := paginator.New()
p := paginator.New(ctx)
p.Type = paginator.Dots
p.ActiveDot = styles.ActivePaginationDot.String()
p.InactiveDot = styles.InactivePaginationDot.String()
@@ -236,7 +238,8 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model {
items: items,
Paginator: p,
spinner: sp,
Help: help.New(),
Help: help.New(ctx),
ctx: ctx,
}
m.updatePagination()
@@ -768,7 +771,7 @@ func (m *Model) hideStatusMessage() {
}
// Update is the Bubble Tea update loop.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
func (m Model) Update(ctx tea.Context, msg tea.Msg) (Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
@@ -782,7 +785,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
case spinner.TickMsg:
newSpinnerModel, cmd := m.spinner.Update(msg)
newSpinnerModel, cmd := m.spinner.Update(ctx, msg)
m.spinner = newSpinnerModel
if m.showSpinner {
cmds = append(cmds, cmd)
@@ -859,7 +862,7 @@ func (m *Model) handleBrowsing(msg tea.Msg) tea.Cmd {
}
}
cmd := m.delegate.Update(msg, m)
cmd := m.delegate.Update(m.ctx, msg, m)
cmds = append(cmds, cmd)
// Keep the index in bounds when paginating
@@ -909,7 +912,7 @@ func (m *Model) handleFiltering(msg tea.Msg) tea.Cmd {
}
// Update the filter text input component
newFilterInputModel, inputCmd := m.FilterInput.Update(msg)
newFilterInputModel, inputCmd := m.FilterInput.Update(m.ctx, msg)
filterChanged := m.FilterInput.Value() != newFilterInputModel.Value()
m.FilterInput = newFilterInputModel
cmds = append(cmds, inputCmd)
@@ -1003,7 +1006,7 @@ func (m Model) FullHelp() [][]key.Binding {
}
// View renders the component.
func (m Model) View() string {
func (m Model) View(ctx tea.Context) string {
var (
sections []string
availHeight = m.height
@@ -1033,7 +1036,7 @@ func (m Model) View() string {
availHeight -= lipgloss.Height(help)
}
content := lipgloss.NewStyle().Height(availHeight).Render(m.populatedView())
content := ctx.NewStyle().Height(availHeight).Render(m.populatedView())
sections = append(sections, content)
if m.showPagination {
@@ -1062,7 +1065,7 @@ func (m Model) titleView() string {
// If the filter's showing, draw that. Otherwise draw the title.
if m.showFilter && m.filterState == Filtering {
view += m.FilterInput.View()
view += m.FilterInput.View(m.ctx)
} else if m.showTitle {
if m.showSpinner && spinnerOnLeft {
view += spinnerView + spinnerLeftGap
@@ -1147,13 +1150,13 @@ func (m Model) paginationView() string {
return ""
}
s := m.Paginator.View()
s := m.Paginator.View(m.ctx)
// If the dot pagination is wider than the width of the window
// use the arabic paginator.
if ansi.PrintableRuneWidth(s) > m.width {
m.Paginator.Type = paginator.Arabic
s = m.Styles.ArabicPagination.Render(m.Paginator.View())
s = m.Styles.ArabicPagination.Render(m.Paginator.View(m.ctx))
}
style := m.Styles.PaginationStyle
@@ -1209,7 +1212,7 @@ func (m Model) helpView() string {
}
func (m Model) spinnerView() string {
return m.spinner.View()
return m.spinner.View(m.ctx)
}
func filterItems(m Model) tea.Cmd {
+19 -18
View File
@@ -1,6 +1,7 @@
package list
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
@@ -41,57 +42,57 @@ type Styles struct {
// DefaultStyles returns a set of default style definitions for this list
// component.
func DefaultStyles() (s Styles) {
func DefaultStyles(ctx tea.Context) (s Styles) {
verySubduedColor := lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"}
subduedColor := lipgloss.AdaptiveColor{Light: "#9B9B9B", Dark: "#5C5C5C"}
s.TitleBar = lipgloss.NewStyle().Padding(0, 0, 1, 2)
s.TitleBar = ctx.NewStyle().Padding(0, 0, 1, 2)
s.Title = lipgloss.NewStyle().
s.Title = ctx.NewStyle().
Background(lipgloss.Color("62")).
Foreground(lipgloss.Color("230")).
Padding(0, 1)
s.Spinner = lipgloss.NewStyle().
s.Spinner = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#8E8E8E", Dark: "#747373"})
s.FilterPrompt = lipgloss.NewStyle().
s.FilterPrompt = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#04B575", Dark: "#ECFD65"})
s.FilterCursor = lipgloss.NewStyle().
s.FilterCursor = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#EE6FF8", Dark: "#EE6FF8"})
s.DefaultFilterCharacterMatch = lipgloss.NewStyle().Underline(true)
s.DefaultFilterCharacterMatch = ctx.NewStyle().Underline(true)
s.StatusBar = lipgloss.NewStyle().
s.StatusBar = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"}).
Padding(0, 0, 1, 2)
s.StatusEmpty = lipgloss.NewStyle().Foreground(subduedColor)
s.StatusEmpty = ctx.NewStyle().Foreground(subduedColor)
s.StatusBarActiveFilter = lipgloss.NewStyle().
s.StatusBarActiveFilter = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"})
s.StatusBarFilterCount = lipgloss.NewStyle().Foreground(verySubduedColor)
s.StatusBarFilterCount = ctx.NewStyle().Foreground(verySubduedColor)
s.NoItems = lipgloss.NewStyle().
s.NoItems = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#909090", Dark: "#626262"})
s.ArabicPagination = lipgloss.NewStyle().Foreground(subduedColor)
s.ArabicPagination = ctx.NewStyle().Foreground(subduedColor)
s.PaginationStyle = lipgloss.NewStyle().PaddingLeft(2) //nolint:gomnd
s.PaginationStyle = ctx.NewStyle().PaddingLeft(2) //nolint:gomnd
s.HelpStyle = lipgloss.NewStyle().Padding(1, 0, 0, 2)
s.HelpStyle = ctx.NewStyle().Padding(1, 0, 0, 2)
s.ActivePaginationDot = lipgloss.NewStyle().
s.ActivePaginationDot = ctx.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"}).
SetString(bullet)
s.InactivePaginationDot = lipgloss.NewStyle().
s.InactivePaginationDot = ctx.NewStyle().
Foreground(verySubduedColor).
SetString(bullet)
s.DividerDot = lipgloss.NewStyle().
s.DividerDot = ctx.NewStyle().
Foreground(verySubduedColor).
SetString(" " + bullet + " ")