Skip to content

Commit 148747b

Browse files
committed
Add Ask method
1 parent a108510 commit 148747b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

actions.go

+14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type Actions interface {
3535
// ShowPaged shows a paged text that is scrollable.
3636
// This leverages on "less" for unix and "more" for windows.
3737
ShowPaged(text string) error
38+
// Wait for an answer from user
39+
// text is displayed before
40+
Ask(text string) string
41+
// AskErr is Ask but returns error as well
42+
AskErr(text string) (string, error)
3843
// MultiChoice presents options to the user.
3944
// returns the index of the selection or -1 if nothing is
4045
// selected.
@@ -116,6 +121,15 @@ func (s *shellActionsImpl) Printf(format string, val ...interface{}) {
116121
fmt.Fprintf(s.writer, format, val...)
117122
}
118123

124+
func (s *shellActionsImpl) Ask(text string) string {
125+
line, _ := s.ask(text)
126+
return line
127+
}
128+
129+
func (s *shellActionsImpl) AskErr(text string) (string, error) {
130+
return s.ask(text)
131+
}
132+
119133
func (s *shellActionsImpl) MultiChoice(options []string, text string) int {
120134
choice := s.multiChoice(options, text, nil, false)
121135
return choice[0]

ishell.go

+16
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ func toggle(selected []int, cur int) []int {
443443
return append(selected, cur)
444444
}
445445

446+
func (s *Shell) ask(text string) (string, error) {
447+
conf := s.reader.scanner.Config.Clone()
448+
449+
conf.DisableAutoSaveHistory = true
450+
451+
s.ShowPrompt(false)
452+
defer s.ShowPrompt(true)
453+
oldconf := s.reader.scanner.SetConfig(conf)
454+
455+
s.Print(text + " ")
456+
answer, err := s.ReadLineErr()
457+
458+
s.reader.scanner.SetConfig(oldconf)
459+
return answer, err
460+
}
461+
446462
func (s *Shell) multiChoice(options []string, text string, init []int, multiResults bool) []int {
447463
s.multiChoiceActive = true
448464
defer func() { s.multiChoiceActive = false }()

0 commit comments

Comments
 (0)