Skip to content

Commit 603eace

Browse files
committed
Add key binding for Delete
1 parent f1ea5a0 commit 603eace

File tree

4 files changed

+170
-5
lines changed

4 files changed

+170
-5
lines changed

lib/reline/ansi.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Reline::ANSI
77
CAPNAME_KEY_BINDINGS = {
88
'khome' => :ed_move_to_beg,
99
'kend' => :ed_move_to_end,
10+
'kdch1' => :key_delete,
1011
'kcuu1' => :ed_prev_history,
1112
'kcud1' => :ed_next_history,
1213
'kcuf1' => :ed_next_char,
@@ -29,8 +30,8 @@ def self.win?
2930
false
3031
end
3132

32-
def self.set_default_key_bindings(config)
33-
if Reline::Terminfo.enabled?
33+
def self.set_default_key_bindings(config, allow_terminfo: true)
34+
if allow_terminfo && Reline::Terminfo.enabled?
3435
set_default_key_bindings_terminfo(config)
3536
else
3637
set_default_key_bindings_comprehensive_list(config)

test/reline/helper.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
module Reline
99
class <<self
10-
def test_mode
10+
def test_mode(ansi: false)
1111
remove_const('IOGate') if const_defined?('IOGate')
12-
const_set('IOGate', Reline::GeneralIO)
12+
const_set('IOGate', ansi ? Reline::ANSI : Reline::GeneralIO)
1313
if ENV['RELINE_TEST_ENCODING']
1414
encoding = Encoding.find(ENV['RELINE_TEST_ENCODING'])
1515
else
1616
encoding = Encoding::UTF_8
1717
end
18-
Reline::GeneralIO.reset(encoding: encoding)
18+
Reline::GeneralIO.reset(encoding: encoding) unless ansi
1919
send(:core).config.instance_variable_set(:@test_mode, true)
2020
send(:core).config.reset
2121
end
@@ -122,4 +122,11 @@ def assert_whole_lines(expected)
122122
end
123123
assert_equal(expected, lines)
124124
end
125+
126+
def assert_key_binding(input, method_symbol, editing_modes = [:emacs, :vi_insert, :vi_command])
127+
editing_modes.each do |editing_mode|
128+
@config.editing_mode = editing_mode
129+
assert_equal(method_symbol, @config.editing_mode.default_key_bindings[input.bytes])
130+
end
131+
end
125132
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require_relative 'helper'
2+
require 'reline/ansi'
3+
4+
class Reline::ANSI::TestWithTerminfo < Reline::TestCase
5+
def setup
6+
Reline.send(:test_mode, ansi: true)
7+
@config = Reline::Config.new
8+
Reline::IOGate.set_default_key_bindings(@config, allow_terminfo: true)
9+
end
10+
11+
def teardown
12+
Reline.test_reset
13+
end
14+
15+
# Home key
16+
def test_khome
17+
assert_key_binding(Reline::Terminfo.tigetstr('khome'), :ed_move_to_beg)
18+
rescue Reline::Terminfo::TerminfoError => e
19+
omit e.message
20+
end
21+
22+
# End key
23+
def test_kend
24+
assert_key_binding(Reline::Terminfo.tigetstr('kend'), :ed_move_to_end)
25+
rescue Reline::Terminfo::TerminfoError => e
26+
omit e.message
27+
end
28+
29+
# Delete key
30+
def test_kdch1
31+
assert_key_binding(Reline::Terminfo.tigetstr('kdch1'), :key_delete)
32+
rescue Reline::Terminfo::TerminfoError => e
33+
omit e.message
34+
end
35+
36+
# Up arrow key
37+
def test_kcuu1
38+
assert_key_binding(Reline::Terminfo.tigetstr('kcuu1'), :ed_prev_history)
39+
rescue Reline::Terminfo::TerminfoError => e
40+
omit e.message
41+
end
42+
43+
# Down arrow key
44+
def test_kcud1
45+
assert_key_binding(Reline::Terminfo.tigetstr('kcud1'), :ed_next_history)
46+
rescue Reline::Terminfo::TerminfoError => e
47+
omit e.message
48+
end
49+
50+
# Right arrow key
51+
def test_kcuf1
52+
assert_key_binding(Reline::Terminfo.tigetstr('kcuf1'), :ed_next_char)
53+
rescue Reline::Terminfo::TerminfoError => e
54+
omit e.message
55+
end
56+
57+
# Left arrow key
58+
def test_kcub1
59+
assert_key_binding(Reline::Terminfo.tigetstr('kcub1'), :ed_prev_char)
60+
rescue Reline::Terminfo::TerminfoError => e
61+
omit e.message
62+
end
63+
64+
# Ctrl+arrow and Meta+arrow; always mapped regardless of terminfo enabled or not
65+
def test_extended
66+
assert_key_binding("\e[1;5C", :em_next_word) # Ctrl+→
67+
assert_key_binding("\e[1;5D", :ed_prev_word) # Ctrl+←
68+
assert_key_binding("\e[1;3C", :em_next_word) # Meta+→
69+
assert_key_binding("\e[1;3D", :ed_prev_word) # Meta+←
70+
end
71+
72+
# Shift-Tab; always mapped regardless of terminfo enabled or not
73+
def test_shift_tab
74+
assert_key_binding("\e[Z", :completion_journey_up, [:emacs, :vi_insert])
75+
end
76+
77+
# A few emacs bindings that are always mapped regardless of terminfo enabled or not
78+
def test_more_emacs
79+
assert_key_binding("\e ", :em_set_mark, [:emacs])
80+
assert_key_binding("\C-x\C-x", :em_exchange_mark, [:emacs])
81+
end
82+
end if Reline::Terminfo.enabled?
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
require_relative 'helper'
2+
require 'reline/ansi'
3+
4+
class Reline::ANSI::TestWithoutTerminfo < Reline::TestCase
5+
def setup
6+
Reline.send(:test_mode, ansi: true)
7+
@config = Reline::Config.new
8+
Reline::IOGate.set_default_key_bindings(@config, allow_terminfo: false)
9+
end
10+
11+
def teardown
12+
Reline.test_reset
13+
end
14+
15+
def test_home
16+
assert_key_binding("\e[1~", :ed_move_to_beg) # Console (80x25)
17+
assert_key_binding("\e[H", :ed_move_to_beg) # KDE
18+
assert_key_binding("\e[7~", :ed_move_to_beg) # urxvt / exoterm
19+
assert_key_binding("\eOH", :ed_move_to_beg) # GNOME
20+
end
21+
22+
def test_end
23+
assert_key_binding("\e[4~", :ed_move_to_end) # Console (80x25)
24+
assert_key_binding("\e[F", :ed_move_to_end) # KDE
25+
assert_key_binding("\e[8~", :ed_move_to_end) # urxvt / exoterm
26+
assert_key_binding("\eOF", :ed_move_to_end) # GNOME
27+
end
28+
29+
def test_delete
30+
assert_key_binding("\e[3~", :key_delete)
31+
end
32+
33+
def test_up_arrow
34+
assert_key_binding("\e[A", :ed_prev_history) # Console (80x25)
35+
assert_key_binding("\eGA", :ed_prev_history) # KDE
36+
assert_key_binding("\eOA", :ed_prev_history)
37+
end
38+
39+
def test_down_arrow
40+
assert_key_binding("\e[B", :ed_next_history) # Console (80x25)
41+
assert_key_binding("\eGB", :ed_next_history) # KDE
42+
assert_key_binding("\eOB", :ed_next_history)
43+
end
44+
45+
def test_right_arrow
46+
assert_key_binding("\e[C", :ed_next_char) # Console (80x25)
47+
assert_key_binding("\eGC", :ed_next_char) # KDE
48+
assert_key_binding("\eOC", :ed_next_char)
49+
end
50+
51+
def test_left_arrow
52+
assert_key_binding("\e[D", :ed_prev_char) # Console (80x25)
53+
assert_key_binding("\eGD", :ed_prev_char) # KDE
54+
assert_key_binding("\eOD", :ed_prev_char)
55+
end
56+
57+
# Ctrl+arrow and Meta+arrow; always mapped regardless of terminfo enabled or not
58+
def test_extended
59+
assert_key_binding("\e[1;5C", :em_next_word) # Ctrl+→
60+
assert_key_binding("\e[1;5D", :ed_prev_word) # Ctrl+←
61+
assert_key_binding("\e[1;3C", :em_next_word) # Meta+→
62+
assert_key_binding("\e[1;3D", :ed_prev_word) # Meta+←
63+
end
64+
65+
# Shift-Tab; always mapped regardless of terminfo enabled or not
66+
def test_shift_tab
67+
assert_key_binding("\e[Z", :completion_journey_up, [:emacs, :vi_insert])
68+
end
69+
70+
# A few emacs bindings that are always mapped regardless of terminfo enabled or not
71+
def test_more_emacs
72+
assert_key_binding("\e ", :em_set_mark, [:emacs])
73+
assert_key_binding("\C-x\C-x", :em_exchange_mark, [:emacs])
74+
end
75+
end

0 commit comments

Comments
 (0)