Show shows the interactive select menu and returns the selected entry.
(text ...string)
| 81 | |
| 82 | // Show shows the interactive select menu and returns the selected entry. |
| 83 | func (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 |
nothing calls this directly
no test coverage detected