diff --git a/lib/uh/wm/env.rb b/lib/uh/wm/env.rb index 3c14d9b..5cf8318 100644 --- a/lib/uh/wm/env.rb +++ b/lib/uh/wm/env.rb @@ -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? diff --git a/lib/uh/wm/run_control.rb b/lib/uh/wm/run_control.rb index 871925a..fc66ceb 100644 --- a/lib/uh/wm/run_control.rb +++ b/lib/uh/wm/run_control.rb @@ -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 diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index d2ddd4d..18a3262 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -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 diff --git a/spec/uh/wm/run_control_spec.rb b/spec/uh/wm/run_control_spec.rb index b9115a5..3ac1f39 100644 --- a/spec/uh/wm/run_control_spec.rb +++ b/spec/uh/wm/run_control_spec.rb @@ -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 diff --git a/spec/uh/wm/runner_spec.rb b/spec/uh/wm/runner_spec.rb index 98e7951..3dd8630 100644 --- a/spec/uh/wm/runner_spec.rb +++ b/spec/uh/wm/runner_spec.rb @@ -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