Skip to content

Commit

Permalink
♻️ Refactor filter list component
Browse files Browse the repository at this point in the history
Refactor filter list to not require configuration options when
instantiating.

These changes were necessary as future requirements would increase the
function signature arguments beyond a reasonable size.
  • Loading branch information
mikelorant committed Feb 28, 2023
1 parent 47551d1 commit d0d19ba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
28 changes: 19 additions & 9 deletions internal/ui/filterlist/filterlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ type Model struct {
}

const (
defaultHeight = 2
defaultWidth = 68
defaultCharLimit = 20
)

func New(items []list.Item, prompt string, h int, state *commit.State) Model {
func New(state *commit.State) Model {
m := Model{
PromptText: prompt,
Height: h,
Width: defaultWidth,
CharLimit: defaultCharLimit,
state: state,
styles: defaultStyles(state.Theme),
items: items,
Height: defaultHeight,
Width: defaultWidth,
CharLimit: defaultCharLimit,
state: state,
styles: defaultStyles(state.Theme),
}

m.list = m.newList()
Expand Down Expand Up @@ -152,8 +151,19 @@ func (m *Model) SetItems(i []list.Item) tea.Cmd {
return m.list.SetItems(i)
}

func (m *Model) SetHeight(h int) {
m.Height = h
m.list.SetHeight(h)
}

func (m *Model) SetPromptText(txt string) {
m.PromptText = txt
m.textInput.Width = m.Width - lipgloss.Width(m.PromptText)
m.styleTextInput(&m.textInput)
}

func (m Model) newList() list.Model {
l := list.New(m.items, list.NewDefaultDelegate(), m.Width, m.Height)
l := list.New([]list.Item{}, list.NewDefaultDelegate(), m.Width, m.Height)
l.SetShowHelp(false)
l.SetShowPagination(false)
l.SetShowStatusBar(false)
Expand Down
17 changes: 16 additions & 1 deletion internal/ui/filterlist/filterlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,24 @@ func TestModel(t *testing.T) {
Theme: theme.New(config.ColourAdaptive),
}

m := filterlist.New(castToListItems(tt.args.items), tt.args.title, tt.args.height, state)
m := filterlist.New(state)
m, _ = filterlist.ToModel(m.Update(nil))

if len(tt.args.items) > 0 {
m.SetItems(castToListItems(tt.args.items))
m, _ = filterlist.ToModel(m.Update(nil))
}

if tt.args.height != 0 {
m.SetHeight(tt.args.height)
m, _ = filterlist.ToModel(m.Update(nil))
}

if tt.args.title != "" {
m.SetPromptText(tt.args.title)
m, _ = filterlist.ToModel(m.Update(nil))
}

if tt.args.model != nil {
m = tt.args.model(m)
}
Expand Down
11 changes: 5 additions & 6 deletions internal/ui/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ func New(state *commit.State) Model {
state: state,
styles: defaultStyles(state.Theme),
summaryInput: summaryInput(state),
filterList: filterlist.New(
castToListItems(state.Emojis.Emojis, WithCompatibility(state.Config.View.Compatibility)),
filterPromptText,
filterHeight,
state,
),
filterList: filterlist.New(state),
}

m.filterList.SetItems(castToListItems(state.Emojis.Emojis, WithCompatibility(state.Config.View.Compatibility)))
m.filterList.SetHeight(filterHeight)
m.filterList.SetPromptText(filterPromptText)

return m
}

Expand Down
11 changes: 5 additions & 6 deletions internal/ui/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,13 @@ func New(state *commit.State) Model {
Authors: authors,
state: state,
styles: defaultStyles(state.Theme),
filterList: filterlist.New(
castToListItems(state.Repository.Users),
filterPromptText,
filterHeight,
state,
),
filterList: filterlist.New(state),
}

m.filterList.SetItems(castToListItems(state.Repository.Users))
m.filterList.SetHeight(filterHeight)
m.filterList.SetPromptText(filterPromptText)

if state.Options.Amend {
m.Hash = state.Repository.Head.Hash
m.Date = state.Repository.Head.When.Format(dateTimeFormat)
Expand Down

0 comments on commit d0d19ba

Please sign in to comment.