Render prints an item.
(w io.Writer, m Model, index int, item Item)
| 126 | |
| 127 | // Render prints an item. |
| 128 | func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item) { |
| 129 | var ( |
| 130 | title, desc string |
| 131 | matchedRunes []int |
| 132 | s = &d.Styles |
| 133 | ) |
| 134 | |
| 135 | if i, ok := item.(DefaultItem); ok { |
| 136 | title = i.Title() |
| 137 | desc = i.Description() |
| 138 | } else { |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | // Prevent text from exceeding list width |
| 143 | if m.width > 0 { |
| 144 | textwidth := uint(m.width - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight()) |
| 145 | title = truncate.StringWithTail(title, textwidth, ellipsis) |
| 146 | desc = truncate.StringWithTail(desc, textwidth, ellipsis) |
| 147 | } |
| 148 | |
| 149 | // Conditions |
| 150 | var ( |
| 151 | isSelected = index == m.Index() |
| 152 | emptyFilter = m.FilterState() == Filtering && m.FilterValue() == "" |
| 153 | isFiltered = m.FilterState() == Filtering || m.FilterState() == FilterApplied |
| 154 | ) |
| 155 | |
| 156 | if isFiltered && index < len(m.filteredItems) { |
| 157 | // Get indices of matched characters |
| 158 | matchedRunes = m.MatchesForItem(index) |
| 159 | } |
| 160 | |
| 161 | if emptyFilter { |
| 162 | title = s.DimmedTitle.Render(title) |
| 163 | desc = s.DimmedDesc.Render(desc) |
| 164 | } else if isSelected && m.FilterState() != Filtering { |
| 165 | if isFiltered { |
| 166 | // Highlight matches |
| 167 | unmatched := s.SelectedTitle.Inline(true) |
| 168 | matched := unmatched.Copy().Inherit(s.FilterMatch) |
| 169 | title = lipgloss.StyleRunes(title, matchedRunes, matched, unmatched) |
| 170 | } |
| 171 | title = s.SelectedTitle.Render(title) |
| 172 | desc = s.SelectedDesc.Render(desc) |
| 173 | } else { |
| 174 | if isFiltered { |
| 175 | // Highlight matches |
| 176 | unmatched := s.NormalTitle.Inline(true) |
| 177 | matched := unmatched.Copy().Inherit(s.FilterMatch) |
| 178 | title = lipgloss.StyleRunes(title, matchedRunes, matched, unmatched) |
| 179 | } |
| 180 | title = s.NormalTitle.Render(title) |
| 181 | desc = s.NormalDesc.Render(desc) |
| 182 | } |
| 183 | |
| 184 | if d.ShowDescription { |
| 185 | fmt.Fprintf(w, "%s\n%s", title, desc) |
nothing calls this directly
no test coverage detected