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

Method Show

interactive_textinput_printer.go:83–272  ·  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

81
82// Show shows the interactive select menu and returns the selected entry.
83func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
84 // should be the first defer statement to make sure it is executed last
85 // and all the needed cleanup can be done before
86 cancel, exit := internal.NewCancelationSignal(p.OnInterruptFunc)
87 defer exit()
88
89 var areaText string
90
91 if len(text) == 0 || text[0] == "" {
92 text = []string{p.DefaultText}
93 }
94
95 if p.MultiLine {
96 areaText = p.TextStyle.Sprintfln("%s %s %s", text[0], ThemeDefault.SecondaryStyle.Sprint("[Press tab to submit]"), p.Delimiter)
97 } else {
98 areaText = p.TextStyle.Sprintf("%s%s", text[0], p.Delimiter)
99 }
100
101 p.text = areaText
102 area := cursor.NewArea()
103 area.Update(areaText)
104 area.StartOfLine()
105
106 if !p.MultiLine {
107 cursor.Right(runewidth.StringWidth(RemoveColorFromString(areaText)))
108 }
109
110 if p.DefaultValue != "" {
111 p.input = append(p.input, p.DefaultValue)
112 p.updateArea(&area)
113 }
114
115 err := keyboard.Listen(func(key keys.Key) (stop bool, err error) {
116 if !p.MultiLine {
117 p.cursorYPos = 0
118 }
119 if len(p.input) == 0 {
120 p.input = append(p.input, "")
121 }
122
123 switch key.Code {
124 case keys.Tab:
125 if p.MultiLine {
126 area.Bottom()
127 return true, nil
128 }
129 case keys.Enter:
130 if p.DefaultValue != "" && !p.startedTyping {
131 for i := range p.input {
132 p.input[i] = RemoveColorFromString(p.input[i])
133 }
134
135 if p.MultiLine {
136 area.Bottom()
137 }
138 return true, nil
139 }
140

Callers

nothing calls this directly

Calls 10

updateAreaMethod · 0.95
NewCancelationSignalFunction · 0.92
GetStringMaxWidthFunction · 0.92
RemoveColorFromStringFunction · 0.85
PrintlnFunction · 0.85
UpdateMethod · 0.80
SprintflnMethod · 0.65
SprintMethod · 0.65
SprintfMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected