mirror of
https://github.com/golang/term.git
synced 2026-02-07 19:26:03 +03:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d2308b09d | ||
|
|
e770dddbf5 | ||
|
|
04218fdaf7 | ||
|
|
208db03875 | ||
|
|
743b2709ab | ||
|
|
40b02d69cd | ||
|
|
442846aa8d | ||
|
|
b725e362a8 | ||
|
|
54df7da90d | ||
|
|
9d5441ab55 |
11
README.md
11
README.md
@@ -4,16 +4,13 @@
|
||||
|
||||
This repository provides Go terminal and console support packages.
|
||||
|
||||
## Download/Install
|
||||
|
||||
The easiest way to install is to run `go get -u golang.org/x/term`. You can
|
||||
also manually git clone the repository to `$GOPATH/src/golang.org/x/term`.
|
||||
|
||||
## Report Issues / Send Patches
|
||||
|
||||
This repository uses Gerrit for code changes. To learn how to submit changes to
|
||||
this repository, see https://golang.org/doc/contribute.html.
|
||||
this repository, see https://go.dev/doc/contribute.
|
||||
|
||||
The git repository is https://go.googlesource.com/term.
|
||||
|
||||
The main issue tracker for the term repository is located at
|
||||
https://github.com/golang/go/issues. Prefix your issue with "x/term:" in the
|
||||
https://go.dev/issues. Prefix your issue with "x/term:" in the
|
||||
subject line, so it is easy to find.
|
||||
|
||||
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.25.0
|
||||
require golang.org/x/sys v0.32.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,2 +1,2 @@
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.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