From 7f0a2582da40f8047d69e86568850c5443c4acee Mon Sep 17 00:00:00 2001 From: koki-develop Date: Fri, 7 Apr 2023 08:23:16 +0900 Subject: [PATCH 1/4] writeItem -> itemView --- model.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/model.go b/model.go index 3551526..7223803 100644 --- a/model.go +++ b/model.go @@ -200,7 +200,7 @@ func (m *model) itemsView() string { } cursorLine := m.cursorPosition == i - m.writeItem(&v, match, cursorLine) + v.WriteString(m.itemView(match, cursorLine)) if i+1-m.windowYPosition >= m.windowHeight-inputHeight { break } @@ -212,9 +212,8 @@ func (m *model) itemsView() string { continue } - match := m.matches[i] cursorLine := m.cursorPosition == i - m.writeItem(&v, match, cursorLine) + v.WriteString(m.itemView(m.matches[i], cursorLine)) if i-1 < m.windowYPosition { break } @@ -225,7 +224,9 @@ func (m *model) itemsView() string { return v.String() } -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) @@ -262,6 +263,8 @@ func (m *model) writeItem(v *strings.Builder, match Match, cursorLine bool) { _, _ = v.WriteRune(c) } } + + return v.String() } /* From 3f3fca4ff650c25c288936064aa85c5dc28bc3e1 Mon Sep 17 00:00:00 2001 From: koki-develop Date: Fri, 7 Apr 2023 08:33:35 +0900 Subject: [PATCH 2/4] implement min() --- util.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/util.go b/util.go index 8cc3d85..b5b562c 100644 --- a/util.go +++ b/util.go @@ -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 + } +} From 14e88f2ab5e85add7078d6537f0b0bc5ebdbc1b0 Mon Sep 17 00:00:00 2001 From: koki-develop Date: Fri, 7 Apr 2023 08:54:29 +0900 Subject: [PATCH 3/4] implement itemsHeight() --- model.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/model.go b/model.go index 7223803..f29c302 100644 --- a/model.go +++ b/model.go @@ -187,6 +187,10 @@ func (m *model) inputHeight() int { return lipgloss.Height(m.inputView()) } +func (m *model) itemsHeight() int { + return min(m.windowHeight-m.inputHeight(), len(m.matches)) +} + func (m *model) itemsView() string { var v strings.Builder From 9fe16b793ecbbfe7d5ca400847d63558c5942b04 Mon Sep 17 00:00:00 2001 From: koki-develop Date: Fri, 7 Apr 2023 09:13:05 +0900 Subject: [PATCH 4/4] fix itemsView --- model.go | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/model.go b/model.go index f29c302..2394ad8 100644 --- a/model.go +++ b/model.go @@ -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 {