promptui icon indicating copy to clipboard operation
promptui copied to clipboard

Using arrays or other data structures

Open mikyll opened this issue 4 years ago • 0 comments

Hi! I'll start saying that your package is amazing and I found it pretty useful.

I was wondering: by any chance, is there a way to use a property of array type, inside the struct used for the items of the Select, and get a specific element from it?

Here's an example, extended from main.go:

type pepper struct {
	Name     string
	HeatUnit int
	Peppers  int
	ArrayValue [5]int  //<----- Array type property
}

func main() {
	[...]

	templates := &promptui.SelectTemplates{
		Label:    "{{ . }}?",
		Active:   "\U0001F336 {{ .Name | cyan }} ({{ .HeatUnit | red }})",
		Inactive: "  {{ .Name | cyan }} ({{ .HeatUnit | red }})",
		Selected: "\U0001F336 {{ .Name | red | cyan }}",
		Details: `
--------- Pepper ----------
{{ "Name:" | faint }}	{{ .Name }}
{{ "Heat Unit:" | faint }}	{{ .HeatUnit }}
{{ "Peppers:" | faint }}	{{ .Peppers }}`,
{{ "ArrayValueAt2:" | faint }}	{{ .ArrayValue[2] }}  //<----- something like this?
	}

	[...]
}

I searched a lot (even on text/template package), but couldn't find the answer. I also hope I've made it clear enough, my English isn't the best and I'm still learning 😅

mikyll avatar Jan 08 '22 23:01 mikyll