Skip to content

Commit 7db38f0

Browse files
committed
Add Ask method
1 parent 4f77a91 commit 7db38f0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

actions.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ type Actions interface {
4040
// ShowPagedReader shows a paged text that is scrollable, from a reader source.
4141
// This leverages on "less" for unix and "more" for windows.
4242
ShowPagedReader(r io.Reader) error
43+
// Wait for an answer from user
44+
// text is displayed before
45+
Ask(text string) string
46+
// AskErr is Ask but returns error as well
47+
AskErr(text string) (string, error)
4348
// MultiChoice presents options to the user.
4449
// returns the index of the selection or -1 if nothing is
4550
// selected.
@@ -133,6 +138,15 @@ func (s *shellActionsImpl) Printf(format string, val ...interface{}) {
133138
fmt.Fprintf(s.writer, format, val...)
134139
}
135140

141+
func (s *shellActionsImpl) Ask(text string) string {
142+
line, _ := s.ask(text)
143+
return line
144+
}
145+
146+
func (s *shellActionsImpl) AskErr(text string) (string, error) {
147+
return s.ask(text)
148+
}
149+
136150
func (s *shellActionsImpl) MultiChoice(options []string, text string) int {
137151
choice := s.multiChoice(options, text, nil, false)
138152
return choice[0]

ishell.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,22 @@ func toggle(selected []int, cur int) []int {
483483
return append(selected, cur)
484484
}
485485

486+
func (s *Shell) ask(text string) (string, error) {
487+
conf := s.reader.scanner.Config.Clone()
488+
489+
conf.DisableAutoSaveHistory = true
490+
491+
s.ShowPrompt(false)
492+
defer s.ShowPrompt(true)
493+
oldconf := s.reader.scanner.SetConfig(conf)
494+
495+
s.Print(text + " ")
496+
answer, err := s.ReadLineErr()
497+
498+
s.reader.scanner.SetConfig(oldconf)
499+
return answer, err
500+
}
501+
486502
func (s *Shell) multiChoice(options []string, text string, init []int, multiResults bool) []int {
487503
s.multiChoiceActive = true
488504
defer func() { s.multiChoiceActive = false }()

0 commit comments

Comments
 (0)