Skip to content

Commit

Permalink
fix: initialize list with model
Browse files Browse the repository at this point in the history
Model.list didn't get properly initialized until
Model.updateWindowSize got called. In some situations
onProfilesLoaded can arrive before the terminal dimensions have
been retrieved however, and calling it will call updatePagination
which accesses the still uninitialized (nil) item delegate. This
causes a crash.

Fixes #38.
  • Loading branch information
muesli authored and orlangure committed Nov 17, 2022
1 parent ed63ca8 commit 08dd060
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ func New(opts ...Option) *Model {
activeView: activeViewList,
helpState: helpStateShort,
codeRoot: ".",
list: list.New([]list.Item{}, coverProfileDelegate{}, 0, 0),
}

m.list.Title = "Available files:"
m.list.SetShowStatusBar(true)
m.list.SetFilteringEnabled(true)
m.list.Styles.Title = titleStyle
m.list.FilterInput.PromptStyle = m.list.FilterInput.PromptStyle.Copy().Margin(1, 0, 0, 0)
m.list.Styles.PaginationStyle = paginationStyle
m.list.Styles.HelpStyle = helpStyle
m.list.Styles.StatusBar = statusBarStyle.Foreground(lipgloss.Color(styles.CurrentTheme.InactiveColor))

for _, opt := range opts {
opt(m)
}
Expand Down Expand Up @@ -152,17 +162,6 @@ func (m *Model) isErrorView() bool {
func (m *Model) updateWindowSize(width, height int) (tea.Model, tea.Cmd) {
if !m.ready {
m.code = codeview.New(width, height)

m.list = list.New([]list.Item{}, coverProfileDelegate{}, width, height-1)
m.list.Title = "Available files:"
m.list.SetShowStatusBar(true)
m.list.SetFilteringEnabled(true)
m.list.Styles.Title = titleStyle
m.list.FilterInput.PromptStyle = m.list.FilterInput.PromptStyle.Copy().Margin(1, 0, 0, 0)
m.list.Styles.PaginationStyle = paginationStyle
m.list.Styles.HelpStyle = helpStyle
m.list.Styles.StatusBar = statusBarStyle.Foreground(lipgloss.Color(styles.CurrentTheme.InactiveColor))

m.ready = true
}

Expand Down

0 comments on commit 08dd060

Please sign in to comment.