Module: I3::Runner

Extended by:
Runner
Included in:
Runner
Defined in:
lib/i3-ipc/runner.rb

Instance Method Summary collapse

Instance Method Details

#execute(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/i3-ipc/runner.rb', line 31

def execute(*args)
  socket_file = '/tmp/i3-ipc.sock'
  type = 0
  quiet = false
  output = :default

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: i3-ipc [options] [message]"

    s_desc = 'Set socket file, defaults to /tmp/i3-ipc.sock'
    opts.on('-s', '--socket', s_desc) do |s|
      socket_file = s
    end

    t_desc = 'Set type, 0 = command, 1 = workspace list, 2 = subscribe to workspace event, 3 = output list, default: 0'
    opts.on('-tTYPE', '--type TYPE', Integer, t_desc) do |t|
      type = t
    end

    opts.on('-p', '--pretty-print', 'Pretty print reply') do |p|
      output = :pretty_print
    end

    opts.on('-j', '--json', 'Output raw json') do |p|
      output = :json
    end

    opts.on('-q', '--quiet', %(Don't show reply)) do |q|
      quiet = q
    end

    opts.on('-m', '--man', 'Print manual') do
      I3::Manpage.display("i3-ipc")
    end

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end

  opts.parse!(args)

  s = I3::IPC.new(socket_file)

  case type
  when 0
    if args.empty?
      abort "error: message type needs a message"
    end

    payload = args.shift

    puts s.command(payload) unless quiet
  when I3::IPC::MESSAGE_TYPE_GET_WORKSPACES
    format_output s.get_workspaces, output
  when I3::IPC::MESSAGE_REPLY_SUBSCRIBE
    subscribe socket_file, output
  when I3::IPC::MESSAGE_TYPE_GET_OUTPUTS
    format_output s.get_outputs, output
  else
    abort "error: type #{type} not yet implemented"
  end
end

#format_output(object, output) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/i3-ipc/runner.rb', line 8

def format_output(object, output)
  if output == :pretty_print
    pp object
  elsif output == :json
    require 'json'
    puts object.to_json
  else
    p object
  end
end

#subscribe(path, output) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/i3-ipc/runner.rb', line 19

def subscribe(path, output)
  trap('SIGINT') { EM.stop; puts }
  I3::IPC.subscribe([:workspace], path) do |em, type, data|
    case type
    when I3::IPC::MESSAGE_REPLY_GET_WORKSPACES
      format_output data, output
    when I3::IPC::EVENT_WORKSPACE
      em.send_data I3::IPC.format(I3::IPC::MESSAGE_TYPE_GET_WORKSPACES)
    end
  end
end