summaryrefslogtreecommitdiff
path: root/prompt
diff options
context:
space:
mode:
authorPaul Bütow <pbuetow@mimecast.com>2020-01-20 18:41:05 +0000
committerPaul Bütow <pbuetow@mimecast.com>2020-01-21 14:35:23 +0000
commitc128865c4c7411c29a59fca9a3a2f95537686d7b (patch)
tree193bccc70d942c8b70cc93fae2670263701e43aa /prompt
parent3755a9911ecb05886577095f2b8cc8b9e4066a3a (diff)
Move commands to cmd/ and move internal dependencies to internal/
Diffstat (limited to 'prompt')
-rw-r--r--prompt/prompt.go95
1 files changed, 0 insertions, 95 deletions
diff --git a/prompt/prompt.go b/prompt/prompt.go
deleted file mode 100644
index 395d4bd..0000000
--- a/prompt/prompt.go
+++ /dev/null
@@ -1,95 +0,0 @@
-package prompt
-
-import (
- "bufio"
- "dtail/logger"
- "fmt"
- "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
-}