Class: Weechat::Command

Inherits:
Hook show all
Defined in:
lib/weechat/command.rb

Instance Attribute Summary collapse

Attributes inherited from Hook

#callback, #id

Attributes included from Pointer

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hook

#call, compute_free_id, find_by_id, #hooked?, hooks, inherited, init, register, #unhook, unhook, unhook_all, unregister

Methods included from Pointer

#==, #hash, #inspect, #to_s

Constructor Details

#initialize(*args, &callback) ⇒ Command

Returns a new instance of Command.



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
# File 'lib/weechat/command.rb', line 8

def initialize(*args, &callback)
  raise "No callback specified" if callback.nil?
  super

  if args.size == 2
    @command, @description = args
  elsif args.size == 1 && args[0].is_a?(Hash)
    @command, @description, @args, =
    args[0].values_at(:command, :description, :args)

    @completion = case args[0][:completion]
                  when Array
                    args[0][:completion].join(" || ")
                  else
                    args[0][:completion].to_s
                  end

    case args[0][:args_description]
    when Hash
      lines = []
      color = Weechat.color("white")
      reset = Weechat.color("reset")
      max_length = args[0][:args_description].keys.map {|k| k.size}.max
      args[0][:args_description].each do |key, value|
        key = (" " * (max_length - key.size)) + key
        lines << "#{color}#{key}: #{reset}#{value}"
      end
      @args_description = lines.join("\n")
    when Array
      @args_description = args[0][:args_description].join("\n")
    else
      @args_description = args[0][:args_description].to_s
    end

  else
    raise "Please supply two arguments or a hash"
  end

  @command[0..0] = '' if @command[0..0] == '/'
  @callback         = EvaluatedCallback.new(callback)
  @ptr              = Weechat.hook_command(@command,
                                   @description.to_s,
                                   @args.to_s,
                                   @args_description.to_s,
                                   @completion.to_s,
                                   "command_callback",
                                   id.to_s)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/weechat/command.rb', line 5

def args
  @args
end

#args_descriptionObject (readonly)

Returns the value of attribute args_description.



6
7
8
# File 'lib/weechat/command.rb', line 6

def args_description
  @args_description
end

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/weechat/command.rb', line 3

def command
  @command
end

#completionObject (readonly)

Returns the value of attribute completion.



7
8
9
# File 'lib/weechat/command.rb', line 7

def completion
  @completion
end

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/weechat/command.rb', line 4

def description
  @description
end

Class Method Details

.find_by_command(name) ⇒ Object Also known as: find_by_name



58
59
60
# File 'lib/weechat/command.rb', line 58

def find_by_command(name)
  @hooks.values.find {|h| h.command == name}
end