Skip to content

Commit

Permalink
Add Ask method
Browse files Browse the repository at this point in the history
  • Loading branch information
Elbandi committed Mar 25, 2018
1 parent a108510 commit 148747b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type Actions interface {
// ShowPaged shows a paged text that is scrollable.
// This leverages on "less" for unix and "more" for windows.
ShowPaged(text string) 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.
Expand Down Expand Up @@ -116,6 +121,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]
Expand Down
16 changes: 16 additions & 0 deletions ishell.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,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 }()
Expand Down

0 comments on commit 148747b

Please sign in to comment.