charmbracelet / huh

Build terminal forms and prompts 🤷🏻‍♀️
MIT License
4.18k stars 113 forks source link

feat: add a key getter func to options #256

Closed whlit closed 2 months ago

whlit commented 4 months ago

Add a method to get the key from the value to generate the options.

type People struct {
    Name string
    Age  int
}
NewSelect[People]().Options(ToOptions(func(p People) string { return p.Name }, 
    People{Name: "Foo", Age: 20}, 
    People{Name: "Bar", Age: 25}, 
    People{Name: "Baz", Age: 23},
)...)
maaslalani commented 4 months ago

Hmmmm, looking at this now, we probably should have made Option an interface!

maaslalani commented 2 months ago

Thanks for the PR. This is a great helper function. I think to keep the API surface area of Huh? as small as possible I'd recommend we don't add this to core huh? but rather people can use this as a copy / paste helper in their own code / forms! Thank you so much for the work / contribution here. ❤️