bug fix
This commit is contained in:
parent
b3ad857712
commit
b568f51676
122
io.go
122
io.go
@ -23,30 +23,103 @@ func PasswdWithMask(hint string, defaultVal string, mask string) InputMsg {
|
|||||||
return passwd(hint, defaultVal, mask)
|
return passwd(hint, defaultVal, mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
func passwd(hint string, defaultVal string, mask string) InputMsg {
|
func MessageBoxRaw(hint string, defaultVal string) InputMsg {
|
||||||
var ioBuf []byte
|
return messageBox(hint, defaultVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func messageBox(hint string, defaultVal string) InputMsg {
|
||||||
|
var ioBuf []rune
|
||||||
if hint != "" {
|
if hint != "" {
|
||||||
fmt.Print(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 {
|
if err != nil {
|
||||||
return InputMsg{"", err}
|
return InputMsg{"", err}
|
||||||
}
|
}
|
||||||
defer terminal.Restore(0, state)
|
defer fmt.Println()
|
||||||
|
defer terminal.Restore(fd, state)
|
||||||
inputReader := bufio.NewReader(os.Stdin)
|
inputReader := bufio.NewReader(os.Stdin)
|
||||||
for {
|
for {
|
||||||
b, err := inputReader.ReadByte()
|
b, _, err := inputReader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InputMsg{"", err}
|
return InputMsg{"", err}
|
||||||
}
|
}
|
||||||
if b == 0x0d {
|
if b == 0x0d {
|
||||||
fmt.Println()
|
strValue := strings.TrimSpace(string(ioBuf))
|
||||||
return InputMsg{strings.TrimSpace(string(ioBuf)), nil}
|
if len(strValue) == 0 {
|
||||||
|
strValue = defaultVal
|
||||||
}
|
}
|
||||||
if 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)
|
||||||
|
}
|
||||||
|
fmt.Print(string(ioBuf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func passwd(hint string, defaultVal string, mask 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)
|
||||||
|
}
|
||||||
|
for i := 0; i < len(ioBuf); i++ {
|
||||||
fmt.Print(mask)
|
fmt.Print(mask)
|
||||||
}
|
}
|
||||||
ioBuf = append(ioBuf, b)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +132,11 @@ func MessageBox(hint string, defaultVal string) InputMsg {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return InputMsg{"", err}
|
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) {
|
func (im InputMsg) String() (string, error) {
|
||||||
@ -148,6 +225,7 @@ func (im InputMsg) MustFloat32() float32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func YesNo(hint string, defaults bool) bool {
|
func YesNo(hint string, defaults bool) bool {
|
||||||
|
for {
|
||||||
res := strings.ToUpper(MessageBox(hint, "").MustString())
|
res := strings.ToUpper(MessageBox(hint, "").MustString())
|
||||||
if res == "" {
|
if res == "" {
|
||||||
return defaults
|
return defaults
|
||||||
@ -157,38 +235,36 @@ func YesNo(hint string, defaults bool) bool {
|
|||||||
return true
|
return true
|
||||||
} else if res == "N" {
|
} else if res == "N" {
|
||||||
return false
|
return false
|
||||||
} else {
|
}
|
||||||
return defaults
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StopUntil(hint string, trigger string, repeat bool) error {
|
func StopUntil(hint string, trigger string, repeat bool) error {
|
||||||
pressLen := len(trigger)
|
pressLen := len([]rune(trigger))
|
||||||
if trigger == "" {
|
if trigger == "" {
|
||||||
pressLen = 1
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer terminal.Restore(0, state)
|
defer terminal.Restore(fd, state)
|
||||||
inputReader := bufio.NewReader(os.Stdin)
|
inputReader := bufio.NewReader(os.Stdin)
|
||||||
//ioBuf := make([]byte, pressLen)
|
//ioBuf := make([]byte, pressLen)
|
||||||
if hint != "" && !repeat {
|
|
||||||
fmt.Print(hint)
|
|
||||||
}
|
|
||||||
i := 0
|
i := 0
|
||||||
for {
|
for {
|
||||||
b, err := inputReader.ReadByte()
|
b, _, err := inputReader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if trigger == "" {
|
if trigger == "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if b == trigger[i] {
|
if b == []rune(trigger)[i] {
|
||||||
i++
|
i++
|
||||||
if i == pressLen {
|
if i == pressLen {
|
||||||
break
|
break
|
||||||
@ -197,7 +273,7 @@ func StopUntil(hint string, trigger string, repeat bool) error {
|
|||||||
}
|
}
|
||||||
i = 0
|
i = 0
|
||||||
if hint != "" && repeat {
|
if hint != "" && repeat {
|
||||||
fmt.Print("\n")
|
fmt.Print("\r\n")
|
||||||
fmt.Print(hint)
|
fmt.Print(hint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user