Skip to content

Commit

Permalink
✨ feat(cli): add new cli util command ReadAsBool() and update Confirm()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 2, 2023
1 parent fb2a7c2 commit 6c2f703
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions cliutil/read.go
Expand Up @@ -15,13 +15,13 @@ var (
// Input global input stream
Input io.Reader = os.Stdin
// Output global output stream
// Output io.Writer = os.Stdout
Output io.Writer = os.Stdout
)

// ReadInput read user input form Stdin
func ReadInput(question string) (string, error) {
if len(question) > 0 {
color.Print(question)
color.Fprint(Output, question)
}

scanner := bufio.NewScanner(Input)
Expand All @@ -41,7 +41,7 @@ func ReadInput(question string) (string, error) {
// ans, _ := cliutil.ReadLine("your name?")
func ReadLine(question string) (string, error) {
if len(question) > 0 {
color.Print(question)
color.Fprint(Output, question)
}

reader := bufio.NewReader(Input)
Expand All @@ -66,7 +66,7 @@ func ReadFirst(question string) (string, error) {
// ans, _ := cliutil.ReadFirstByte("continue?[y/n] ")
func ReadFirstByte(question string) (byte, error) {
if len(question) > 0 {
color.Print(question)
color.Fprint(Output, question)
}

reader := bufio.NewReader(Input)
Expand All @@ -76,14 +76,31 @@ func ReadFirstByte(question string) (byte, error) {
// ReadFirstRune read first rune char
func ReadFirstRune(question string) (rune, error) {
if len(question) > 0 {
color.Print(question)
color.Fprint(Output, question)
}

reader := bufio.NewReader(Input)
answer, _, err := reader.ReadRune()
return answer, err
}

// ReadAsBool check user inputted answer is right
//
// Usage:
//
// ok := ReadAsBool("are you OK? [y/N]", false)
func ReadAsBool(tip string, defVal bool) bool {
fChar, err := ReadFirstByte(tip)
if err != nil {
panic(err)
}

if fChar != 0 {
return ByteIsYes(fChar)
}
return defVal
}

// ReadPassword from console terminal
func ReadPassword(question ...string) string {
if len(question) > 0 {
Expand All @@ -102,12 +119,16 @@ func ReadPassword(question ...string) string {
}

// Confirm with user input
func Confirm(tip string) bool {
ans, err := ReadFirst(tip + " [y/N] ")
if err != nil {
return false
func Confirm(tip string, defVal ...bool) bool {
mark := " [y/N]: "

var defV bool
if len(defVal) > 0 && defVal[0] {
defV = true
mark = " [Y/n]: "
}
return InputIsYes(ans)

return ReadAsBool(tip+mark, defV)
}

// InputIsYes answer: yes, y, Yes, Y
Expand Down

0 comments on commit 6c2f703

Please sign in to comment.