Skip to content

Commit

Permalink
fix itemsView
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Apr 7, 2023
1 parent 14e88f2 commit 9fe16b7
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions model.go
Expand Up @@ -192,40 +192,26 @@ func (m *model) itemsHeight() int {
}

func (m *model) itemsView() string {
var v strings.Builder

inputHeight := m.inputHeight()

itemsHeight := m.itemsHeight()
if itemsHeight < 1 {
return ""
}
matches := m.matches[m.windowYPosition : itemsHeight+m.windowYPosition]
rows := make([]string, len(matches))
switch m.option.inputPosition {
case InputPositionTop:
for i, match := range m.matches {
if i < m.windowYPosition {
continue
}

cursorLine := m.cursorPosition == i
v.WriteString(m.itemView(match, cursorLine))
if i+1-m.windowYPosition >= m.windowHeight-inputHeight {
break
}
v.WriteRune('\n')
for i, match := range matches {
cursorLine := m.cursorPosition == (i + m.windowYPosition)
rows[i] = m.itemView(match, cursorLine)
}
case InputPositionBottom:
for i := len(m.matches) - 1; i >= 0; i-- {
if len(m.matches)-i+m.windowHeight-inputHeight < m.windowYPosition {
continue
}

cursorLine := m.cursorPosition == i
v.WriteString(m.itemView(m.matches[i], cursorLine))
if i-1 < m.windowYPosition {
break
}
v.WriteRune('\n')
for i, match := range matches {
cursorLine := m.cursorPosition == (i + m.windowYPosition)
rows[len(matches)-1-i] = m.itemView(match, cursorLine)
}
}

return v.String()
return strings.Join(rows, "\n")
}

func (m *model) itemView(match Match, cursorLine bool) string {
Expand Down

0 comments on commit 9fe16b7

Please sign in to comment.