add signal control for password input
This commit is contained in:
parent
95503ec65a
commit
609bdf7079
38
io.go
38
io.go
@ -3,11 +3,11 @@ package stario
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
type InputMsg struct {
|
||||
@ -17,11 +17,19 @@ type InputMsg struct {
|
||||
}
|
||||
|
||||
func Passwd(hint string, defaultVal string) InputMsg {
|
||||
return passwd(hint, defaultVal, "")
|
||||
return passwd(hint, defaultVal, "", false)
|
||||
}
|
||||
|
||||
func PasswdWithMask(hint string, defaultVal string, mask string) InputMsg {
|
||||
return passwd(hint, defaultVal, mask)
|
||||
return passwd(hint, defaultVal, mask, false)
|
||||
}
|
||||
|
||||
func PasswdResponseSignal(hint string, defaultVal string) InputMsg {
|
||||
return passwd(hint, defaultVal, "", true)
|
||||
}
|
||||
|
||||
func PasswdResponseSignalWithMask(hint string, defaultVal string, mask string) InputMsg {
|
||||
return passwd(hint, defaultVal, mask, true)
|
||||
}
|
||||
|
||||
func MessageBoxRaw(hint string, defaultVal string) InputMsg {
|
||||
@ -75,7 +83,16 @@ func messageBox(hint string, defaultVal string) InputMsg {
|
||||
}
|
||||
}
|
||||
|
||||
func passwd(hint string, defaultVal string, mask string) InputMsg {
|
||||
func isSiganl(s rune) bool {
|
||||
switch s {
|
||||
case 0x03, 0x1a, 0x1c:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func passwd(hint string, defaultVal string, mask string, handleSignal bool) InputMsg {
|
||||
var ioBuf []rune
|
||||
if hint != "" {
|
||||
fmt.Print(hint)
|
||||
@ -96,6 +113,17 @@ func passwd(hint string, defaultVal string, mask string) InputMsg {
|
||||
if err != nil {
|
||||
return InputMsg{msg: "", err: err}
|
||||
}
|
||||
if handleSignal && isSiganl(b) {
|
||||
if runtime.GOOS != "windows" {
|
||||
terminal.Restore(fd, state)
|
||||
}
|
||||
if err := signal(b); err != nil {
|
||||
return InputMsg{
|
||||
msg: "",
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
}
|
||||
if b == 0x0d {
|
||||
strValue := strings.TrimSpace(string(ioBuf))
|
||||
if len(strValue) == 0 {
|
||||
|
22
signal_windows.go
Normal file
22
signal_windows.go
Normal file
@ -0,0 +1,22 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package stario
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func signal(sigtype rune) error {
|
||||
//todo: use win32api call signal
|
||||
switch sigtype {
|
||||
case 0x03:
|
||||
return errors.New("SIGNAL SIGINT RECIVED")
|
||||
case 0x1a:
|
||||
return errors.New("SIGNAL SIGSTOP RECIVED")
|
||||
case 0x1c:
|
||||
return errors.New("SIGNAL SIGQUIT RECIVED")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
26
singal_other.go
Normal file
26
singal_other.go
Normal file
@ -0,0 +1,26 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package stario
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func signal(sigtype rune) error {
|
||||
switch sigtype {
|
||||
case 0x03:
|
||||
syscall.Kill(os.Getpid(), syscall.SIGINT)
|
||||
return errors.New("SIGNAL SIGINT RECIVED")
|
||||
case 0x1a:
|
||||
syscall.Kill(os.Getpid(), syscall.SIGSTOP)
|
||||
return errors.New("SIGNAL SIGSTOP RECIVED")
|
||||
case 0x1c:
|
||||
syscall.Kill(os.Getpid(), syscall.SIGQUIT)
|
||||
return errors.New("SIGNAL SIGQUIT RECIVED")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user