Skip to content

Commit 9b991dd

Browse files
joshariangopherbot
authored andcommitted
x/term: handle delete key
Fixes golang/go#76826 Change-Id: I80dc9468c2cc716dab10a8e165ca5f72817ef4b9 Reviewed-on: https://go-review.googlesource.com/c/term/+/730440 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 3863673 commit 9b991dd

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

‎terminal.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ const (
160160
keyEnd
161161
keyDeleteWord
162162
keyDeleteLine
163+
keyDelete
163164
keyClearScreen
164165
keyPasteStart
165166
keyPasteEnd
@@ -228,6 +229,10 @@ func bytesToKey(b []byte, pasteActive bool) (rune, []byte) {
228229
}
229230
}
230231

232+
if !pasteActive && len(b) >= 4 && b[0] == keyEscape && b[1] == '[' && b[2] == '3' && b[3] == '~' {
233+
return keyDelete, b[4:]
234+
}
235+
231236
if !pasteActive && len(b) >= 6 && b[0] == keyEscape && b[1] == '[' && b[2] == '1' && b[3] == ';' && b[4] == '3' {
232237
switch b[5] {
233238
case 'C':
@@ -590,7 +595,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
590595
}
591596
t.line = t.line[:t.pos]
592597
t.moveCursorToPos(t.pos)
593-
case keyCtrlD:
598+
case keyCtrlD, keyDelete:
594599
// Erase the character under the current position.
595600
// The EOF case when the line is empty is handled in
596601
// readLine().

‎terminal_test.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,31 @@ var keyPressTests = []struct {
238238
in: "a\003\r",
239239
err: io.EOF,
240240
},
241+
{
242+
// Delete at EOL: nothing
243+
in: "abc\x1b[3~\r",
244+
line: "abc",
245+
},
246+
{
247+
// Delete in empty string: nothing
248+
in: "\x1b[3~\r",
249+
line: "",
250+
},
251+
{
252+
// Move left, delete: delete 'c'
253+
in: "abc\x1b[D\x1b[3~\r",
254+
line: "ab",
255+
},
256+
{
257+
// Home, delete: delete 'a'
258+
in: "abc\x1b[H\x1b[3~\r",
259+
line: "bc",
260+
},
261+
{
262+
// Home, delete twice: delete 'a' and 'b'
263+
in: "abc\x1b[H\x1b[3~\x1b[3~\r",
264+
line: "c",
265+
},
241266
}
242267

243268
func TestKeyPresses(t *testing.T) {
@@ -387,7 +412,7 @@ func TestReadPasswordLineEnd(t *testing.T) {
387412
input string
388413
want string
389414
}
390-
var tests = []testType{
415+
tests := []testType{
391416
{"\r\n", ""},
392417
{"test\r\n", "test"},
393418
{"test\r", "test"},

0 commit comments

Comments
 (0)