diff --git a/actions.go b/actions.go index aa4fc75..ea1491d 100644 --- a/actions.go +++ b/actions.go @@ -40,6 +40,11 @@ type Actions interface { // ShowPagedReader shows a paged text that is scrollable, from a reader source. // This leverages on "less" for unix and "more" for windows. ShowPagedReader(r io.Reader) error + // Wait for an answer from user + // text is displayed before + Ask(text string) string + // AskErr is Ask but returns error as well + AskErr(text string) (string, error) // MultiChoice presents options to the user. // returns the index of the selection or -1 if nothing is // selected. @@ -133,6 +138,15 @@ func (s *shellActionsImpl) Printf(format string, val ...interface{}) { fmt.Fprintf(s.writer, format, val...) } +func (s *shellActionsImpl) Ask(text string) string { + line, _ := s.ask(text) + return line +} + +func (s *shellActionsImpl) AskErr(text string) (string, error) { + return s.ask(text) +} + func (s *shellActionsImpl) MultiChoice(options []string, text string) int { choice := s.multiChoice(options, text, nil, false) return choice[0] diff --git a/ishell.go b/ishell.go index f440095..e5a6380 100644 --- a/ishell.go +++ b/ishell.go @@ -483,6 +483,22 @@ func toggle(selected []int, cur int) []int { return append(selected, cur) } +func (s *Shell) ask(text string) (string, error) { + conf := s.reader.scanner.Config.Clone() + + conf.DisableAutoSaveHistory = true + + s.ShowPrompt(false) + defer s.ShowPrompt(true) + oldconf := s.reader.scanner.SetConfig(conf) + + s.Print(text + " ") + answer, err := s.ReadLineErr() + + s.reader.scanner.SetConfig(oldconf) + return answer, err +} + func (s *Shell) multiChoice(options []string, text string, init []int, multiResults bool) []int { s.multiChoiceActive = true defer func() { s.multiChoiceActive = false }()