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

feat(select): custom option renderer #424

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MultiSelect[T comparable] struct {
title Eval[string]
description Eval[string]
options Eval[[]Option[T]]
renderOptionsFn OptionRenderer[T]
filterable bool
filteredOptions []Option[T]
limit int
Expand Down Expand Up @@ -156,6 +157,12 @@ func (m *MultiSelect[T]) OptionsFunc(f func() []Option[T], bindings any) *MultiS
return m
}

// OptionsRenderFunc sets the function to render the options.
func (m *MultiSelect[T]) OptionsRenderFunc(f OptionRenderer[T]) *MultiSelect[T] {
m.renderOptionsFn = f
return m
}

// Filterable sets the multi-select field as filterable.
func (m *MultiSelect[T]) Filterable(filterable bool) *MultiSelect[T] {
m.filterable = filterable
Expand Down Expand Up @@ -556,7 +563,9 @@ func (m *MultiSelect[T]) optionsView() string {
sb.WriteString(strings.Repeat(" ", lipgloss.Width(c)))
}

if m.filteredOptions[i].selected {
if m.renderOptionsFn != nil {
sb.WriteString(m.renderOptionsFn(option))
} else if m.filteredOptions[i].selected {
sb.WriteString(styles.SelectedPrefix.String())
sb.WriteString(styles.SelectedOption.Render(option.Key))
} else {
Expand Down
16 changes: 14 additions & 2 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Select[T comparable] struct {
description Eval[string]
options Eval[[]Option[T]]
filteredOptions []Option[T]
renderOptionsFn OptionRenderer[T]

validate func(T) error
err error
Expand Down Expand Up @@ -227,6 +228,11 @@ func (s *Select[T]) OptionsFunc(f func() []Option[T], bindings any) *Select[T] {
return s
}

func (s *Select[T]) OptionsRenderFunc(f OptionRenderer[T]) *Select[T] {
s.renderOptionsFn = f
return s
}

// Inline sets whether the select input should be inline.
func (s *Select[T]) Inline(v bool) *Select[T] {
s.inline = v
Expand Down Expand Up @@ -562,7 +568,11 @@ func (s *Select[T]) optionsView() string {
if s.inline {
sb.WriteString(styles.PrevIndicator.Faint(s.selected <= 0).String())
if len(s.filteredOptions) > 0 {
sb.WriteString(styles.SelectedOption.Render(s.filteredOptions[s.selected].Key))
if s.renderOptionsFn != nil {
sb.WriteString(s.renderOptionsFn(s.filteredOptions[s.selected]))
} else {
sb.WriteString(styles.SelectedOption.Render(s.filteredOptions[s.selected].Key))
}
} else {
sb.WriteString(styles.TextInput.Placeholder.Render("No matches"))
}
Expand All @@ -571,7 +581,9 @@ func (s *Select[T]) optionsView() string {
}

for i, option := range s.filteredOptions {
if s.selected == i {
if s.renderOptionsFn != nil {
sb.WriteString(s.renderOptionsFn(option))
} else if s.selected == i {
sb.WriteString(c + styles.SelectedOption.Render(option.Key))
} else {
sb.WriteString(strings.Repeat(" ", lipgloss.Width(c)) + styles.UnselectedOption.Render(option.Key))
Expand Down
109 changes: 109 additions & 0 deletions huh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,76 @@ func TestSelect(t *testing.T) {
}
}

func TestSelectWithCustomOptionRendererFunc(t *testing.T) {
customOptionRenderer := func(option Option[string]) string {
return fmt.Sprintf("%s (%s)", option.Key, option.Value)
}

field := NewSelect[string]().
Options(NewOptions("Foo", "Bar", "Baz")...).
Title("Which one?").
OptionsRenderFunc(customOptionRenderer)

f := NewForm(NewGroup(field))
f.Update(f.Init())

view := ansi.Strip(f.View())

if !strings.Contains(view, "Foo (Foo)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo (Foo).")
}

if !strings.Contains(view, "Bar (Bar)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Bar (Bar).")
}

if !strings.Contains(view, "Baz (Baz)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Baz (Baz).")
}
}

func TestSelectWithCustomOptionRendererFuncInline(t *testing.T) {
customOptionRenderer := func(option Option[string]) string {
return fmt.Sprintf("%s (%s)", option.Key, option.Value)
}

field := NewSelect[string]().
Options(NewOptions("Foo", "Bar", "Baz")...).
Title("Which one?").
OptionsRenderFunc(customOptionRenderer).
Inline(true)

f := NewForm(NewGroup(field))
f.Update(f.Init())

view := ansi.Strip(f.View())

if !strings.Contains(view, "Foo (Foo)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo (Foo).")
}

m, _ := f.Update(tea.KeyMsg{Type: tea.KeyRight})

view = ansi.Strip(m.View())

if !strings.Contains(view, "Bar (Bar)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Bar (Bar).")
}

m, _ = f.Update(tea.KeyMsg{Type: tea.KeyRight})

view = ansi.Strip(m.View())
if !strings.Contains(view, "Baz (Baz)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Baz (Baz).")
}
}

func TestMultiSelect(t *testing.T) {
field := NewMultiSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?")
f := NewForm(NewGroup(field))
Expand Down Expand Up @@ -543,6 +613,45 @@ func TestMultiSelect(t *testing.T) {
}
}

func TestMultiSelectWithCustomOptionRendererFunc(t *testing.T) {
customOptionRenderer := func(option Option[string]) string {
icon := "[✓]"
if !option.IsSelected() {
icon = "[ ]"
}
return fmt.Sprintf("%s %s (%s)", icon, option.Key, option.Value)
}

field := NewMultiSelect[string]().
Options(
NewOption("Foo", "Foo").Selected(true),
NewOption("Bar", "Bar"),
NewOption("Baz", "Baz").Selected(true),
).
Title("Which one?").
OptionsRenderFunc(customOptionRenderer)

f := NewForm(NewGroup(field))
f.Update(f.Init())

view := ansi.Strip(f.View())

if !strings.Contains(view, "[✓] Foo (Foo)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo (Foo).")
}

if !strings.Contains(view, "[ ] Bar (Bar)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Bar (Bar).")
}

if !strings.Contains(view, "[✓] Baz (Baz)") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Baz (Baz).")
}
}

func TestMultiSelectFiltering(t *testing.T) {
tests := []struct {
name string
Expand Down
12 changes: 11 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package huh

import "fmt"
import (
"fmt"
)

// Option is an option for select fields.
type Option[T comparable] struct {
Expand Down Expand Up @@ -32,7 +34,15 @@ func (o Option[T]) Selected(selected bool) Option[T] {
return o
}

// IsSelected returns whether the option is currently selected.
func (o Option[T]) IsSelected() bool {
return o.selected
}

// String returns the key of the option.
func (o Option[T]) String() string {
return o.Key
}

// OptionRenderer is a function that is responsible for rendering an option.
type OptionRenderer[T comparable] func(option Option[T]) string