wujian_develop_tool/util/common.go

53 lines
930 B
Go
Raw Normal View History

2022-01-06 22:32:03 +08:00
package util
import (
"bufio"
"fmt"
"os"
"strings"
2022-01-10 20:26:14 +08:00
"github.com/fatih/color"
2022-01-06 22:32:03 +08:00
)
func ReadBool() bool {
2022-01-10 03:37:21 +08:00
retry:
2022-01-06 22:32:03 +08:00
read := bufio.NewReader(os.Stdin)
ret, _ := read.ReadByte()
switch ret {
case 'y':
fallthrough
case 'Y':
return true
case 'n':
fallthrough
case 'N':
return false
2022-01-10 03:37:21 +08:00
default:
2022-01-10 20:26:14 +08:00
NoNewLinePrint(color.HiRedString, "请输入(y,Y,n,N)其中之一: ")
2022-01-10 03:37:21 +08:00
goto retry
2022-01-06 22:32:03 +08:00
}
}
func ReadBoolWithMessage(message string) bool {
NoNewLinePrint(color.HiRedString, message)
return ReadBool()
}
func ReadLine() string {
read := bufio.NewReader(os.Stdin)
s, _ := read.ReadString('\n')
return s[:len(s)-1]
}
func ReadLineWithMessage(message string) string {
NoNewLinePrint(color.HiRedString, message)
return ReadLine()
}
func NoNewLinePrint(f func(format string, a ...interface{}) string, message string) {
if strings.HasSuffix(message, "\n") {
message = message[:len(message)-1]
}
fmt.Printf(f(message))
}