Skip to content

Commit

Permalink
Drop loading terminfo, remove fiddle dependency in non-windows enviro…
Browse files Browse the repository at this point in the history
…nment. (#769)

Reline works perfectly in most major terminal emulators without terminfo.
In minor/old terminal emulator, we used to get key bindings from terminfo, but I think it is not used so much.
  • Loading branch information
tompng authored Nov 14, 2024
1 parent aa5b278 commit 3ceba3b
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 382 deletions.
1 change: 0 additions & 1 deletion lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'reline/key_stroke'
require 'reline/line_editor'
require 'reline/history'
require 'reline/terminfo'
require 'reline/io'
require 'reline/face'
require 'rbconfig'
Expand Down
49 changes: 4 additions & 45 deletions lib/reline/io/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class Reline::ANSI < Reline::IO
'H' => [:ed_move_to_beg, {}],
}

if Reline::Terminfo.enabled?
Reline::Terminfo.setupterm(0, 2)
end

def initialize
@input = STDIN
@output = STDOUT
Expand All @@ -44,14 +40,10 @@ def encoding
@input.external_encoding || Encoding.default_external
end

def set_default_key_bindings(config, allow_terminfo: true)
def set_default_key_bindings(config)
set_bracketed_paste_key_bindings(config)
set_default_key_bindings_ansi_cursor(config)
if allow_terminfo && Reline::Terminfo.enabled?
set_default_key_bindings_terminfo(config)
else
set_default_key_bindings_comprehensive_list(config)
end
set_default_key_bindings_comprehensive_list(config)
{
[27, 91, 90] => :completion_journey_up, # S-Tab
}.each_pair do |key, func|
Expand Down Expand Up @@ -98,23 +90,6 @@ def set_default_key_bindings_ansi_cursor(config)
end
end

def set_default_key_bindings_terminfo(config)
key_bindings = CAPNAME_KEY_BINDINGS.map do |capname, key_binding|
begin
key_code = Reline::Terminfo.tigetstr(capname)
[ key_code.bytes, key_binding ]
rescue Reline::Terminfo::TerminfoError
# capname is undefined
end
end.compact.to_h

key_bindings.each_pair do |key, func|
config.add_default_key_binding_by_keymap(:emacs, key, func)
config.add_default_key_binding_by_keymap(:vi_insert, key, func)
config.add_default_key_binding_by_keymap(:vi_command, key, func)
end
end

def set_default_key_bindings_comprehensive_list(config)
{
# xterm
Expand Down Expand Up @@ -281,27 +256,11 @@ def move_cursor_down(x)
end

def hide_cursor
seq = "\e[?25l"
if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
begin
seq = Reline::Terminfo.tigetstr('civis')
rescue Reline::Terminfo::TerminfoError
# civis is undefined
end
end
@output.write seq
@output.write "\e[?25l"
end

def show_cursor
seq = "\e[?25h"
if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
begin
seq = Reline::Terminfo.tigetstr('cnorm')
rescue Reline::Terminfo::TerminfoError
# cnorm is undefined
end
end
@output.write seq
@output.write "\e[?25h"
end

def erase_after_cursor
Expand Down
158 changes: 0 additions & 158 deletions lib/reline/terminfo.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require_relative 'helper'
require 'reline'

class Reline::ANSI::WithoutTerminfoTest < Reline::TestCase
class Reline::ANSITest < Reline::TestCase
def setup
Reline.send(:test_mode, ansi: true)
@config = Reline::Config.new
Reline.core.io_gate.set_default_key_bindings(@config, allow_terminfo: false)
Reline.core.io_gate.set_default_key_bindings(@config)
end

def teardown
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_left_arrow
assert_key_binding("\eOD", :ed_prev_char)
end

# Ctrl+arrow and Meta+arrow; always mapped regardless of terminfo enabled or not
# Ctrl+arrow and Meta+arrow
def test_extended
assert_key_binding("\e[1;5C", :em_next_word) # Ctrl+→
assert_key_binding("\e[1;5D", :ed_prev_word) # Ctrl+←
Expand All @@ -60,12 +60,11 @@ def test_extended
assert_key_binding("\e\e[D", :ed_prev_word) # Meta+←
end

# Shift-Tab; always mapped regardless of terminfo enabled or not
def test_shift_tab
assert_key_binding("\e[Z", :completion_journey_up, [:emacs, :vi_insert])
end

# A few emacs bindings that are always mapped regardless of terminfo enabled or not
# A few emacs bindings that are always mapped
def test_more_emacs
assert_key_binding("\e ", :em_set_mark, [:emacs])
assert_key_binding("\C-x\C-x", :em_exchange_mark, [:emacs])
Expand Down
112 changes: 0 additions & 112 deletions test/reline/test_ansi_with_terminfo.rb

This file was deleted.

Loading

0 comments on commit 3ceba3b

Please sign in to comment.