Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(list): fix list display area overflow. #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ func (m *Model) updateKeybindings() {
func (m *Model) updatePagination() {
index := m.Index()
availHeight := m.height
const paginationDefaultHeight = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the values hardcoded leaks paginationView() implementation details and is brittle in at least two ways:

  • It may break if paginationView() is changed in the future.
  • It may break if the user changes the PaginationStyle or ArabicPagination styles (e.g. extra top or bottom margin is added).

Calculating the available height here seems to be quite the chicken and egg conundrum and I do not have better suggestions ATM 🤯 🤷

FWIW, it might be helpful to add some tests while fixing this problem to have more certainty about all possible cases in the future. I've used something like this to test locally, maybe it will be useful:

func prepareListModel(m *Model) {
	m.SetShowFilter(false)
	m.SetShowStatusBar(false)
	m.SetShowTitle(false)
	m.SetShowHelp(false)
	m.SetShowPagination(true)
	// Remove the padding on the left.
	m.Styles.TitleBar.UnsetPadding()
	// Use arabic pagination for simpler output.
	m.Paginator.Type = paginator.Arabic
}

func handleCmd(m Model, cmd tea.Cmd) Model {
	for cmd != nil {
		m, cmd = m.Update(cmd)
	}

	return m
}

func expectView(t *testing.T, m Model, expected string) {
	t.Helper()

	actual := m.View()
	if expected != actual {
		t.Fatalf("Error: expected %q, got %q", expected, actual)
	}
}

func TestUpdatePagination(t *testing.T) {
	list := New([]Item{item("a"), item("b"), item("c")}, itemDelegate{}, 10, 4)
	prepareListModel(&list)

	expectView(t, list, "TODO")

	cmd := list.InsertItem(len(list.Items()), item("d"))
	list = handleCmd(list, cmd)

	expectView(t, list, "TODO")
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review!
Indeed, I assumed that paginationView() always returns "" in the case of pagination disabled for the future.

Yes, I'm struggling with that very point.

Calculating the available height here seems to be quite the chicken and egg conundrum

Now I'm thinking that resetting paginator property for each time runs of updatePagination() is not bad idea.
This culculates paginationDefaultHeight each time.
Performance will not be that different.

func (m *Model) updatePagination() {
	index := m.Index()
	availHeight := m.height

	// Reset paginator
	m.Paginator.Page = 0
	m.Paginator.PerPage = len(m.VisibleItems())
	m.Paginator.SetTotalPages(len(m.VisibleItems()))
	paginationDefaultHeight := lipgloss.Height(m.paginationView())

	if m.showTitle || (m.showFilter && m.filteringEnabled) {
		availHeight -= lipgloss.Height(m.titleView())
	}
	if m.showStatusBar {
		availHeight -= lipgloss.Height(m.statusView())
	}
	if m.showPagination {
		availHeight -= paginationDefaultHeight
	}
	if m.showHelp {
		availHeight -= lipgloss.Height(m.helpView())
	}

	updatePages := func(availHeight int) {
		m.Paginator.PerPage = max(1, availHeight/(m.delegate.Height()+m.delegate.Spacing()))

		if pages := len(m.VisibleItems()); pages < 1 {
			m.Paginator.SetTotalPages(1)
		} else {
			m.Paginator.SetTotalPages(pages)
		}
	}

	updatePages(availHeight)

	// If pagination is needed, recompute items alignment
	paginationHeight := lipgloss.Height(m.paginationView())
	if m.showPagination && paginationDefaultHeight != paginationHeight {
		availHeight += paginationDefaultHeight
		availHeight -= paginationHeight
		updatePages(availHeight)
	}

	// Restore index
	m.Paginator.Page = index / m.Paginator.PerPage
	m.cursor = index % m.Paginator.PerPage

	// Make sure the page stays in bounds
	if m.Paginator.Page >= m.Paginator.TotalPages-1 {
		m.Paginator.Page = max(0, m.Paginator.TotalPages-1)
	}
}


if m.showTitle || (m.showFilter && m.filteringEnabled) {
availHeight -= lipgloss.Height(m.titleView())
Expand All @@ -736,18 +737,31 @@ func (m *Model) updatePagination() {
availHeight -= lipgloss.Height(m.statusView())
}
if m.showPagination {
availHeight -= lipgloss.Height(m.paginationView())
// First assume that pagination will not needed
availHeight -= paginationDefaultHeight
}
if m.showHelp {
availHeight -= lipgloss.Height(m.helpView())
}

m.Paginator.PerPage = max(1, availHeight/(m.delegate.Height()+m.delegate.Spacing()))
updatePages := func(availHeight int) {
m.Paginator.PerPage = max(1, availHeight/(m.delegate.Height()+m.delegate.Spacing()))

if pages := len(m.VisibleItems()); pages < 1 {
m.Paginator.SetTotalPages(1)
} else {
m.Paginator.SetTotalPages(pages)
if pages := len(m.VisibleItems()); pages < 1 {
m.Paginator.SetTotalPages(1)
} else {
m.Paginator.SetTotalPages(pages)
}
}

updatePages(availHeight)

// If pagination is needed, recompute items alignment
paginationHeight := lipgloss.Height(m.paginationView())
if m.showPagination && 2 < paginationHeight {
availHeight += paginationDefaultHeight
availHeight -= paginationHeight
updatePages(availHeight)
}

// Restore index
Expand Down