Class: Weechat::Command

Inherits:
Hook
  • Object
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

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

Methods included from Pointer

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

Constructor Details

#new(name, description) {|buffer, input| ... } ⇒ Command #new(opts) {|buffer, input| ... } ⇒ Command

Creates a new command, which can then be invoked with /command-name args also will register help for the command

Overloads:

  • #new(name, description) {|buffer, input| ... } ⇒ Command

    Creates a command with a name and description

    Parameters:

    • name (String)

      The name of the command

    • description (String)

      The description of the command, will appear in help

    Yields:

    • (buffer, input)

      The action that is performed when the command is run

    Yield Parameters:

    • buffer (Buffer)

      The buffer the command was run in

    • input (String)

      The string passed to the command (or “” if no string was passed)

  • #new(opts) {|buffer, input| ... } ⇒ Command

    Create a command using various options

    Parameters:

    • initialize(opts) (Hash)

    Options Hash (opts):

    • :name (String)

      The name of the command

    • :description (String)

      The description of the command

    • :completion (Array, #to_s)

      TODO document

    • :args_description (Hash, Array, #to_s)

      TODO document

    Yields:

    • (buffer, input)

      The action that is performed when the command is run

    Yield Parameters:

    • buffer (Buffer)

      The buffer the command was run in

    • input (String)

      The string passed to the command (or “” if no string was passed)



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

def initialize(*args, &callback)
  args = args.map{|e| e.dup}
  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



78
79
80
81
# File 'lib/weechat/command.rb', line 78

def find_by_command(name)
  name[0..0] = '' if name[0..0] == '/'
  @hooks.values.find {|h| h.command == name}
end