Skip to content

Commit

Permalink
Add Ask method
Browse files Browse the repository at this point in the history
  • Loading branch information
Elbandi committed Jan 9, 2022
1 parent 4f77a91 commit 7db38f0
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 @@ -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.
Expand Down Expand Up @@ -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]
Expand Down
16 changes: 16 additions & 0 deletions ishell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }()
Expand Down

0 comments on commit 7db38f0

Please sign in to comment.