Use setTotalPages() to set the number of items in the paginator

This takes the filtered stash state in account
This commit is contained in:
Nicolas Martin 2020-10-24 22:47:40 +02:00 committed by Christian Rocha
parent e985f96dc4
commit 259f4b584b

View file

@ -124,12 +124,19 @@ func (m *stashModel) setSize(width, height int) {
m.terminalHeight = height m.terminalHeight = height
// Update the paginator // Update the paginator
perPage := max(1, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)/stashViewItemHeight) m.setTotalPages()
m.paginator.PerPage = perPage
m.paginator.SetTotalPages(len(m.markdowns))
m.noteInput.Width = m.terminalWidth - stashViewHorizontalPadding*2 - len(setNotePromptText) m.noteInput.Width = m.terminalWidth - stashViewHorizontalPadding*2 - len(setNotePromptText)
m.searchInput.Width = m.terminalWidth - stashViewHorizontalPadding*2 - len(searchNotePromptText) m.searchInput.Width = m.terminalWidth - stashViewHorizontalPadding*2 - len(searchNotePromptText)
}
// Sets the total paginator pages according to the amount of markdowns for the
// current state.
func (m *stashModel) setTotalPages() {
pages := len(m.getNotes())
m.paginator.PerPage = max(1, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)/stashViewItemHeight)
m.paginator.SetTotalPages(pages)
// Make sure the page stays in bounds // Make sure the page stays in bounds
if m.paginator.Page >= m.paginator.TotalPages-1 { if m.paginator.Page >= m.paginator.TotalPages-1 {
@ -159,7 +166,7 @@ func (m *stashModel) addMarkdowns(mds ...*markdown) {
if len(mds) > 0 { if len(mds) > 0 {
m.markdowns = append(m.markdowns, mds...) m.markdowns = append(m.markdowns, mds...)
sort.Sort(markdownsByLocalFirst(m.markdowns)) sort.Sort(markdownsByLocalFirst(m.markdowns))
m.paginator.SetTotalPages(len(m.markdowns)) m.setTotalPages()
} }
} }
@ -417,6 +424,12 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
m.paginator.Page = m.paginator.TotalPages - 1 m.paginator.Page = m.paginator.TotalPages - 1
m.index = m.paginator.ItemsOnPage(pages) - 1 m.index = m.paginator.ItemsOnPage(pages) - 1
// esc only passed trough in stashStateSearchNotes
case "esc":
m.state = stashStateReady
m.searchInput.SetValue("") // clear the searchinput
m.setTotalPages()
// Open document // Open document
case "enter": case "enter":
m.hideStatusMessage() m.hideStatusMessage()
@ -427,8 +440,13 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
// Load the document from the server. We'll handle the message // Load the document from the server. We'll handle the message
// that comes back in the main update function. // that comes back in the main update function.
m.state = stashStateLoadingDocument
md := m.selectedMarkdown() md := m.selectedMarkdown()
m.state = stashStateLoadingDocument
// This is only relevant when "enter" was called from the
// filtered stash view
m.searchInput.SetValue("")
m.setTotalPages()
if md.markdownType == localMarkdown { if md.markdownType == localMarkdown {
cmds = append(cmds, loadLocalMarkdown(md)) cmds = append(cmds, loadLocalMarkdown(md))
@ -562,27 +580,32 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
break break
} }
i := m.markdownIndex() smd := m.selectedMarkdown()
id := m.markdowns[i].ID for i, md := range m.markdowns {
if md != smd {
continue
}
if m.markdowns[i].markdownType == convertedMarkdown { if md.markdownType == convertedMarkdown {
// If document was stashed during this session, convert it // If document was stashed during this session, convert it
// back to a local file. // back to a local file.
m.markdowns[i].markdownType = localMarkdown md.markdownType = localMarkdown
m.markdowns[i].Note = m.markdowns[i].displayPath md.Note = m.markdowns[i].displayPath
} else { }
// Delete optimistically and remove the stashed item // Delete optimistically and remove the stashed item
// before we've received a success response. // before we've received a success response.
m.markdowns = append(m.markdowns[:i], m.markdowns[i+1:]...) m.markdowns = append(m.markdowns[:i], m.markdowns[i+1:]...)
} }
// Update pagination
m.paginator.SetTotalPages(len(m.markdowns))
m.paginator.Page = min(m.paginator.Page, m.paginator.TotalPages-1)
// Set state and delete // Set state and delete
m.state = stashStateReady m.state = stashStateReady
return m, deleteStashedItem(m.cc, id) if m.searchInput.Value() != "" {
m.state = stashStateShowFiltered
}
// Update pagination
m.setTotalPages()
return m, deleteStashedItem(m.cc, smd.ID)
default: default:
m.state = stashStateReady m.state = stashStateReady
@ -620,6 +643,10 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
newSearchInputModel, cmd := textinput.Update(msg, m.searchInput) newSearchInputModel, cmd := textinput.Update(msg, m.searchInput)
m.searchInput = newSearchInputModel m.searchInput = newSearchInputModel
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
// Update pagination
m.setTotalPages()
case stashStateSettingNote: case stashStateSettingNote:
if msg, ok := msg.(tea.KeyMsg); ok { if msg, ok := msg.(tea.KeyMsg); ok {
switch msg.String() { switch msg.String() {