diff options
| author | Paul Bütow <pbuetow@mimecast.com> | 2020-02-08 18:59:05 +0000 |
|---|---|---|
| committer | Paul Bütow <pbuetow@mimecast.com> | 2020-02-08 18:59:05 +0000 |
| commit | 75c530dcd9fbe3376f3a3c617663af0303a935e3 (patch) | |
| tree | f8041840493cab9396003c009f9a4b761ca8694a /internal/prompt | |
| parent | 6c3ddb7746c062e967b44af568077f5ce2c9ec39 (diff) | |
move prompt to io/
Diffstat (limited to 'internal/prompt')
| -rw-r--r-- | internal/prompt/prompt.go | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go deleted file mode 100644 index a438d33..0000000 --- a/internal/prompt/prompt.go +++ /dev/null @@ -1,95 +0,0 @@ -package prompt - -import ( - "bufio" - "fmt" - "github.com/mimecast/dtail/internal/io/logger" - "os" - "strings" -) - -// Answer is a user input of a prompt question. -type Answer struct { - // Long version of the expected user input - Long string - // Short version of the expected user input - Short string - // Runs when user input matches - Callback func() - // Runs after Callback and after logging resumes - EndCallback func() - - AskAgain bool -} - -// Prompt used for interactive user input. -type Prompt struct { - question string - answers []Answer -} - -func (p *Prompt) askString() string { - var sb strings.Builder - - sb.WriteString(p.question) - sb.WriteString("? (") - - var ax []string - for _, a := range p.answers { - ax = append(ax, fmt.Sprintf("%s=%s", a.Short, a.Long)) - } - - sb.WriteString(strings.Join(ax, ",")) - sb.WriteString("): ") - - return sb.String() -} - -// New returns a new prompt. -func New(question string) *Prompt { - return &Prompt{question: question} -} - -// Add an answer. -func (p *Prompt) Add(answer Answer) { - p.answers = append(p.answers, answer) -} - -// Ask a question. -func (p *Prompt) Ask() { - reader := bufio.NewReader(os.Stdin) - logger.Pause() - - for { - fmt.Print(p.askString()) - answerStr, _ := reader.ReadString('\n') - - if a, ok := p.answer(strings.TrimSpace(answerStr)); ok { - if a.Callback != nil { - a.Callback() - } - - if !a.AskAgain { - logger.Resume() - if a.EndCallback != nil { - a.EndCallback() - } - return - } - } - } -} - -func (p *Prompt) answer(answerStr string) (*Answer, bool) { - for _, a := range p.answers { - switch answerStr { - case a.Long: - return &a, true - case a.Short: - return &a, true - default: - } - } - - return nil, false -} |
