args
name: (required) Symbol as command name aliases: Array of command alias (ex. ['u', 'up']) exec_proc: Proc for procedure of the command. If need the proc must return object for hook. completion_proc: Proc for input completion. The proc must return Array of candidates (Optional) help: Help text for the command (Optional) author: The author's name (Optional)
# File lib/termtter/command.rb, line 13 def initialize(args) raise ArgumentError, ":name is not given." unless args.has_key?(:name) args = args.dup args[:exec_proc] ||= args[:exec] args[:completion_proc] ||= args[:completion] args[:aliases] ||= [args[:alias]].compact cfg = { :aliases => [], :exec_proc => lambda {|arg| }, :comletion_proc => lambda {|command, arg| [] }, :author => 'ujihisa', }.merge(args) {|k, v1, v2| v2 ? v2 : v1 } set cfg end
# File lib/termtter/command.rb, line 103 def alias=(a) self.aliases = [a] end
# File lib/termtter/command.rb, line 97 def aliases=(as) @aliases = as.map { |a| a.to_sym } end
# File lib/termtter/command.rb, line 59 def call(cmd = nil, arg = nil, original_text = nil) from = Time.now arg = case arg when nil '' when String arg else raise ArgumentError, 'arg should be String or nil' end Termtter::Client.logger.debug { "command: #{cmd} #{arg}" } result = exec_proc.call(arg) Termtter::Client.logger.debug { "command: #{cmd} #{arg} #{'%.2fsec' % (Time.now - from)}" } result rescue => e Termtter::Client.logger.debug { "command: #{cmd} #{arg} #{e.message} #{'%.2fsec' % (Time.now - from)}" } raise e end
# File lib/termtter/command.rb, line 113 def command_words name.to_s.split(/\s+/) end
# File lib/termtter/command.rb, line 93 def commands [name] + aliases end
# File lib/termtter/command.rb, line 43 def complement(input) input = input.sub(/^\s*/, '') if match?(input) && input =~ /^[^\s]+\s/ if completion_proc command_str, command_arg = split_command_line(input) [completion_proc.call(command_str, command_arg || '')].flatten.compact else [] end else [] end end
# File lib/termtter/command.rb, line 80 def match?(input) !!pattern.match(input) end
# File lib/termtter/command.rb, line 86 def pattern commands_regex = commands.map {|name| name.to_s.split(' ').map {|i| Regexp.quote(i)}.join('\s+') }.join('|') /^\s*((#{commands_regex})|(#{commands_regex})(?:\s+|\b)(.*?))\s*$/ end
Generated with the Darkfish Rdoc Generator 2.