manifoldco / promptui

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

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

Open zlesnr opened 3 years ago

zlesnr commented 3 years ago

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
}
mister11 commented 3 years ago

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.