class Termtter::Command

Attributes

aliases[RW]
author[RW]
completion_proc[RW]
exec_proc[RW]
help[RW]
name[RW]

Public Class Methods

new(args) click to toggle source

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

Public Instance Methods

alias= :: Symbol → () click to toggle source
# File lib/termtter/command.rb, line 103
def alias=(a)
  self.aliases = [a]
end
aliases=(as) click to toggle source
# File lib/termtter/command.rb, line 97
def aliases=(as)
  @aliases = as.map { |a| a.to_sym }
end
author= :: String → () click to toggle source
# File lib/termtter/command.rb, line 109
def author=(a)
  @author = a
end
call :: ??? click to toggle source
# 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
command_words() click to toggle source
# File lib/termtter/command.rb, line 113
def command_words
  name.to_s.split(/\s+/)
end
commands :: [Symbol] click to toggle source
# File lib/termtter/command.rb, line 93
def commands
  [name] + aliases
end
complement :: String → [String] click to toggle source
# 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
match? :: String → Boolean click to toggle source
# File lib/termtter/command.rb, line 80
def match?(input)
  !!pattern.match(input)
end
pattern :: Regexp click to toggle source
# 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
set :: Hash → () click to toggle source
# File lib/termtter/command.rb, line 32
def set(cfg)
  self.name             = cfg[:name].to_sym
  self.aliases          = cfg[:aliases]
  self.exec_proc        = cfg[:exec_proc]
  self.completion_proc  = cfg[:completion_proc]
  self.help             = cfg[:help]
  self.author           = cfg[:author]
end
split_command_line :: String → (String, String) click to toggle source
# File lib/termtter/command.rb, line 119
def split_command_line(line)
  m = pattern.match(line)
  if m
    unless m[2].nil?
      [m[2], '']
    else
      [m[3], m[4]]
    end
  else
    []
  end
end