manifoldco / promptui

Interactive prompt for command-line applications

Home Page:https://www.manifold.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When using select, the `Label` disappears after the selection is made.

zlesnr opened this issue · comments

This confuses our users because they want to see what question they were asked, along with their answers as the interview continues. Is there a way to get the Label to remain in place along with the selection after the selection is made?

func (p *PromptUIPrompter) PromptYesNo(msg string) (bool, error) {
	prompt := promptui.Select{
		Label:        msg,
		Items:        []string{"Yes", "No"},
	}

	_, result, err := prompt.Run()
	if err != nil {
		return false, err
	}

	return result == "Yes", nil
}

This behavior is defined by a template. Default template defines that only a selected value is printed without a label:

if tpls.Selected == "" {
	tpls.Selected = fmt.Sprintf(`{{ "%s" | green }} {{ . | faint }}`, IconGood)
}

So, to fix the issues you have, you can override a default template like this:

prompt := promptui.Select{
	Label: prefix,
	Items: choices,
	Templates: &promptui.SelectTemplates {
		Selected: fmt.Sprintf(`%s: {{ . | faint }}`, prefix),
	},
}

You can customize the output depending on your needs.