Class: Termtter::Command
- Inherits:
-
Object
- Object
- Termtter::Command
- Defined in:
- lib/termtter/command.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#author ⇒ Object
Returns the value of attribute author.
-
#completion_proc ⇒ Object
Returns the value of attribute completion_proc.
-
#exec_proc ⇒ Object
Returns the value of attribute exec_proc.
-
#help ⇒ Object
Returns the value of attribute help.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#alias=(a) ⇒ Object
- call-seq: alias=
-
Symbol -> ().
-
#call(cmd = nil, arg = nil, original_text = nil) ⇒ Object
- call-seq: call
-
???.
- #command_words ⇒ Object
-
#commands ⇒ Object
- call-seq: commands
-
[Symbol].
-
#complement(input) ⇒ Object
- call-seq: complement
-
String -> [String].
-
#initialize(args) ⇒ Command
constructor
args name: (required) Symbol as command name aliases: Array of command alias (ex. [‘u’, ‘up’]) exec_proc: Proc for procedure of the command.
-
#match?(input) ⇒ Boolean
- call-seq: match?
-
String -> Boolean.
-
#pattern ⇒ Object
- call-seq: pattern
-
Regexp.
-
#set(cfg) ⇒ Object
- call-seq: set
-
Hash -> ().
-
#split_command_line(line) ⇒ Object
- call-seq: split_command_line
-
String -> (String, String).
Constructor Details
#initialize(args) ⇒ Command
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)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/termtter/command.rb', line 14 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 |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
5 6 7 |
# File 'lib/termtter/command.rb', line 5 def aliases @aliases end |
#author ⇒ Object
Returns the value of attribute author.
5 6 7 |
# File 'lib/termtter/command.rb', line 5 def @author end |
#completion_proc ⇒ Object
Returns the value of attribute completion_proc.
5 6 7 |
# File 'lib/termtter/command.rb', line 5 def completion_proc @completion_proc end |
#exec_proc ⇒ Object
Returns the value of attribute exec_proc.
5 6 7 |
# File 'lib/termtter/command.rb', line 5 def exec_proc @exec_proc end |
#help ⇒ Object
Returns the value of attribute help.
5 6 7 |
# File 'lib/termtter/command.rb', line 5 def help @help end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/termtter/command.rb', line 5 def name @name end |
Instance Method Details
#alias=(a) ⇒ Object
call-seq:
alias= :: Symbol -> ()
104 105 106 |
# File 'lib/termtter/command.rb', line 104 def alias=(a) self.aliases = [a] end |
#call(cmd = nil, arg = nil, original_text = nil) ⇒ Object
call-seq:
call :: ???
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/termtter/command.rb', line 60 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.} #{'%.2fsec' % (Time.now - from)}" } raise e end |
#command_words ⇒ Object
114 115 116 |
# File 'lib/termtter/command.rb', line 114 def command_words name.to_s.split(/\s+/) end |
#commands ⇒ Object
call-seq:
commands :: [Symbol]
94 95 96 |
# File 'lib/termtter/command.rb', line 94 def commands [name] + aliases end |
#complement(input) ⇒ Object
call-seq:
complement :: String -> [String]
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/termtter/command.rb', line 44 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?(input) ⇒ Boolean
call-seq:
match? :: String -> Boolean
81 82 83 |
# File 'lib/termtter/command.rb', line 81 def match?(input) !!pattern.match(input) end |
#pattern ⇒ Object
call-seq:
pattern :: Regexp
87 88 89 90 |
# File 'lib/termtter/command.rb', line 87 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(cfg) ⇒ Object
call-seq:
set :: Hash -> ()
33 34 35 36 37 38 39 40 |
# File 'lib/termtter/command.rb', line 33 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. = cfg[:author] end |
#split_command_line(line) ⇒ Object
call-seq:
split_command_line :: String -> (String, String)
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/termtter/command.rb', line 120 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 |