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

Method Show

interactive_confirm_printer.go:109–166  ·  view source on GitHub ↗

Show shows the confirm prompt. Example: result, _ := pterm.DefaultInteractiveConfirm.Show("Are you sure?") pterm.Println(result)

(text ...string)

Source from the content-addressed store, hash-verified

107// result, _ := pterm.DefaultInteractiveConfirm.Show("Are you sure?")
108// pterm.Println(result)
109func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
110 // should be the first defer statement to make sure it is executed last
111 // and all the needed cleanup can be done before
112 cancel, exit := internal.NewCancelationSignal(p.OnInterruptFunc)
113 defer exit()
114
115 var result bool
116
117 if len(text) == 0 || text[0] == "" {
118 text = []string{p.DefaultText}
119 }
120
121 p.TextStyle.Print(text[0] + " " + p.getSuffix() + p.Delimiter)
122 y, n := p.getShortHandles()
123
124 var interrupted bool
125 err := keyboard.Listen(func(keyInfo keys.Key) (stop bool, err error) {
126 key := keyInfo.Code
127 char := strings.ToLower(keyInfo.String())
128 if err != nil {
129 return false, fmt.Errorf("failed to get key: %w", err)
130 }
131
132 switch key {
133 case keys.RuneKey:
134 switch char {
135 case y:
136 p.ConfirmStyle.Print(p.ConfirmText)
137 Println()
138 result = true
139 return true, nil
140 case n:
141 p.RejectStyle.Print(p.RejectText)
142 Println()
143 result = false
144 return true, nil
145 }
146 case keys.Enter:
147 if p.DefaultValue {
148 p.ConfirmStyle.Print(p.ConfirmText)
149 } else {
150 p.RejectStyle.Print(p.RejectText)
151 }
152 Println()
153 result = p.DefaultValue
154 return true, nil
155 case keys.CtrlC:
156 cancel()
157 interrupted = true
158 return true, nil
159 }
160 return false, nil
161 })
162 if !interrupted {
163 cursor.StartOfLine()
164 }
165 return result, err
166}

Callers

nothing calls this directly

Calls 6

getSuffixMethod · 0.95
getShortHandlesMethod · 0.95
NewCancelationSignalFunction · 0.92
PrintlnFunction · 0.85
PrintMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected