mirror of
https://github.com/golang/go
synced 2024-11-21 21:24:45 -07:00
exp/terminal: add SetPrompt and handle large pastes.
(This was missing in the last change because I uploaded it from the wrong machine.) Large pastes previously misbehaved because the code tried reading from the terminal before checking whether an line was already buffered. Large pastes can cause multiples lines to be read at once from the terminal. R=bradfitz CC=golang-dev https://golang.org/cl/5542049
This commit is contained in:
parent
423a09760b
commit
a9e1f6d7a6
@ -463,6 +463,31 @@ func (t *Terminal) readLine() (line string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
rest := t.remainder
|
||||||
|
lineOk := false
|
||||||
|
for !lineOk {
|
||||||
|
var key int
|
||||||
|
key, rest = bytesToKey(rest)
|
||||||
|
if key < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if key == keyCtrlD {
|
||||||
|
return "", io.EOF
|
||||||
|
}
|
||||||
|
line, lineOk = t.handleKey(key)
|
||||||
|
}
|
||||||
|
if len(rest) > 0 {
|
||||||
|
n := copy(t.inBuf[:], rest)
|
||||||
|
t.remainder = t.inBuf[:n]
|
||||||
|
} else {
|
||||||
|
t.remainder = nil
|
||||||
|
}
|
||||||
|
t.c.Write(t.outBuf)
|
||||||
|
t.outBuf = t.outBuf[:0]
|
||||||
|
if lineOk {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// t.remainder is a slice at the beginning of t.inBuf
|
// t.remainder is a slice at the beginning of t.inBuf
|
||||||
// containing a partial key sequence
|
// containing a partial key sequence
|
||||||
readBuf := t.inBuf[len(t.remainder):]
|
readBuf := t.inBuf[len(t.remainder):]
|
||||||
@ -476,38 +501,19 @@ func (t *Terminal) readLine() (line string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil {
|
t.remainder = t.inBuf[:n+len(t.remainder)]
|
||||||
t.remainder = t.inBuf[:n+len(t.remainder)]
|
|
||||||
rest := t.remainder
|
|
||||||
lineOk := false
|
|
||||||
for !lineOk {
|
|
||||||
var key int
|
|
||||||
key, rest = bytesToKey(rest)
|
|
||||||
if key < 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if key == keyCtrlD {
|
|
||||||
return "", io.EOF
|
|
||||||
}
|
|
||||||
line, lineOk = t.handleKey(key)
|
|
||||||
}
|
|
||||||
if len(rest) > 0 {
|
|
||||||
n := copy(t.inBuf[:], rest)
|
|
||||||
t.remainder = t.inBuf[:n]
|
|
||||||
} else {
|
|
||||||
t.remainder = nil
|
|
||||||
}
|
|
||||||
t.c.Write(t.outBuf)
|
|
||||||
t.outBuf = t.outBuf[:0]
|
|
||||||
if lineOk {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPrompt sets the prompt to be used when reading subsequent lines.
|
||||||
|
func (t *Terminal) SetPrompt(prompt string) {
|
||||||
|
t.lock.Lock()
|
||||||
|
defer t.lock.Unlock()
|
||||||
|
|
||||||
|
t.prompt = prompt
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Terminal) SetSize(width, height int) {
|
func (t *Terminal) SetSize(width, height int) {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user