Method: I3::Runner#execute

Defined in:
lib/i3-ipc/runner.rb

#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