Skip to content

Commit

Permalink
Merge pull request #6 from MeztliRA/dev
Browse files Browse the repository at this point in the history
Add PromptOnce() function and documentation for it
  • Loading branch information
MeztliRA authored Apr 6, 2021
2 parents a3efd26 + 7579dbc commit a9f38c5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions yon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yon

import (
"bufio"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -30,6 +31,30 @@ func Promptln(prompt string) Response {
return answer
}

// Similar to Prompt(), but only prompt the user for a responce once, return yon.Yes or yon.No and a error, error is not nil if user didnt response with yes or no
func PromptOnce(prompt string) (Response, error) {
log.SetPrefix("prompt: ")
log.SetFlags(0)

reader := bufio.NewReader(os.Stdin)

fmt.Print(prompt)
answer, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
answer = strings.Trim(answer, "\n")

switch answer {
case "y", "Y", "yes", "Yes", "YES":
return Yes, nil
case "n", "N", "no", "No", "NO":
return No, nil
default:
return No, errors.New("prompt: user didnt input yes or no")
}
}

func promptCore(prompt string, ln bool) Response {
log.SetPrefix("prompt: ")
log.SetFlags(0)
Expand Down

0 comments on commit a9f38c5

Please sign in to comment.