From 7f7bd16e1b4d8f77d1f71b704ea5908bc94b634e Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Wed, 9 Mar 2016 17:26:35 +0800 Subject: [PATCH] Refactor rbindkeys, with the help of rebocop. (/spec leave untouched) --- bin/rbindkeys | 8 +- lib/rbindkeys.rb | 14 --- lib/rbindkeys/bind_resolver.rb | 22 ++-- lib/rbindkeys/bind_set.rb | 1 - lib/rbindkeys/bind_tree.rb | 57 +++++---- lib/rbindkeys/cli.rb | 60 +++++----- lib/rbindkeys/device.rb | 9 +- lib/rbindkeys/device_operator.rb | 28 +++-- lib/rbindkeys/fix_resolver.rb | 11 +- lib/rbindkeys/key_bind.rb | 4 +- lib/rbindkeys/key_event_handler.rb | 109 ++++++++---------- lib/rbindkeys/key_event_handler/configurer.rb | 62 +++++----- lib/rbindkeys/log_utils.rb | 31 ++--- lib/rbindkeys/observer.rb | 34 +++--- lib/rbindkeys/version.rb | 2 +- lib/rbindkeys/virtual_device.rb | 6 +- lib/rbindkeys/window_matcher.rb | 12 +- 17 files changed, 203 insertions(+), 267 deletions(-) diff --git a/bin/rbindkeys b/bin/rbindkeys index 555aebd..c6175ff 100755 --- a/bin/rbindkeys +++ b/bin/rbindkeys @@ -1,5 +1,5 @@ -# -*- coding:utf-8; mode:ruby; -*- +#!/usr/bin/env ruby +# -*- coding: utf-8; mode:ruby; -*- -require "rbindkeys" - -Rbindkeys::CLI::main ARGV +require 'rbindkeys' +Rbindkeys::CLI.main ARGV diff --git a/lib/rbindkeys.rb b/lib/rbindkeys.rb index 251e4d3..2eb1f9f 100644 --- a/lib/rbindkeys.rb +++ b/lib/rbindkeys.rb @@ -19,20 +19,6 @@ require 'rbindkeys/cli' module Rbindkeys - - class BindTree; end - class Observer; end - class Devicie; end - class VirtualDevice; end - - class DeviceOperator; end - class WindowMatcher; end - class KeyEventHandler; end - class BindResolver; end - class FixResolver; end - - class CLI; end - class DuplicateNodeError < ArgumentError; end class UnknownKeyValue < Exception; end end diff --git a/lib/rbindkeys/bind_resolver.rb b/lib/rbindkeys/bind_resolver.rb index 00297d7..fcdf6a0 100644 --- a/lib/rbindkeys/bind_resolver.rb +++ b/lib/rbindkeys/bind_resolver.rb @@ -1,9 +1,7 @@ # -*- coding:utf-8; mode:ruby; -*- module Rbindkeys - class BindResolver - LOG = LogUtils.get_logger name DEFAULT_VALUE = :through @@ -15,43 +13,43 @@ class BindResolver # if this resolver is set by prefix key, then true # else, false attr_reader :two_stroke - alias :two_stroke? :two_stroke + alias two_stroke? two_stroke - def initialize upper_resolver=:through, two_stroke=false + def initialize(upper_resolver=:through, two_stroke=false) @tree = {} - if upper_resolver.kind_of? Symbol + if upper_resolver.is_a? Symbol upper_resolver = FixResolver.instance upper_resolver end @upper_resolver = upper_resolver @two_stroke = two_stroke end - def bind input, output + def bind(input, output) @tree[input.last] ||= [] @tree[input.last].each do |b| if b.input == input - raise DuplicateNodeError, "already this input(#{input.inspect}) was binded" + fail DuplicateNodeError, "already this input(#{input.inspect}) was binded" end end kb = KeyBind.new input, output - @tree[input.last] << kb # TODO implement a bubble insertion - @tree[input.last].sort!{|a,b| b.input.length <=> a.input.length} + @tree[input.last] << kb # TODO: implement a bubble insertion + @tree[input.last].sort! {|a, b| b.input.length <=> a.input.length } kb end - def resolve key_code, key_code_set + def resolve(key_code, key_code_set) just_resolve(key_code, key_code_set) or @upper_resolver.resolve(key_code, key_code_set) end - def just_resolve key_code, key_code_set + def just_resolve(key_code, key_code_set) arr = @tree[key_code] arr.each do |kb| sub = kb.input - key_code_set sub.first == kb.input.last and return kb - end if not arr.nil? + end unless arr.nil? nil end end diff --git a/lib/rbindkeys/bind_set.rb b/lib/rbindkeys/bind_set.rb index a0a3dad..568ef36 100644 --- a/lib/rbindkeys/bind_set.rb +++ b/lib/rbindkeys/bind_set.rb @@ -1,7 +1,6 @@ # -*- coding:utf-8; mode:ruby; -*- module Rbindkeys - # BindSet is a implementation for replace with BindTree # using Array#& (set intersection). # because BindTree drop bind order infomation. diff --git a/lib/rbindkeys/bind_tree.rb b/lib/rbindkeys/bind_tree.rb index 41f5ffc..9c3358f 100644 --- a/lib/rbindkeys/bind_tree.rb +++ b/lib/rbindkeys/bind_tree.rb @@ -2,9 +2,8 @@ module Rbindkeys class BindTree - DEFAULT_DEFAULT_VALUE = :through - AVAIVABLE_DEFAULT_VALUE = [:through, :ignore] + AVAIVABLE_DEFAULT_VALUE = [:through, :ignore].freeze # a tree structure which that nodes are Fixnum(keycode) and # leaves are Leaf @@ -20,24 +19,24 @@ class BindTree # a value if no binds hit attr_reader :default_value - def initialize default_value=DEFAULT_DEFAULT_VALUE + def initialize(default_value=DEFAULT_DEFAULT_VALUE) @tree = {} @active_key_binds = [] if AVAIVABLE_DEFAULT_VALUE.include? default_value @default_value = default_value else - raise ArgumentError, "expect #{AVAIVABLE_DEFAULT_VALUE.join('/')}" + fail ArgumentError, "expect #{AVAIVABLE_DEFAULT_VALUE.join('/')}" end end # register an input-output pair # _input_: Array of (Array of) input keycodes # _output_: Array of send keycodes or Proc - def bind input, output=nil + def bind(input, output=nil) input = input.clone new_input = [] - if input.kind_of? Array and input[0].kind_of? Array + if input.is_a? Array and input[0].is_a? Array new_input = input input = new_input.shift end @@ -47,29 +46,27 @@ def bind input, output=nil subtree = @tree input.each do |code| - if subtree.has_key? code and (not subtree[code].kind_of? Hash) - raise DuplicateNodeError, "already register an input:#{input}" + if subtree.key? code and (not subtree[code].is_a? Hash) + fail DuplicateNodeError, "already register an input:#{input}" end subtree[code] ||= {} subtree = subtree[code] end if not new_input.empty? - if subtree.has_key?(tail_code) and - not (subtree[tail_code].kind_of?(Leaf) and - subtree[tail_code].payload.kind_of?(BindTree)) - raise DuplicateNodeError, "already register an input:#{input}" + if subtree.key?(tail_code) and + not (subtree[tail_code].is_a? Leaf and + subtree[tail_code].payload.is_a? BindTree) + fail DuplicateNodeError, "already register an input:#{input}" end - if new_input.length == 1 - new_input = new_input.first - end + new_input = new_input.first if new_input.length == 1 subtree[tail_code] ||= Leaf.new BindTree.new :ignore subtree[tail_code].payload.bind new_input, output - elsif subtree.has_key? tail_code - raise DuplicateNodeError, "already register an input:#{input}" + elsif subtree.key? tail_code + fail DuplicateNodeError, "already register an input:#{input}" else subtree[tail_code] = Leaf.new KeyBind.new input.push(tail_code), output @@ -77,7 +74,7 @@ def bind input, output=nil end # called when event.value == 0 - def resolve_for_released_event event, pressed_keys + def resolve_for_released_event(event, _pressed_keys) release_binds = [] @active_key_binds.reject! do |key_bind| if key_bind.input.include? event.code @@ -96,38 +93,36 @@ def resolve_for_released_event event, pressed_keys end # called when event.value == 1 - def resolve_for_pressed_event event, pressed_keys + def resolve_for_pressed_event(event, pressed_keys) subtree = @tree last_code = -1 pressed_keys.each do |code| if last_code >= code - raise ArgumentError, "expect a sorted Array for 2nd arg (pressed_keys)" + fail ArgumentError, 'expect a sorted Array for 2nd arg (pressed_keys)' end last_code = code - if subtree.has_key? code - subtree = subtree[code] - end + subtree = subtree[code] if subtree.key? code end - subtree = (subtree.kind_of?(Hash) and subtree[event.code]) + subtree = (subtree.is_a? Hash and subtree[event.code]) - if not subtree or subtree.kind_of? Hash + if not subtree or subtree.is_a? Hash return @default_value - elsif subtree.kind_of? Leaf - if subtree.payload.kind_of? KeyBind + elsif subtree.is_a? Leaf + if subtree.payload.is_a? KeyBind @active_key_binds << subtree.payload return subtree.payload - elsif subtree.payload.kind_of? BindTree + elsif subtree.payload.is_a? BindTree return subtree.payload end else - raise UnexpecedLeafError, "unexpeced Leaf: #{subtree.inspect}" + fail UnexpecedLeafError, "unexpeced Leaf: #{subtree.inspect}" end end # called when event.value == 2 - def resolve_for_pressing_event event, pressed_keys + def resolve_for_pressing_event(_event, _pressed_keys) if @active_key_binds.empty? @default_value else @@ -138,7 +133,7 @@ def resolve_for_pressing_event event, pressed_keys class Leaf attr_reader :payload - def initialize payload + def initialize(payload) @payload = payload end end diff --git a/lib/rbindkeys/cli.rb b/lib/rbindkeys/cli.rb index d2755b6..fd8a7ea 100644 --- a/lib/rbindkeys/cli.rb +++ b/lib/rbindkeys/cli.rb @@ -1,46 +1,43 @@ # -*- coding:utf-8; mode:ruby; -*- module Rbindkeys - - SUMMARY = 'key remapper for Linux which is configured in ruby' + SUMMARY = 'key remapper for Linux which is configured in ruby'.freeze # a class is executed by bin/rbindkeys class CLI + EVDEVS = '/dev/input/event*'.freeze - EVDEVS = '/dev/input/event*' - - class << self - require 'optparse' + # if @cmd == :observe then CLI execute to observe a given event device + # else if @cmd == :ls then CLI list event devices + # (default: :observe) + @cmd = :observe - # if @@cmd == :observe then CLI excecute to observe a given event device - # else if @@cmd == :ls then CLI list event devices - # (default: :observe) - @@cmd = :observe - def cmd; @@cmd end + # a location of a config file (default: "~/.rbindkeys.rb") + @config = "#{ENV['HOME']}/.rbindkeys.rb" - # a location of a config file (default: "~/.rbindkeys.rb") - @@config = "#{ENV['HOME']}/.rbindkeys.rb" - def config; @@config end + @usage = SUMMARY - @@usage = SUMMARY + class << self + require 'optparse' + attr_reader :cmd, :config - def main args + def main(args) begin parse_opt args rescue OptionParser::ParseError => e - puts "ERROR #{e.to_s}" + puts "ERROR #{e}" err end - method(@@cmd).call(args) + method(@cmd).call(args) end - def err code=1 - puts @@usage + def err(code=1) + puts @usage exit code end - def parse_opt args + def parse_opt(args) opt = OptionParser.new < "+ - "#{@bind_resolver}" if LOG.info? + LOG.info "switch bind_resolver: #{old_resolver} => " + + @bind_resolver.to_s if LOG.info? @bind_resolver end - def handle_press_event event + def handle_press_event(event) r = @bind_resolver.resolve event.code, @pressed_key_set LOG.debug "resolve result: #{r.inspect}" if LOG.debug? - if r.kind_of? KeyBind + if r.is_a? KeyBind if @bind_resolver.two_stroke? set_bind_resolver (@window_bind_resolver or @default_bind_resolver) end - if r.output.kind_of? Array + if r.output.is_a? Array r.input.reject {|c| c == event.code }.each {|c| @operator.release_key c } - r.output.each {|c| @operator.press_key c} + r.output.each {|c| @operator.press_key c } @active_bind_set << r :ignore - elsif r.output.kind_of? BindResolver + elsif r.output.is_a? BindResolver set_bind_resolver r.output :ignore - elsif r.output.kind_of? Proc + elsif r.output.is_a? Proc r.output.call event, @operator - elsif r.output.kind_of? Symbol + elsif r.output.is_a? Symbol r end else @@ -143,7 +141,7 @@ def handle_press_event event end end - def handle_pressing_event event + def handle_pressing_event(_event) if @active_bind_set.empty? :through else @@ -152,19 +150,17 @@ def handle_pressing_event event end end - def fill_gap_pressed_state event + def fill_gap_pressed_state(event) return if @operator.pressed_key_set == @pressed_key_set sub = @pressed_key_set - @operator.pressed_key_set - if event.value == 0 - sub.delete event.code - end - sub.each {|code| @operator.press_key code} + sub.delete event.code if event.value == 0 + sub.each {|code| @operator.press_key code } end - def handle_pressed_keys event + def handle_pressed_keys(event) if event.value == 1 @pressed_key_set << event.code - @pressed_key_set.sort! # TODO do not sort. implement an bubble insertion + @pressed_key_set.sort! # TODO: do not sort. implement an bubble insertion elsif event.value == 0 if @pressed_key_set.delete(event.code).nil? LOG.warn "#{event.code} does not exists on @pressed_keys" if LOG.warn? @@ -172,64 +168,63 @@ def handle_pressed_keys event end end - def active_window_changed window + def active_window_changed(window) if not window.nil? title = window.title app_name = window.app_name if LOG.info? - LOG.info "" unless LOG.debug? + LOG.info '' unless LOG.debug? LOG.info "change active_window: :class => \"#{app_name}\", :title => \"#{title}\"" end @window_bind_resolver_map.each do |matcher, bind_resolver| - if matcher.match? app_name, title - if LOG.info? - LOG.info "=> matcher #{matcher.app_name.inspect}, #{matcher.title.inspect}" - LOG.info " bind_resolver #{bind_resolver.inspect}" - end - set_bind_resolver bind_resolver - @window_bind_resolver = bind_resolver - return + next unless matcher.match? app_name, title + if LOG.info? + LOG.info "=> matcher #{matcher.app_name.inspect}, #{matcher.title.inspect}" + LOG.info " bind_resolver #{bind_resolver.inspect}" end + set_bind_resolver bind_resolver + @window_bind_resolver = bind_resolver + return end else if LOG.info? - LOG.info "" unless LOG.debug? - LOG.info "change active_window: nil" + LOG.info '' unless LOG.debug? + LOG.info 'change active_window: nil' end end - LOG.info "=> no matcher" if LOG.info? + LOG.info '=> no matcher' if LOG.info? set_bind_resolver @default_bind_resolver @window_bind_resolver = nil - return + nil end class << self # parse and normalize to Fixnum/Array - def parse_code code, depth = 0 - if code.kind_of? Symbol + def parse_code(code, depth=0) + if code.is_a? Symbol code = parse_symbol code - elsif code.kind_of? Array - raise ArgumentError, "expect Array is the depth less than 1" if depth >= 1 - code.map!{|c| parse_code c, (depth+1)} - elsif code.kind_of? Fixnum and depth == 0 + elsif code.is_a? Array + fail ArgumentError, 'expect Array is the depth less than 1' if depth >= 1 + code.map! {|c| parse_code c, (depth + 1) } + elsif code.is_a? Fixnum and depth == 0 code = [code] - elsif not code.kind_of? Fixnum - raise ArgumentError, "expect Symbol / Fixnum / Array" + elsif not code.is_a? Fixnum + fail ArgumentError, 'expect Symbol / Fixnum / Array' end code end - # TODO convert :j -> KEY_J, :ctrl -> KEY_LEFTCTRL - def parse_symbol sym - if not sym.kind_of? Symbol - raise ArgumentError, "expect Symbol / Fixnum / Array" + # TODO: convert :j -> KEY_J, :ctrl -> KEY_LEFTCTRL + def parse_symbol(sym) + unless sym.is_a? Symbol + fail ArgumentError, 'expect Symbol / Fixnum / Array' end Revdev.const_get sym end - def get_state_by_value ev + def get_state_by_value(ev) case ev.value when 0 then 'released ' when 1 then 'pressed ' @@ -237,7 +232,5 @@ def get_state_by_value ev end end end - end - end diff --git a/lib/rbindkeys/key_event_handler/configurer.rb b/lib/rbindkeys/key_event_handler/configurer.rb index 6ec0e74..deb08f8 100644 --- a/lib/rbindkeys/key_event_handler/configurer.rb +++ b/lib/rbindkeys/key_event_handler/configurer.rb @@ -5,15 +5,14 @@ module Rbindkeys class KeyEventHandler - # pre-prosessed key codes replacement for all inputs - def pre_bind_key input, output - if not (input.kind_of? Fixnum and output.kind_of? Fixnum) - raise ArgumentError, 'expect Fixnum for input and output' + def pre_bind_key(input, output) + unless input.is_a? Fixnum and output.is_a? Fixnum + fail ArgumentError, 'expect Fixnum for input and output' end - if @pre_bind_resolver.has_key? input - raise DuplicateNodeError, "1st arg (#{input}) was already entried" + if @pre_bind_resolver.key? input + fail DuplicateNodeError, "1st arg (#{input}) was already entried" end LOG.info "pre_bind_key #{input.inspect},\t#{output.inspect}" if LOG.info? @@ -23,23 +22,23 @@ def pre_bind_key input, output end # define a new key binding - def bind_key input, output=nil, resolver=@bind_resolver, &block - if input.kind_of?(Array) or input.kind_of?(Fixnum) + def bind_key(input, output=nil, resolver=@bind_resolver, &block) + if input.is_a? Array or input.is_a? Fixnum input = KeyEventHandler.parse_code input else - raise ArgumentError, '1st arg expect Array / Fixnum' + fail ArgumentError, '1st arg expect Array / Fixnum' end if block_given? output = block elsif output.nil? - raise ArgumentError, 'expect 1 arg with a block / 2 args' + fail ArgumentError, 'expect 1 arg with a block / 2 args' elsif output.kind_of? BindResolver - elsif output.kind_of?(Array) or output.kind_of?(Fixnum) - output = KeyEventHandler::parse_code output + elsif output.is_a? Array or output.is_a? Fixnum + output = KeyEventHandler.parse_code output elsif output == :through or output == :ignore else - raise ArgumentError, '2nd arg expect Array / Fixnum / BindResolver / '+ + fail ArgumentError, '2nd arg expect Array / Fixnum / BindResolver / ' + 'Symbol(:through/:ignore)' end @@ -52,22 +51,20 @@ def bind_key input, output=nil, resolver=@bind_resolver, &block # _input_ :: prefix key. (e.g. [KEY_LEFTCTRL, KEY_X] (C-x) # _resolver_ :: upper bind_resolver for fall throught # _block_ :: to define sub-binds on this prefix key bind - def bind_prefix_key input, resolver=@bind_resolver, &block - if not block_given? - raise ArgumentError, "expect a block" - end + def bind_prefix_key(input, resolver=@bind_resolver, &_block) + fail ArgumentError, 'expect a block' unless block_given? - input = KeyEventHandler::parse_code input + input = KeyEventHandler.parse_code input LOG.info "bind_prefix_key #{input.inspect}\t#{resolver}" if LOG.info? tmp = input.clone tail_input = tmp.pop binded_resolver = resolver.just_resolve tail_input, tmp - if binded_resolver == nil + if binded_resolver.nil? binded_resolver = BindResolver.new :ignore, true resolver.bind input, binded_resolver elsif not binded_resolver.kind_of? BindResolver - raise DuplicateNodeError, "the codes (#{input.inspect}) was already binded" + fail DuplicateNodeError, "the codes (#{input.inspect}) was already binded" end @bind_resolver = binded_resolver @@ -76,10 +73,8 @@ def bind_prefix_key input, resolver=@bind_resolver, &block binded_resolver end - def new_bind_resolver upper_resolver=@bind_resolver, &block - if not block_given? - raise ArgumentError, "expect a block" - end + def new_bind_resolver(upper_resolver=@bind_resolver, &_block) + fail ArgumentError, 'expect a block' unless block_given? new_resolver = BindResolver.new upper_resolver @@ -102,27 +97,27 @@ def new_bind_resolver upper_resolver=@bind_resolver, &block # :title's Regexp AND the window class match with :class's Regexp. # if a regexp was given, this bind is active when the window title match # with the given regexp - def window upper_resolver, arg + def window(upper_resolver, arg) if upper_resolver.nil? upper_resolver = @bind_resolver - elsif upper_resolver.kind_of? Symbol + elsif upper_resolver.is_a? Symbol upper_resolver = FixResolver.instance upper_resolver elsif not upper_resolver.kind_of? BindResolver - raise ArgumentError, "1nd argument is expected to be a BindResolver or"+ - " a Symbol : #{ upper_resolver.to_s}" + fail ArgumentError, '1nd argument is expected to be a BindResolver or' + + " a Symbol : #{upper_resolver}" end - if arg.kind_of? Regexp + if arg.is_a? Regexp arg = { :title => arg } - elsif arg.kind_of? Hash + elsif arg.is_a? Hash arg.each do |k, v| - if not (k.kind_of?(Symbol) and v.kind_of?(Regexp)) - raise ArgumentError, 'the 2nd argument Hash must only have'+ + unless k.is_a? Symbol and v.is_a? Regexp + fail ArgumentError, 'the 2nd argument Hash must only have' + " Symbol keys and Regexp values : #{arg.inspect}" end end else - raise ArgumentError, "2nd argument is expected to be a Hash or a Regexp : #{arg}" + fail ArgumentError, "2nd argument is expected to be a Hash or a Regexp : #{arg}" end resolver = BindResolver.new(upper_resolver) @@ -137,6 +132,5 @@ def window upper_resolver, arg resolver end - end end diff --git a/lib/rbindkeys/log_utils.rb b/lib/rbindkeys/log_utils.rb index 3e90318..fb66bc5 100644 --- a/lib/rbindkeys/log_utils.rb +++ b/lib/rbindkeys/log_utils.rb @@ -3,49 +3,40 @@ require 'logger' module Rbindkeys - class LogUtils - DEFAULT_LOG_OUTPUT = STDOUT DEFAULT_FORMAT = :simple DEFAULT_LEVEL = Logger::INFO - @@output = DEFAULT_LOG_OUTPUT - @@format = DEFAULT_FORMAT - @@level = DEFAULT_LEVEL + @output = DEFAULT_LOG_OUTPUT + @format = DEFAULT_FORMAT + @level = DEFAULT_LEVEL class << self + attr_accessor :output, :format, :level - def get_logger progname - logger = Logger.new @@output + def get_logger(progname) + logger = Logger.new @output logger.progname = progname - logger.level = @@level + logger.level = @level set_formatter logger logger end - def set_formatter logger - case @@format + def set_formatter(logger) + case @format when :simple - logger.formatter = proc do |sev, date, prog, msg| + logger.formatter = proc do |_sev, _date, _prog, msg| "* #{msg}\n" end when :default else l = Logger.new STDERR - l.fatal "unknown logger format" + l.fatal 'unknown logger format' exit false end end - - # setter and reader methods for the class variables - [:output, :format, :level].each do |name| - eval "def #{name}; @@#{name} end" - eval "def #{name}= o; @@#{name} = o end" - end - end end - end diff --git a/lib/rbindkeys/observer.rb b/lib/rbindkeys/observer.rb index d41f427..c947de2 100644 --- a/lib/rbindkeys/observer.rb +++ b/lib/rbindkeys/observer.rb @@ -1,15 +1,15 @@ # -*- coding:utf-8; mode:ruby; -*- -require "revdev" +require 'revdev' +require 'active_window_x' module Rbindkeys - # main event loop class class Observer include Revdev LOG = LogUtils.get_logger name - VIRTUAL_DEVICE_NAME = "rbindkyes" + VIRTUAL_DEVICE_NAME = 'rbindkeys'.freeze DEFAULT_TIMEOUT = 0.5 attr_reader :device @@ -17,7 +17,7 @@ class Observer attr_reader :event_handler attr_reader :config_file - def initialize config_name, device_location + def initialize(config_name, device_location) @device = Device.new device_location @virtual = VirtualDevice.new operator = DeviceOperator.new @device, @virtual @@ -33,11 +33,11 @@ def start @device.grab @device.release_all_key - @virtual.create VIRTUAL_DEVICE_NAME #, @device.device_id + @virtual.create VIRTUAL_DEVICE_NAME # , @device.device_id @event_handler.load_config @config_file - @event_handler.bind_resolver.tree.each do |k,v| + @event_handler.bind_resolver.tree.each do |k, v| puts "#{k} => #{v.inspect}" end @@ -49,7 +49,7 @@ def start # start main loop @started = true - while true + loop do ios = select_ios if ios.nil? # select timeout @@ -58,7 +58,7 @@ def start end if LOG.debug? - LOG.debug "" + LOG.debug '' LOG.debug "select => #{ios.inspect}" end @@ -106,39 +106,37 @@ def handle_device_event end end - def destroy *args - if not @started + def destroy(*_args) + unless @started LOG.error 'did not start to observe' return end begin - LOG.info "try @device.ungrab" + LOG.info 'try @device.ungrab' @device.ungrab - LOG.info "=> success" + LOG.info '=> success' rescue => e LOG.error e end begin - LOG.info "try @virtural.destroy" + LOG.info 'try @virtural.destroy' @virtual.destroy - LOG.info "=> success" + LOG.info '=> success' rescue => e LOG.error e end begin - LOG.info "try @window_observer.destory" + LOG.info 'try @window_observer.destory' @window_observer.destroy - LOG.info "=> success" + LOG.info '=> success' rescue => e LOG.error e end exit true end - end # of class - end # of module Rbindkeys diff --git a/lib/rbindkeys/version.rb b/lib/rbindkeys/version.rb index bf06a3f..adf5b05 100644 --- a/lib/rbindkeys/version.rb +++ b/lib/rbindkeys/version.rb @@ -1,3 +1,3 @@ module Rbindkeys - VERSION = "0.1.0" + VERSION = '0.1.0'.freeze end diff --git a/lib/rbindkeys/virtual_device.rb b/lib/rbindkeys/virtual_device.rb index 7d0c1ef..f7b6a96 100644 --- a/lib/rbindkeys/virtual_device.rb +++ b/lib/rbindkeys/virtual_device.rb @@ -1,14 +1,10 @@ # -*- coding:utf-8; mode:ruby; -*- -require "ruinput" +require 'ruinput' module Rbindkeys - class VirtualDevice < Ruinput::UinputDevice - def release_all end - end - end diff --git a/lib/rbindkeys/window_matcher.rb b/lib/rbindkeys/window_matcher.rb index afabf70..79f5cda 100644 --- a/lib/rbindkeys/window_matcher.rb +++ b/lib/rbindkeys/window_matcher.rb @@ -5,31 +5,29 @@ module Rbindkeys class WindowMatcher - attr_reader :app_name, :title - def initialize h + def initialize(h) @app_name = (h[:class] or h[:app_name] or h[:app_class]) @title = (h[:title] or h[:name]) if not @app_name.nil? and not @title.nil? - raise ArgumentError, 'expect to be given :class, :app_name,'+ + fail ArgumentError, 'expect to be given :class, :app_name,' + ' :app_class, :title or :name ' end end - def match? app_name, title + def match?(app_name, title) (@app_name.nil? or match_app?(app_name)) and (@title.nil? or match_title?(title)) end - def match_app? app_name + def match_app?(app_name) app_name and app_name.match @app_name end - def match_title? title + def match_title?(title) title and title.match @title end - end end