Module: Lijab::Commands

Defined in:
lib/lijab/commands.rb,
lib/lijab/commands/simple.rb,
lib/lijab/commands/status.rb,
lib/lijab/commands/options.rb,
lib/lijab/commands/contacts.rb,
lib/lijab/commands/subscription.rb

Defined Under Namespace

Modules: CommandMixin, ContactCompleterMixin, ContactsCommandMixin Classes: Command, CommandError

Class Method Summary collapse

Class Method Details

.completer(line) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lijab/commands.rb', line 121

def completer(line)
   cmd, args = line[1..-1].split(" ", 2).strip
   cmd ||= ""

   matches = @registered.keys.find_all { |c| c.to_s.match(/^#{Regexp.escape(cmd)}/) }

   if !cmd.empty? && (matches.length == 1 || args) && registered?(cmd)
      (@registered[cmd.to_sym].completer(line) || []).map { |s| "/#{cmd} #{s}" }
   else
      matches.map { |k| "/#{k}" }
   end
end

.get(name) ⇒ Object



98
99
100
# File 'lib/lijab/commands.rb', line 98

def get(name)
   @registered[name.to_sym]
end

.initObject



57
58
59
60
61
62
63
64
65
# File 'lib/lijab/commands.rb', line 57

def init
   files = Dir[File.join(File.dirname(File.expand_path(__FILE__)), 'commands', '*.rb')] + \
           Dir[File.join(Config.dirs[:commands], '**', '*.rb')]

   files.each { |f| load f }
   Config.opts[:aliases].each do |a, c|
      register_alias(a, c)
   end
end

.register(name, cmd) ⇒ Object



67
68
69
70
71
# File 'lib/lijab/commands.rb', line 67

def register(name, cmd)
   name = name.to_sym
   @overloaded[name] = @registered[name] if @registered.key?(name)
   @registered[name] = cmd
end

.register_alias(name, s) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lijab/commands.rb', line 73

def register_alias(name, s)
   alias_cmd, alias_args = s.split(" ", 2)
   alias_cmd.strip!
   alias_cmd = alias_cmd[1..-1] if alias_cmd[0] == ?/
   name = name[1..-1] if name[0] == ?/

   Command.define name.to_sym do
      description %{Alias for "#{s}"}
      @alias_cmd = alias_cmd
      @alias_args = alias_args
      @name = name

      def run(args)
         Commands::run(@alias_cmd, [@alias_args, args].join(" "), true)
      end

      def completer(line)
         args = line.split(" ", 2)[1]
         Commands::completer("/#{@alias_cmd} #{@alias_args} #{args}").map do |r|
            r.gsub(/\/#{@alias_cmd}\s?/, "")
         end
      end
   end
end

.registeredObject

.registered?(name) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/lijab/commands.rb', line 102

def registered?(name)
   @registered.key?(name.to_sym)
end

.run(cmd, args = "", is_alias = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lijab/commands.rb', line 106

def run(cmd, args="", is_alias=false)
   cmd = cmd.strip.to_sym
   command = @overloaded[cmd] if is_alias
   command ||= @registered[cmd]
   if command
      begin
         command.run(args.strip)
      rescue CommandError => e
         Out::error("#{cmd}: #{e}", false)
      end
   else
      Out::error("no such command: /#{cmd}", false)
   end
end