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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/i3-ipc/runner.rb', line 7

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, defaults to 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)

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

    payload = args.shift

    puts s.command(payload) unless quiet
  elsif type == 1
    workspaces = s.get_workspaces
    if output == :pretty_print
      require 'pp'
      pp workspaces
    elsif output == :json
      require 'json'
      puts workspaces.to_json
    else
      p workspaces
    end
  else
    abort "error: type #{type} not yet implemented"
  end
end