Commits

Taru Karttunen committed 43e5128

PasswordWithEcho + Canon

Comments (0)

Files changed (3)

 	return s
 }
 
+func PasswordWithEcho(prompt string, echo string) string {
+	old,_ := CurrentTerminal()
+	t := old
+	t.Echo(false).Canon(false).Set()
+	defer old.Set()
+
+        os.Stderr.Write([]byte(prompt))
+        var res []byte
+        for {
+                var c [1]byte
+                _,e := os.Stdin.Read(c[:])
+                if e!=nil { return "" }
+                switch c[0] {
+                case '\r':
+                case '\n':
+	                os.Stderr.Write([]byte{'\n'})
+                        return string(res)
+                default:
+			os.Stderr.WriteString(echo)
+                        res = append(res, c[0])
+                }
+        }
+
+	os.Stderr.Write([]byte{'\n'})
+	return ""
+}
+
 func PasswordConfirm(prompt string, prompt2 string) string {
 	var s1, s2 string
 	for {

termios_freebsd.go

 	return t
 }
 
+// Change canonical mode, but do not *change* it
+func (t *Termios) Canon(echo bool) *Termios {
+	if echo {
+		t.Lflag |= _CANON
+	} else {
+		t.Lflag &= ^uint32(_CANON)
+	}
+	return t
+}
+
 const (
 	_TCGETS  = 0x5401
 	_TCSETSW = 0x5403
 	_NCCS    = 32
 	_ECHO    = 010
+        _CANON   = 002
 )
 
 type Termios struct {
 	return t
 }
 
+// Change canonical mode, but do not *change* it
+func (t *Termios) Canon(echo bool) *Termios {
+	if echo {
+		t.Lflag |= _CANON
+	} else {
+		t.Lflag &= ^uint32(_CANON)
+	}
+	return t
+}
+
 const (
 	_TCGETS  = 0x5401
 	_TCSETSW = 0x5403
 	_NCCS    = 32
 	_ECHO    = 010
+        _CANON   = 002
 )
 
 type Termios struct {