From 6c7b01c9aa4617e9dc12aa2174a5d8249e1bbd48 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sun, 19 Apr 2015 01:12:35 +0000 Subject: [PATCH] Accept modifier in `key' run control keyword --- features/run_control/key.feature | 8 ++++++++ lib/uh/wm/run_control.rb | 7 ++++--- spec/uh/wm/run_control_spec.rb | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/features/run_control/key.feature b/features/run_control/key.feature index ec6408e..d931be1 100644 --- a/features/run_control/key.feature +++ b/features/run_control/key.feature @@ -8,6 +8,14 @@ Feature: `key' run control keyword When I press the alt+f keys Then the output must contain "trigger f key code" + Scenario: defines code to run when given keys are pressed + Given uhwm is running with this run control file: + """ + key(:f, :shift) { puts 'trigger f key code' } + """ + When I press the alt+shift+f keys + Then the output must contain "trigger f key code" + Scenario: translates common key names to their X equivalent Given uhwm is running with this run control file: """ diff --git a/lib/uh/wm/run_control.rb b/lib/uh/wm/run_control.rb index b3fc618..c890ee1 100644 --- a/lib/uh/wm/run_control.rb +++ b/lib/uh/wm/run_control.rb @@ -28,8 +28,8 @@ module Uh @env.modifier = mod end - def key keysym, &block - @env.keybinds[translate_keysym keysym] = block + def key *keysyms, &block + @env.keybinds[translate_keysym *keysyms] = block end def worker type, **options @@ -39,7 +39,8 @@ module Uh private - def translate_keysym keysym + def translate_keysym keysym, modifier = nil + return [translate_keysym(keysym)[0].to_sym, modifier] if modifier translate_key = keysym.to_s.downcase.to_sym translated_keysym = KEYSYM_TRANSLATIONS.key?(translate_key) ? KEYSYM_TRANSLATIONS[translate_key] : diff --git a/spec/uh/wm/run_control_spec.rb b/spec/uh/wm/run_control_spec.rb index 1afca27..598af58 100644 --- a/spec/uh/wm/run_control_spec.rb +++ b/spec/uh/wm/run_control_spec.rb @@ -65,6 +65,11 @@ module Uh expect(env.keybinds.keys).to include :f end + it 'registers a combined keys binding in the env' do + rc.key :f, :shift, &code + expect(env.keybinds.keys).to include %i[f shift] + end + it 'registers given block with the key binding' do rc.key :f, &code expect(env.keybinds[:f].call).to eq :keybind_code