manifoldco / promptui

Interactive prompt for command-line applications
https://www.manifold.co
BSD 3-Clause "New" or "Revised" License
6.03k stars 333 forks source link

dynamic Details, Active, Inactive, Selected possible in SelectTemplates? #150

Closed divramod closed 10 months ago

divramod commented 4 years ago

Hey,

thx for the awesome module!

i like to ask, if it is possible to create a dynamic/conditional Details String. my code looks like that (mostly copied from the exampletemplates, i am a go beginner):

pathesChoose := &promptui.SelectTemplates{
    Label:    "{{ . }}?",
    Active:   "\U0001F336 {{ .Type | cyan }}: ({{ .Value | red }})",
    Inactive: "  {{ .Type | cyan }} ({{ .Value | red }})",
    Selected: "\U0001F336 {{ .Type | red | cyan }}",
    Details: `
--------- Path ----------
{{ "Type:" | faint }}   {{ .Type }}
{{ "Value:" | faint }}  {{ .Value }}
`,
    }
    prompt := promptui.Select{
        Label:     "Select Path",
        Items:     pathes,
        Searcher:  pathSearcher,
        Templates: pathesChoose,
    }

the array with maps i am passing to Items could look like that:

[
  {name: "1", type: "1"},
  {name: "2", type: "2", additionalValue1: "3"},
  {name: "3", type: "2", additionalValue2: "val"},
]

What i am searching for is a solution to make the Deails Template dynamic, dependent on the keys, the corresponding item has. For example like that:

Details: `
--------- Path ----------
{{ "Type:" | faint }}   {{ .Type }}
{{ "Value:" | faint }}  {{ .Value }}
{{ if .AdditionalValue1 | {{  "AddVal1:" }} {{ .Value }} 
{{ if .AdditionalValue2 | {{  "AddVal2:" }} {{ .Value }} 
`

The same would be nice for the Active, Inactive and Selected field.

So my question is, is this already somehow possible? If not, does anyone has an Idea how to create a workaroung?

thx in advance!

obourdon commented 4 years ago

@divramod may be this link could help you (at least it did for me).

In my case I wrote something for a map of K/V (Tags) like:

--- excerpt of code

public name: {{ .PublicName }}
{{ if index .Tags "Project" }}project: {{ index .Tags "Project" }}{{ end -}}
{{ if index .Tags "UserName" }}user: {{ index .Tags "UserName" }}{{ end -}}
{{ if index .Tags "UserEmail" }}email: {{ index .Tags "UserEmail" }}{{ end -}}`,
obourdon commented 4 years ago

even better so that everything is on different lines for real:

public name: {{ .PublicName }}
{{ if index .Tags "Project" }}project: {{ index .Tags "Project" }}
{{ end -}}
{{ if index .Tags "UserName" }}user: {{ index .Tags "UserName" }}
{{ end -}}
{{ if index .Tags "UserEmail" }}email: {{ index .Tags "UserEmail" }}
{{ end -}}`,
divramod commented 10 months ago

thx!