Register key bindings in the env with RunControl

This commit is contained in:
Thibault Jouan 2015-04-14 21:26:29 +00:00
parent b1aa430ef3
commit 2a1a1f1185
5 changed files with 31 additions and 4 deletions

View File

@ -13,12 +13,13 @@ module Uh
def_delegator :logger, :debug, :log_debug
def_delegator :@output, :print
attr_reader :output
attr_reader :output, :keybinds
attr_accessor :verbose, :debug, :rc_path, :layout_class
def initialize output
@output = output
@rc_path = RC_PATH
@keybinds = {}
end
def verbose?

View File

@ -16,6 +16,10 @@ module Uh
def evaluate code
instance_eval code
end
def key keysym, &block
@env.keybinds[keysym] = block
end
end
end
end

View File

@ -44,6 +44,9 @@ module Uh
def connect_manager
@manager.connect
@manager.grab_key :q
@env.keybinds.each do |keysym, _|
@manager.grab_key *keysym
end
end
def run_until &block
@ -70,6 +73,9 @@ module Uh
def register_key_bindings_hooks
@events.on(:key, :q) { stop! }
@env.keybinds.each do |keysym, code|
@events.on :key, keysym, &code
end
end
end
end

View File

@ -48,6 +48,20 @@ module Uh
.to raise_error env.object_id.to_s
end
end
describe '#key' do
let(:code) { -> { :keybind_code } }
before { rc.key :f, &code }
it 'registers a key binding in the env' do
expect(env.keybinds.keys).to include :f
end
it 'registers given block with the key binding' do
expect(env.keybinds[:f].call).to eq :keybind_code
end
end
end
end
end

View File

@ -76,8 +76,9 @@ module Uh
end
it 'registers key bindings event hooks' do
env.keybinds[:f] = -> { }
runner.register_event_hooks
expect(runner.events[:key, :q]).not_to be_empty
expect(runner.events[:key, :f]).not_to be_empty
end
end
@ -90,8 +91,9 @@ module Uh
runner.connect_manager
end
it 'tells the manager to grab keys' do
expect(runner.manager).to receive(:grab_key).with :q
it 'tells the manager to grab keys for env key bindings' do
env.keybinds[:f] = -> { }
expect(runner.manager).to receive(:grab_key).with :f
runner.connect_manager
end
end