master v0.0.3
Starainrt 4 years ago
parent b3ad857712
commit b568f51676

120
io.go

@ -23,31 +23,104 @@ func PasswdWithMask(hint string, defaultVal string, mask string) InputMsg {
return passwd(hint, defaultVal, mask)
}
func MessageBoxRaw(hint string, defaultVal string) InputMsg {
return messageBox(hint, defaultVal)
}
func messageBox(hint string, defaultVal string) InputMsg {
var ioBuf []rune
if hint != "" {
fmt.Print(hint)
}
if strings.Index(hint, "\n") >= 0 {
hint = strings.TrimSpace(hint[strings.LastIndex(hint, "\n"):])
}
fd := int(os.Stdin.Fd())
state, err := terminal.MakeRaw(fd)
if err != nil {
return InputMsg{"", err}
}
defer fmt.Println()
defer terminal.Restore(fd, state)
inputReader := bufio.NewReader(os.Stdin)
for {
b, _, err := inputReader.ReadRune()
if err != nil {
return InputMsg{"", err}
}
if b == 0x0d {
strValue := strings.TrimSpace(string(ioBuf))
if len(strValue) == 0 {
strValue = defaultVal
}
return InputMsg{strValue, nil}
}
if b == 0x08 || b == 0x7F {
if len(ioBuf) > 0 {
ioBuf = ioBuf[:len(ioBuf)-1]
}
fmt.Print("\r")
for i := 0; i < len(ioBuf)+2+len(hint); i++ {
fmt.Print(" ")
}
} else {
ioBuf = append(ioBuf, b)
}
fmt.Print("\r")
if hint != "" {
fmt.Print(hint)
}
fmt.Print(string(ioBuf))
}
}
func passwd(hint string, defaultVal string, mask string) InputMsg {
var ioBuf []byte
var ioBuf []rune
if hint != "" {
fmt.Print(hint)
}
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if strings.Index(hint, "\n") >= 0 {
hint = strings.TrimSpace(hint[strings.LastIndex(hint, "\n"):])
}
fd := int(os.Stdin.Fd())
state, err := terminal.MakeRaw(fd)
if err != nil {
return InputMsg{"", err}
}
defer terminal.Restore(0, state)
defer fmt.Println()
defer terminal.Restore(fd, state)
inputReader := bufio.NewReader(os.Stdin)
for {
b, err := inputReader.ReadByte()
b, _, err := inputReader.ReadRune()
if err != nil {
return InputMsg{"", err}
}
if b == 0x0d {
fmt.Println()
return InputMsg{strings.TrimSpace(string(ioBuf)), nil}
strValue := strings.TrimSpace(string(ioBuf))
if len(strValue) == 0 {
strValue = defaultVal
}
if mask != "" {
fmt.Print(mask)
return InputMsg{strValue, nil}
}
if b == 0x08 || b == 0x7F {
if len(ioBuf) > 0 {
ioBuf = ioBuf[:len(ioBuf)-1]
}
fmt.Print("\r")
for i := 0; i < len(ioBuf)+2+len(hint); i++ {
fmt.Print(" ")
}
} else {
ioBuf = append(ioBuf, b)
}
fmt.Print("\r")
if hint != "" {
fmt.Print(hint)
}
for i := 0; i < len(ioBuf); i++ {
fmt.Print(mask)
}
}
}
func MessageBox(hint string, defaultVal string) InputMsg {
@ -59,7 +132,11 @@ func MessageBox(hint string, defaultVal string) InputMsg {
if err != nil {
return InputMsg{"", err}
}
return InputMsg{strings.TrimSpace(str), nil}
str = strings.TrimSpace(str)
if len(str) == 0 {
str = defaultVal
}
return InputMsg{str, nil}
}
func (im InputMsg) String() (string, error) {
@ -148,6 +225,7 @@ func (im InputMsg) MustFloat32() float32 {
}
func YesNo(hint string, defaults bool) bool {
for {
res := strings.ToUpper(MessageBox(hint, "").MustString())
if res == "" {
return defaults
@ -157,38 +235,36 @@ func YesNo(hint string, defaults bool) bool {
return true
} else if res == "N" {
return false
} else {
return defaults
}
}
}
func StopUntil(hint string, trigger string, repeat bool) error {
pressLen := len(trigger)
pressLen := len([]rune(trigger))
if trigger == "" {
pressLen = 1
} else {
pressLen = len(trigger)
}
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
fd := int(os.Stdin.Fd())
if hint != "" {
fmt.Print(hint)
}
state, err := terminal.MakeRaw(fd)
if err != nil {
return err
}
defer terminal.Restore(0, state)
defer terminal.Restore(fd, state)
inputReader := bufio.NewReader(os.Stdin)
//ioBuf := make([]byte, pressLen)
if hint != "" && !repeat {
fmt.Print(hint)
}
i := 0
for {
b, err := inputReader.ReadByte()
b, _, err := inputReader.ReadRune()
if err != nil {
return err
}
if trigger == "" {
break
}
if b == trigger[i] {
if b == []rune(trigger)[i] {
i++
if i == pressLen {
break
@ -197,7 +273,7 @@ func StopUntil(hint string, trigger string, repeat bool) error {
}
i = 0
if hint != "" && repeat {
fmt.Print("\n")
fmt.Print("\r\n")
fmt.Print(hint)
}
}

Loading…
Cancel
Save