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

Method Show

interactive_select_printer.go:99–270  ·  view source on GitHub ↗

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

(text ...string)

Source from the content-addressed store, hash-verified

97
98// Show shows the interactive select menu and returns the selected entry.
99func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {
100 // should be the first defer statement to make sure it is executed last
101 // and all the needed cleanup can be done before
102 cancel, exit := internal.NewCancelationSignal(p.OnInterruptFunc)
103 defer exit()
104
105 if len(text) == 0 || Sprint(text[0]) == "" {
106 text = []string{p.DefaultText}
107 }
108
109 p.text = p.TextStyle.Sprint(text[0])
110 p.fuzzySearchMatches = append([]string{}, p.Options...)
111
112 if p.MaxHeight == 0 {
113 p.MaxHeight = DefaultInteractiveSelect.MaxHeight
114 }
115
116 maxHeight := p.MaxHeight
117 if maxHeight > len(p.fuzzySearchMatches) {
118 maxHeight = len(p.fuzzySearchMatches)
119 }
120
121 if len(p.Options) == 0 {
122 return "", fmt.Errorf("no options provided")
123 }
124
125 p.displayedOptions = append([]string{}, p.fuzzySearchMatches[:maxHeight]...)
126 p.displayedOptionsStart = 0
127 p.displayedOptionsEnd = maxHeight
128
129 // Get index of default option
130 if p.DefaultOption != "" {
131 for i, option := range p.Options {
132 if option == p.DefaultOption {
133 p.selectedOption = i
134 if i > 0 && len(p.Options) > maxHeight {
135 p.displayedOptionsEnd = int(math.Min(float64(i-1+maxHeight), float64(len(p.Options))))
136 p.displayedOptionsStart = p.displayedOptionsEnd - maxHeight
137 } else {
138 p.displayedOptionsStart = 0
139 p.displayedOptionsEnd = maxHeight
140 }
141 p.displayedOptions = p.Options[p.displayedOptionsStart:p.displayedOptionsEnd]
142 break
143 }
144 }
145 }
146
147 area, err := DefaultArea.Start(p.renderSelectMenu())
148 defer area.Stop()
149 if err != nil {
150 return "", fmt.Errorf("could not start area: %w", err)
151 }
152
153 area.Update(p.renderSelectMenu())
154
155 cursor.Hide()
156 defer cursor.Show()

Calls 10

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