MCPcopy
hub / github.com/pterm/pterm / Show

Method Show

interactive_multiselect_printer.go:136–320  ·  view source on GitHub ↗

Show shows the interactive multiselect menu and returns the selected entry.

(text ...string)

Source from the content-addressed store, hash-verified

134
135// Show shows the interactive multiselect menu and returns the selected entry.
136func (p *InteractiveMultiselectPrinter) Show(text ...string) ([]string, error) {
137 // should be the first defer statement to make sure it is executed last
138 // and all the needed cleanup can be done before
139 cancel, exit := internal.NewCancelationSignal(p.OnInterruptFunc)
140 defer exit()
141
142 if len(text) == 0 || Sprint(text[0]) == "" {
143 text = []string{p.DefaultText}
144 }
145
146 p.text = p.TextStyle.Sprint(text[0])
147 p.fuzzySearchMatches = append([]string{}, p.Options...)
148
149 if p.MaxHeight == 0 {
150 p.MaxHeight = DefaultInteractiveMultiselect.MaxHeight
151 }
152
153 maxHeight := p.MaxHeight
154 if maxHeight > len(p.fuzzySearchMatches) {
155 maxHeight = len(p.fuzzySearchMatches)
156 }
157
158 if len(p.Options) == 0 {
159 return nil, fmt.Errorf("no options provided")
160 }
161
162 p.displayedOptions = append([]string{}, p.fuzzySearchMatches[:maxHeight]...)
163 p.displayedOptionsStart = 0
164 p.displayedOptionsEnd = maxHeight
165
166 for _, option := range p.DefaultOptions {
167 p.selectOption(option)
168 }
169
170 area, err := DefaultArea.Start(p.renderSelectMenu())
171 defer area.Stop()
172 if err != nil {
173 return nil, fmt.Errorf("could not start area: %w", err)
174 }
175
176 if p.Filter && (p.KeyConfirm == keys.Space || p.KeySelect == keys.Space) {
177 return nil, fmt.Errorf("if filter/search is active, keys.Space can not be used for KeySelect or KeyConfirm")
178 }
179
180 area.Update(p.renderSelectMenu())
181
182 cursor.Hide()
183 defer cursor.Show()
184 err = keyboard.Listen(func(keyInfo keys.Key) (stop bool, err error) {
185 key := keyInfo.Code
186
187 if p.MaxHeight > len(p.fuzzySearchMatches) {
188 maxHeight = len(p.fuzzySearchMatches)
189 } else {
190 maxHeight = p.MaxHeight
191 }
192
193 switch key {

Callers

nothing calls this directly

Calls 11

selectOptionMethod · 0.95
renderSelectMenuMethod · 0.95
renderFinishedMenuMethod · 0.95
NewCancelationSignalFunction · 0.92
SprintFunction · 0.85
UpdateMethod · 0.80
SprintMethod · 0.65
PrintlnMethod · 0.65
StartMethod · 0.45
StopMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected