Skip to content

Commit

Permalink
Merge pull request #39 from koki-develop/items-view-performance
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Apr 7, 2023
2 parents e755eff + 9fe16b7 commit fb3c42b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
51 changes: 22 additions & 29 deletions model.go
Expand Up @@ -187,45 +187,36 @@ func (m *model) inputHeight() int {
return lipgloss.Height(m.inputView())
}

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

inputHeight := m.inputHeight()
func (m *model) itemsHeight() int {
return min(m.windowHeight-m.inputHeight(), len(m.matches))
}

func (m *model) itemsView() string {
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
m.writeItem(&v, 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
}

match := m.matches[i]
cursorLine := m.cursorPosition == i
m.writeItem(&v, match, 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) writeItem(v *strings.Builder, match Match, cursorLine bool) {
func (m *model) itemView(match Match, cursorLine bool) string {
var v strings.Builder

// write cursor
if cursorLine {
_, _ = v.WriteString(m.cursor)
Expand Down Expand Up @@ -262,6 +253,8 @@ func (m *model) writeItem(v *strings.Builder, match Match, cursorLine bool) {
_, _ = v.WriteRune(c)
}
}

return v.String()
}

/*
Expand Down
8 changes: 8 additions & 0 deletions util.go
Expand Up @@ -36,3 +36,11 @@ func max(l, r int) int {
return l
}
}

func min(l, r int) int {
if l > r {
return r
} else {
return l
}
}

0 comments on commit fb3c42b

Please sign in to comment.