Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ask method #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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