Class: Rbindkeys::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rbindkeys/cli.rb,
lib/rbindkeys.rb

Overview

a class is executed by bin/rbindkeys

Constant Summary collapse

EVDEVS =
'/dev/input/event*'
@@cmd =

if @@cmd == :observe then CLI excecute to observe a given event device else if @@cmd == :ls then CLI list event devices (default: :observe)

:observe
@@config =

a location of a config file (default: “~/.rbindkeys.rb”)

"#{ENV['HOME']}/.rbindkeys.rb"
@@usage =
SUMMARY

Class Method Summary collapse

Class Method Details

.cmdObject



19
# File 'lib/rbindkeys/cli.rb', line 19

def cmd; @@cmd end

.configObject



23
# File 'lib/rbindkeys/cli.rb', line 23

def config; @@config end

.err(code = 1) ⇒ Object



38
39
40
41
# File 'lib/rbindkeys/cli.rb', line 38

def err code=1
  puts @@usage
  exit code
end

.ls(args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rbindkeys/cli.rb', line 74

def ls args
  require 'revdev'
  Dir::glob(EVDEVS).sort do |a,b|
    am = a.match(/[0-9]+$/)
    bm = b.match(/[0-9]+$/)
    ai = am[0] ? am[0].to_i : 0
    bi = bm[0] ? bm[0].to_i : 0
    ai <=> bi
  end.each do |f|
    begin
      e = Revdev::EventDevice.new f
      puts "#{f}:	#{e.device_name} (#{e.device_id.hr_bustype})"
    rescue => ex
      puts ex
    end
  end
end

.main(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rbindkeys/cli.rb', line 27

def main args
  begin
    parse_opt args
  rescue OptionParser::ParseError => e
    puts "ERROR #{e.to_s}"
    err
  end

  method(@@cmd).call(args)
end

.observe(args) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/rbindkeys/cli.rb', line 65

def observe args
  if args.length != 1
    puts 'ERROR invalid arguments'
    err
  end
  evdev = args.first
  Observer.new(@@config, evdev).start
end

.parse_opt(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbindkeys/cli.rb', line 43

def parse_opt args
  opt = OptionParser.new <<BANNER
#{SUMMARY}
Usage: sudo #{$0} [--config file] #{EVDEVS}
   or: sudo #{$0} --evdev-list
BANNER
  opt.version = VERSION
  opt.on '-l', '--evdev-list', 'a list of event devices' do
    @@cmd = :ls
  end
  opt.on '-c VAL', '--config VAL', 'specifying your configure file' do |v|
    @@config = v
  end
  opt.on '-e', '--print-example', 'print an example config' do |v|
    @@cmd = :print_example
  end

  opt.parse! args

  @@usage = opt.help
end


92
93
94
95
96
97
98
99
# File 'lib/rbindkeys/cli.rb', line 92

def print_example args
  dir = File.dirname File.expand_path __FILE__
  dir = File.expand_path File.join dir, '..', '..', 'sample'
  file = File.join dir, 'emacs.rb'
  IO.foreach file do |line|
    puts "# #{line}"
  end
end