wujian_develop_tool/util/common.go

53 lines
930 B
Go

package util
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/fatih/color"
)
func ReadBool() bool {
retry:
read := bufio.NewReader(os.Stdin)
ret, _ := read.ReadByte()
switch ret {
case 'y':
fallthrough
case 'Y':
return true
case 'n':
fallthrough
case 'N':
return false
default:
NoNewLinePrint(color.HiRedString, "请输入(y,Y,n,N)其中之一: ")
goto retry
}
}
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))
}