|
| 1 | +// Copyright 2022-2026 Salesforce, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package iostreams |
| 16 | + |
| 17 | +// Charm-based prompt implementations using the huh library |
| 18 | +// These are used when the "charm" experiment is enabled |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "slices" |
| 23 | + |
| 24 | + "github.com/charmbracelet/huh" |
| 25 | +) |
| 26 | + |
| 27 | +// charmInputPrompt prompts for text input using a charm huh form |
| 28 | +func charmInputPrompt(_ *IOStreams, _ context.Context, message string, cfg InputPromptConfig) (string, error) { |
| 29 | + var input string |
| 30 | + field := huh.NewInput(). |
| 31 | + Title(message). |
| 32 | + Value(&input) |
| 33 | + if cfg.Required { |
| 34 | + field.Validate(huh.ValidateMinLength(1)) |
| 35 | + } |
| 36 | + err := huh.NewForm(huh.NewGroup(field)).Run() |
| 37 | + if err != nil { |
| 38 | + return "", err |
| 39 | + } |
| 40 | + return input, nil |
| 41 | +} |
| 42 | + |
| 43 | +// charmConfirmPrompt prompts for a yes/no confirmation using a charm huh form |
| 44 | +func charmConfirmPrompt(_ *IOStreams, _ context.Context, message string, defaultValue bool) (bool, error) { |
| 45 | + var choice = defaultValue |
| 46 | + field := huh.NewConfirm(). |
| 47 | + Title(message). |
| 48 | + Value(&choice) |
| 49 | + err := huh.NewForm(huh.NewGroup(field)).Run() |
| 50 | + if err != nil { |
| 51 | + return false, err |
| 52 | + } |
| 53 | + return choice, nil |
| 54 | +} |
| 55 | + |
| 56 | +// charmSelectPrompt prompts the user to select one option using a charm huh form |
| 57 | +func charmSelectPrompt(_ *IOStreams, _ context.Context, msg string, options []string, cfg SelectPromptConfig) (SelectPromptResponse, error) { |
| 58 | + var selected string |
| 59 | + var opts []huh.Option[string] |
| 60 | + for _, opt := range options { |
| 61 | + key := opt |
| 62 | + if cfg.Description != nil { |
| 63 | + if desc := cfg.Description(opt, len(opts)); desc != "" { |
| 64 | + key = opt + "\n " + desc |
| 65 | + } |
| 66 | + } |
| 67 | + opts = append(opts, huh.NewOption(key, opt)) |
| 68 | + } |
| 69 | + |
| 70 | + field := huh.NewSelect[string](). |
| 71 | + Title(msg). |
| 72 | + Options(opts...). |
| 73 | + Value(&selected) |
| 74 | + |
| 75 | + if cfg.PageSize > 0 { |
| 76 | + field.Height(cfg.PageSize + 2) |
| 77 | + } |
| 78 | + |
| 79 | + err := huh.NewForm(huh.NewGroup(field)).Run() |
| 80 | + if err != nil { |
| 81 | + return SelectPromptResponse{}, err |
| 82 | + } |
| 83 | + |
| 84 | + index := slices.Index(options, selected) |
| 85 | + return SelectPromptResponse{Prompt: true, Index: index, Option: selected}, nil |
| 86 | +} |
| 87 | + |
| 88 | +// charmPasswordPrompt prompts for a password (hidden input) using a charm huh form |
| 89 | +func charmPasswordPrompt(_ *IOStreams, _ context.Context, message string, cfg PasswordPromptConfig) (PasswordPromptResponse, error) { |
| 90 | + var input string |
| 91 | + field := huh.NewInput(). |
| 92 | + Title(message). |
| 93 | + EchoMode(huh.EchoModePassword). |
| 94 | + Value(&input) |
| 95 | + if cfg.Required { |
| 96 | + field.Validate(huh.ValidateMinLength(1)) |
| 97 | + } |
| 98 | + err := huh.NewForm(huh.NewGroup(field)).Run() |
| 99 | + if err != nil { |
| 100 | + return PasswordPromptResponse{}, err |
| 101 | + } |
| 102 | + return PasswordPromptResponse{Prompt: true, Value: input}, nil |
| 103 | +} |
| 104 | + |
| 105 | +// charmMultiSelectPrompt prompts the user to select multiple options using a charm huh form |
| 106 | +func charmMultiSelectPrompt(_ *IOStreams, _ context.Context, message string, options []string) ([]string, error) { |
| 107 | + var selected []string |
| 108 | + var opts []huh.Option[string] |
| 109 | + for _, opt := range options { |
| 110 | + opts = append(opts, huh.NewOption(opt, opt)) |
| 111 | + } |
| 112 | + |
| 113 | + field := huh.NewMultiSelect[string](). |
| 114 | + Title(message). |
| 115 | + Options(opts...). |
| 116 | + Value(&selected) |
| 117 | + |
| 118 | + err := huh.NewForm(huh.NewGroup(field)).Run() |
| 119 | + if err != nil { |
| 120 | + return []string{}, err |
| 121 | + } |
| 122 | + return selected, nil |
| 123 | +} |
0 commit comments