Skip to content

Commit

Permalink
refactor itemView
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Mar 25, 2023
1 parent dcbc588 commit 452d33b
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions model.go
Expand Up @@ -30,6 +30,13 @@ type model struct {
nocursor string
cursorPosition int

selectedPrefix string
unselectedPrefix string

matchesStyle lipgloss.Style
cursorLineStyle lipgloss.Style
cursorLineMatchesStyle lipgloss.Style

matches fuzzy.Matches
choices []int

Expand Down Expand Up @@ -62,6 +69,13 @@ func newModel(fzf *FZF, items *items) *model {
nocursor: strings.Repeat(" ", lipgloss.Width(fzf.option.cursor)),
cursorPosition: 0,

selectedPrefix: fzf.option.styles.option.selectedPrefix.Render(fzf.option.selectedPrefix),
unselectedPrefix: fzf.option.styles.option.unselectedPrefix.Render(fzf.option.unselectedPrefix),

matchesStyle: fzf.option.styles.option.matches,
cursorLineStyle: fzf.option.styles.option.cursorLine,
cursorLineMatchesStyle: lipgloss.NewStyle().Inherit(fzf.option.styles.option.matches).Inherit(fzf.option.styles.option.cursorLine),

matches: fuzzy.Matches{},
choices: []int{},
// window
Expand Down Expand Up @@ -96,24 +110,22 @@ func (m *model) itemsView() string {
var v strings.Builder

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

// write cursor
if currentLine {
if cursorLine {
_, _ = v.WriteString(m.cursor)
} else {
_, _ = v.WriteString(m.nocursor)
}

// write toggle
if m.fzf.multiple() {
var togglev strings.Builder
if intContains(m.choices, match.Index) {
_, _ = togglev.WriteString(m.fzf.option.styles.option.selectedPrefix.Render(m.fzf.option.selectedPrefix))
_, _ = v.WriteString(m.selectedPrefix)
} else {
_, _ = togglev.WriteString(m.fzf.option.styles.option.unselectedPrefix.Render(m.fzf.option.unselectedPrefix))
_, _ = v.WriteString(m.unselectedPrefix)
}
_, _ = v.WriteString(togglev.String())
}

// write item prefix
Expand All @@ -122,19 +134,20 @@ func (m *model) itemsView() string {
}

// write item
var itemv strings.Builder
for ci, c := range match.Str {
// matches
style := lipgloss.NewStyle()
if intContains(match.MatchedIndexes, ci) {
style = style.Inherit(m.fzf.option.styles.option.matches)
}
if m.cursorPosition == match.Index {
style = style.Inherit(m.fzf.option.styles.option.cursorLine)
if cursorLine {
_, _ = v.WriteString(m.cursorLineMatchesStyle.Render(string(c)))
} else {
_, _ = v.WriteString(m.matchesStyle.Render(string(c)))
}
} else if cursorLine {
_, _ = v.WriteString(m.cursorLineStyle.Render(string(c)))
} else {
_, _ = v.WriteRune(c)
}
_, _ = itemv.WriteString(style.Render(string(c)))
}
_, _ = v.WriteString(itemv.String())

if i+1 == m.windowHeight-headerHeight {
break
Expand Down

0 comments on commit 452d33b

Please sign in to comment.