Skip to content

Commit 5af85da

Browse files
committed
refactor(tui): move list item rendering to dedicated function
- Extract `renderItem` function to avoid function allocation on each render - Remove unnecessary string join operation - Simplify conditional rendering logic - Improve code readability and maintainability
1 parent 1d64f86 commit 5af85da

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

internal/tui/command.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ type itemDelegate struct{}
4040
func (d itemDelegate) Height() int { return 1 }
4141
func (d itemDelegate) Spacing() int { return 0 }
4242
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
43+
44+
func renderItem(str string, isSelected bool) string {
45+
if isSelected {
46+
return selectedItemStyle.Render("> " + str)
47+
}
48+
return itemStyle.Render(str)
49+
}
50+
4351
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
4452
i, ok := listItem.(SlideItem)
4553
if !ok {
4654
return
4755
}
4856

4957
str := fmt.Sprintf("%d. %s", i.number, i.title)
50-
51-
fn := itemStyle.Render
52-
if index == m.Index() {
53-
fn = func(s ...string) string {
54-
return selectedItemStyle.Render("> " + strings.Join(s, " "))
55-
}
56-
}
57-
58-
fmt.Fprint(w, fn(str))
58+
fmt.Fprint(w, renderItem(str, index == m.Index()))
5959
}
6060

6161
type Command struct {

0 commit comments

Comments
 (0)