Module: I3::Runner

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

Constant Summary collapse

OUTPUT =
$stdout

Instance Method Summary collapse

Instance Method Details

#execute(*args) ⇒ Object



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
95
96
97
98
99
# File 'lib/i3-ipc/runner.rb', line 33

def execute(*args)
  socket_file = nil
  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 `i3 --get-socketpath` output'
    opts.on('-s SOCKET', '--socket SOCKET', s_desc) do |s|
      socket_file = File.expand_path(s)
    end

    t_desc = 'Set type, 0 = command, 1 = workspace list, 2 = subscribe to workspace event, 3 = output list, 4 = tree 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
      OUTPUT.puts opts
      exit
    end
  end

  opts.parse!(args)

  socket_file ||= I3::IPC.socket_path

  if type == I3::IPC.message_type_subscribe
    subscribe socket_file, output
  else
    i3 = I3::IPC.new(socket_file)
    arg = I3::IPC::COMMANDS.find {|t| t.first == type}
    if arg
      if arg.last == :none
        format_output i3.send(arg[1]), output, quiet
      elsif arg.last == :required
        payload = args.shift
        if payload.nil?
          abort "error: payload needed."
        else
          format_output i3.send(arg[1], payload), output, quiet
        end
      elsif arg.last == :optional
        payload = args.shift
        format_output i3.send(arg[1], payload), output, quiet
      end
    end
  end
end

#format_output(object, output, quiet) ⇒ Object



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

def format_output(object, output, quiet)
  return if quiet
  if output == :pretty_print
    PP.pp(object, OUTPUT)
  elsif output == :json
    require 'json'
    OUTPUT.puts object.to_json
  else
    OUTPUT.puts object.inspect
  end
end

#subscribe(path, output) ⇒ Object



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

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