ssh/terminal: handle ctrl+C, ctrl+F, ctrl+B
ctrl+C: terminate readline, which may result in application termination. ctrl+F: keyRight ctrl+B: keyLeft Update golang/go#27147 Change-Id: If319ef79708b98c030cbce102400a785d15137f8 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/228223 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
committed by
Hyang-Ah Hana Kim
parent
056763e48d
commit
4f8f47aa79
@@ -113,6 +113,7 @@ func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
keyCtrlC = 3
|
||||||
keyCtrlD = 4
|
keyCtrlD = 4
|
||||||
keyCtrlU = 21
|
keyCtrlU = 21
|
||||||
keyEnter = '\r'
|
keyEnter = '\r'
|
||||||
@@ -151,8 +152,12 @@ func bytesToKey(b []byte, pasteActive bool) (rune, []byte) {
|
|||||||
switch b[0] {
|
switch b[0] {
|
||||||
case 1: // ^A
|
case 1: // ^A
|
||||||
return keyHome, b[1:]
|
return keyHome, b[1:]
|
||||||
|
case 2: // ^B
|
||||||
|
return keyLeft, b[1:]
|
||||||
case 5: // ^E
|
case 5: // ^E
|
||||||
return keyEnd, b[1:]
|
return keyEnd, b[1:]
|
||||||
|
case 6: // ^F
|
||||||
|
return keyRight, b[1:]
|
||||||
case 8: // ^H
|
case 8: // ^H
|
||||||
return keyBackspace, b[1:]
|
return keyBackspace, b[1:]
|
||||||
case 11: // ^K
|
case 11: // ^K
|
||||||
@@ -738,6 +743,9 @@ func (t *Terminal) readLine() (line string, err error) {
|
|||||||
return "", io.EOF
|
return "", io.EOF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if key == keyCtrlC {
|
||||||
|
return "", io.EOF
|
||||||
|
}
|
||||||
if key == keyPasteStart {
|
if key == keyPasteStart {
|
||||||
t.pasteActive = true
|
t.pasteActive = true
|
||||||
if len(t.line) == 0 {
|
if len(t.line) == 0 {
|
||||||
|
|||||||
@@ -81,6 +81,14 @@ var keyPressTests = []struct {
|
|||||||
in: "a\x1b[Db\r", // left
|
in: "a\x1b[Db\r", // left
|
||||||
line: "ba",
|
line: "ba",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
in: "a\006b\r", // ^F
|
||||||
|
line: "ab",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "a\002b\r", // ^B
|
||||||
|
line: "ba",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
in: "a\177b\r", // backspace
|
in: "a\177b\r", // backspace
|
||||||
line: "b",
|
line: "b",
|
||||||
@@ -208,6 +216,16 @@ var keyPressTests = []struct {
|
|||||||
line: "a",
|
line: "a",
|
||||||
err: ErrPasteIndicator,
|
err: ErrPasteIndicator,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
// Ctrl-C terminates readline
|
||||||
|
in: "\003",
|
||||||
|
err: io.EOF,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Ctrl-C at the end of line also terminates readline
|
||||||
|
in: "a\003\r",
|
||||||
|
err: io.EOF,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyPresses(t *testing.T) {
|
func TestKeyPresses(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user