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

Method Show

interactive_continue_printer.go:122–164  ·  view source on GitHub ↗

Show shows the continue prompt. Example: result, _ := pterm.DefaultInteractiveContinue.Show("Do you want to apply the changes?") pterm.Println(result)

(text ...string)

Source from the content-addressed store, hash-verified

120// result, _ := pterm.DefaultInteractiveContinue.Show("Do you want to apply the changes?")
121// pterm.Println(result)
122func (p InteractiveContinuePrinter) Show(text ...string) (string, error) {
123 var result string
124
125 if len(text) == 0 || text[0] == "" {
126 text = []string{p.DefaultText}
127 }
128
129 p.TextStyle.Print(text[0] + " " + p.getSuffix() + p.Delimiter)
130
131 err := keyboard.Listen(func(keyInfo keys.Key) (stop bool, err error) {
132 if err != nil {
133 return false, fmt.Errorf("failed to get key: %w", err)
134 }
135 key := keyInfo.Code
136 char := keyInfo.String()
137
138 switch key {
139 case keys.RuneKey:
140 for i, c := range p.Handles {
141 if !p.ShowShortHandles {
142 c = string([]rune(c)[0])
143 }
144 if char == c || (i == p.DefaultValueIndex && strings.EqualFold(c, char)) {
145 p.OptionsStyle.Print(p.Options[i])
146 Println()
147 result = p.Options[i]
148 return true, nil
149 }
150 }
151 case keys.Enter:
152 p.OptionsStyle.Print(p.Options[p.DefaultValueIndex])
153 Println()
154 result = p.Options[p.DefaultValueIndex]
155 return true, nil
156 case keys.CtrlC:
157 internal.Exit(1)
158 return true, nil
159 }
160 return false, nil
161 })
162 cursor.StartOfLine()
163 return result, err
164}
165
166// getShortHandles returns the short hand answers for the continueation prompt
167func (p InteractiveContinuePrinter) getShortHandles() []string {

Callers

nothing calls this directly

Calls 5

getSuffixMethod · 0.95
ExitFunction · 0.92
PrintlnFunction · 0.85
PrintMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected