mirror of
https://github.com/golang/term.git
synced 2026-02-07 19:26:03 +03:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d2308b09d | ||
|
|
e770dddbf5 | ||
|
|
04218fdaf7 | ||
|
|
208db03875 | ||
|
|
743b2709ab | ||
|
|
40b02d69cd | ||
|
|
442846aa8d |
4
go.mod
4
go.mod
@@ -1,5 +1,5 @@
|
||||
module golang.org/x/term
|
||||
|
||||
go 1.18
|
||||
go 1.23.0
|
||||
|
||||
require golang.org/x/sys v0.27.0
|
||||
require golang.org/x/sys v0.32.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,2 +1,2 @@
|
||||
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
||||
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
|
||||
@@ -44,6 +44,8 @@ type Terminal struct {
|
||||
// bytes, as an index into |line|). If it returns ok=false, the key
|
||||
// press is processed normally. Otherwise it returns a replacement line
|
||||
// and the new cursor position.
|
||||
//
|
||||
// This will be disabled during ReadPassword.
|
||||
AutoCompleteCallback func(line string, pos int, key rune) (newLine string, newPos int, ok bool)
|
||||
|
||||
// Escape contains a pointer to the escape codes for this terminal.
|
||||
@@ -692,6 +694,8 @@ func (t *Terminal) Write(buf []byte) (n int, err error) {
|
||||
|
||||
// ReadPassword temporarily changes the prompt and reads a password, without
|
||||
// echo, from the terminal.
|
||||
//
|
||||
// The AutoCompleteCallback is disabled during this call.
|
||||
func (t *Terminal) ReadPassword(prompt string) (line string, err error) {
|
||||
t.lock.Lock()
|
||||
defer t.lock.Unlock()
|
||||
@@ -699,6 +703,11 @@ func (t *Terminal) ReadPassword(prompt string) (line string, err error) {
|
||||
oldPrompt := t.prompt
|
||||
t.prompt = []rune(prompt)
|
||||
t.echo = false
|
||||
oldAutoCompleteCallback := t.AutoCompleteCallback
|
||||
t.AutoCompleteCallback = nil
|
||||
defer func() {
|
||||
t.AutoCompleteCallback = oldAutoCompleteCallback
|
||||
}()
|
||||
|
||||
line, err = t.readLine()
|
||||
|
||||
|
||||
@@ -396,6 +396,32 @@ func TestReadPasswordLineEnd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func MockAutoCompleteCallback(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
|
||||
return "not-good", pos, true
|
||||
}
|
||||
|
||||
func TestReadPasswordDisabledAutoCompleteCallback(t *testing.T) {
|
||||
input := "testgood\ranother line\r"
|
||||
expectedPassword := "testgood"
|
||||
terminal := NewTerminal(
|
||||
&MockTerminal{
|
||||
toSend: []byte(input),
|
||||
bytesPerRead: 1,
|
||||
},
|
||||
"prompt")
|
||||
terminal.AutoCompleteCallback = MockAutoCompleteCallback
|
||||
password, err := terminal.ReadPassword("Password: ")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read password: %v", err)
|
||||
}
|
||||
if password != expectedPassword {
|
||||
t.Fatalf("failed to read password, got %q", password)
|
||||
}
|
||||
if terminal.AutoCompleteCallback == nil {
|
||||
t.Fatalf("AutoCompleteCallback should not be nil after ReadPassword")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeRawState(t *testing.T) {
|
||||
fd := int(os.Stdout.Fd())
|
||||
if !IsTerminal(fd) {
|
||||
|
||||
Reference in New Issue
Block a user