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

display items count #6

Merged
merged 6 commits into from Mar 26, 2023
Merged
Show file tree
Hide file tree
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
44 changes: 35 additions & 9 deletions model.go
Expand Up @@ -2,6 +2,7 @@ package fzf

import (
"fmt"
"strconv"
"strings"

"github.com/charmbracelet/bubbles/key"
Expand All @@ -15,10 +16,6 @@ var (
_ tea.Model = (*model)(nil)
)

const (
headerHeight = 1
)

type model struct {
fzf *FZF
items *items
Expand Down Expand Up @@ -103,14 +100,35 @@ func (m *model) View() string {
}

func (m *model) headerView() string {
return m.input.View()
var v strings.Builder

// input
_, _ = v.WriteString(m.input.View())
_, _ = v.WriteRune('\n')

// count
var cv strings.Builder
_, _ = cv.WriteString(strconv.Itoa(m.matches.Len()))
_, _ = cv.WriteRune('/')
_, _ = cv.WriteString(strconv.Itoa(m.items.Len()))
_, _ = cv.WriteRune(' ')
_, _ = v.WriteString(cv.String())
_, _ = v.WriteString(strings.Repeat("─", max(m.windowWidth-cv.Len(), 0)))

return v.String()
}

func (m *model) headerHeight() int {
return lipgloss.Height(m.headerView())
}

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

headerHeight := m.headerHeight()

for i, match := range m.matches[m.windowYPosition:] {
cursorLine := m.cursorPosition == i
cursorLine := m.cursorPosition == i+m.windowYPosition

// write cursor
if cursorLine {
Expand Down Expand Up @@ -207,9 +225,15 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *model) choice() {
if len(m.choices) == 0 && m.cursorPosition >= 0 {
m.choices = append(m.choices, m.matches[m.cursorPosition].Index)
if len(m.choices) > 0 {
return
}

if m.matches.Len() == 0 {
return
}

m.choices = append(m.choices, m.matches[m.cursorPosition].Index)
}

func (m *model) toggle() {
Expand Down Expand Up @@ -258,7 +282,7 @@ func (m *model) filter() {
}

func (m *model) fixCursor() {
if m.cursorPosition < 0 && len(m.matches) > 0 {
if m.cursorPosition < 0 {
m.cursorPosition = 0
return
}
Expand All @@ -270,6 +294,8 @@ func (m *model) fixCursor() {
}

func (m *model) fixYPosition() {
headerHeight := m.headerHeight()

if m.windowHeight-headerHeight > len(m.matches) {
m.windowYPosition = 0
return
Expand Down
8 changes: 8 additions & 0 deletions util.go
Expand Up @@ -28,3 +28,11 @@ func intFilter(is []int, f func(i int) bool) []int {
}
return rtn
}

func max(l, r int) int {
if l < r {
return r
} else {
return l
}
}