Skip to content
This repository was archived by the owner on Mar 12, 2023. It is now read-only.

Commit fc1b2a2

Browse files
committed
Move global key bindings to RubyJard.config
1 parent 59da2e6 commit fc1b2a2

7 files changed

+27
-40
lines changed

lib/ruby_jard.rb

+1-11
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
require 'ruby_jard/path_classifier'
1010
require 'ruby_jard/path_filter'
1111
require 'ruby_jard/control_flow'
12-
require 'ruby_jard/config'
1312
require 'ruby_jard/keys'
13+
require 'ruby_jard/config'
1414
require 'ruby_jard/key_binding'
1515
require 'ruby_jard/key_bindings'
1616
require 'ruby_jard/repl_processor'
@@ -81,16 +81,6 @@ def self.error(exception)
8181
# Ignore
8282
end
8383

84-
def self.global_key_bindings
85-
return @global_key_bindings if defined?(@global_key_bindings)
86-
87-
@global_key_bindings = RubyJard::KeyBindings.new
88-
RubyJard::Keys::DEFAULT_KEY_BINDINGS.each do |sequence, action|
89-
@global_key_bindings.push(sequence, action)
90-
end
91-
@global_key_bindings
92-
end
93-
9484
def self.config
9585
@config ||= RubyJard::Config.smart_load
9686
end

lib/ruby_jard/config.rb

+16-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def load_config(config, path)
3838
end
3939
end
4040

41-
attr_accessor :color_scheme, :alias_to_debugger, :layout
41+
attr_accessor :color_scheme, :alias_to_debugger, :layout, :key_bindings
4242
attr_reader :enabled_screens, :filter_version, :filter, :filter_included, :filter_excluded
4343

4444
CONFIG_FILE_NAME = '.jardrc'
@@ -48,7 +48,19 @@ def load_config(config, path)
4848
DEFAULT_LAYOUT = nil, # Pick layout automatically
4949
DEFAULT_FILTER = RubyJard::PathFilter::FILTER_APPLICATION,
5050
DEFAULT_FILTER_INCLUDED = [].freeze,
51-
DEFAULT_FILTER_EXCLUDED = [].freeze
51+
DEFAULT_FILTER_EXCLUDED = [].freeze,
52+
DEFAULT_KEY_BINDINGS = {
53+
RubyJard::Keys::F2 => 'jard filter switch',
54+
RubyJard::Keys::F5 => 'list',
55+
RubyJard::Keys::F6 => 'up',
56+
RubyJard::Keys::SHIFT_F6 => 'down',
57+
RubyJard::Keys::F7 => 'step',
58+
RubyJard::Keys::SHIFT_F7 => 'step-out',
59+
RubyJard::Keys::F8 => 'next',
60+
RubyJard::Keys::F9 => 'continue',
61+
RubyJard::Keys::CTRL_D => 'continue',
62+
RubyJard::Keys::CTRL_C => 'interrupt'
63+
}.freeze
5264
].freeze
5365

5466
def initialize
@@ -62,6 +74,8 @@ def initialize
6274
@alias_to_debugger = DEFAULT_ALIAS_TO_DEBUGGER.freeze
6375
@layout = DEFAULT_LAYOUT.freeze
6476
@enabled_screens = RubyJard::Screens.names.dup.freeze
77+
78+
@key_bindings = RubyJard::KeyBindings.new(DEFAULT_KEY_BINDINGS)
6579
end
6680

6781
def config

lib/ruby_jard/key_bindings.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ module RubyJard
1010
class KeyBindings
1111
attr_reader :indexes
1212

13-
def initialize
13+
def initialize(sequences)
1414
@key_bindings = []
1515
@indexes = {}
16+
sequences.each do |sequence, action|
17+
push(sequence, action)
18+
end
1619
end
1720

1821
def to_a

lib/ruby_jard/keys.rb

-11
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,5 @@ class Keys
3434
SHIFT_F10 = "\e[21;2~"
3535
SHIFT_F11 = "\e[23;2~"
3636
SHIFT_F12 = "\e[24;2~"
37-
DEFAULT_KEY_BINDINGS = {
38-
F2 => (ACTION_FILTER = 'jard filter switch'),
39-
F5 => (ACTION_LIST = 'list'),
40-
F6 => (ACTION_UP = 'up'),
41-
SHIFT_F6 => (ACTION_DOWN = 'down'),
42-
F7 => (ACTION_STEP = 'step'),
43-
SHIFT_F7 => (ACTION_STEP_OUT = 'step-out'),
44-
F8 => (ACTION_NEXT = 'next'),
45-
F9 => (ACTION_CONTINUE = 'continue'),
46-
CTRL_D => ACTION_CONTINUE
47-
}.freeze
4837
end
4938
end

lib/ruby_jard/repl_interceptor.rb

+3-11
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,13 @@ module RubyJard
4646
#
4747
# As a result, Jard may support key-binding customization without breaking pry functionalities.
4848
class ReplInterceptor
49-
INTERNAL_KEY_BINDINGS = {
50-
RubyJard::Keys::CTRL_C => (KEY_BINDING_INTERRUPT = :interrupt)
51-
}.freeze
52-
5349
KEY_READ_TIMEOUT = 0.2 # 200ms
5450
OUTPUT_TICK = 1.to_f / 60 # 60hz
5551

56-
def initialize(state, console, key_bindings)
52+
def initialize(state, console)
5753
@state = state
5854
@console = console
59-
60-
@key_bindings = key_bindings || RubyJard::KeyBindings.new
61-
INTERNAL_KEY_BINDINGS.each do |sequence, action|
62-
@key_bindings.push(sequence, action)
63-
end
55+
@key_bindings = RubyJard.config.key_bindings
6456

6557
reopen_streams
6658
start_output_bridge
@@ -209,7 +201,7 @@ def listen_key_press
209201

210202
def handle_key_binding(key_binding)
211203
case key_binding.action
212-
when KEY_BINDING_INTERRUPT
204+
when 'interrupt'
213205
handle_interrupt_command
214206
else
215207
@state.check(:ready?) do

lib/ruby_jard/repl_manager.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ module RubyJard
99
class ReplManager
1010
OUTPUT_TICK = 1.to_f / 60 # 60hz
1111

12-
def initialize(console:, key_bindings: nil)
12+
def initialize(console:)
1313
@console = console
1414
@state = RubyJard::ReplState.new
15-
@interceptor = RubyJard::ReplInterceptor.new(@state, @console, key_bindings)
15+
@interceptor = RubyJard::ReplInterceptor.new(@state, @console)
1616

1717
Signal.trap('SIGWINCH') { start_resizing }
1818
end

lib/ruby_jard/session.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def attach(location)
3636
def initialize
3737
@screen_manager = RubyJard::ScreenManager.new
3838
@repl_manager = RubyJard::ReplManager.new(
39-
console: @screen_manager.console,
40-
key_bindings: RubyJard.global_key_bindings
39+
console: @screen_manager.console
4140
)
4241
@path_filter = RubyJard::PathFilter.new
4342
@started = false

0 commit comments

Comments
 (0)