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.
-
#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
- #commands ⇒ Object
- #complement(input) ⇒ Object
-
#exec_if_match(input) ⇒ Object
MEMO: Termtter:Client からはこのメソッドを呼び出すことになると思う。.
- #execute(arg) ⇒ Object
-
#initialize(args) ⇒ Command
constructor
args name: Symbol as command name aliases: Array of command alias (ex. [‘u’, ‘up’]) exec_proc: Proc for procedure of the command.
-
#match?(input) ⇒ Boolean
return array like [command, arg].
- #pattern ⇒ Object
Constructor Details
#initialize(args) ⇒ Command
args
name: 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)
13 14 15 16 17 18 19 20 |
# File 'lib/termtter/command.rb', line 13 def initialize(args) raise ArgumentError, ":name is not given." unless args.has_key?(:name) @name = args[:name].to_sym @aliases = args[:aliases] ? args[:aliases].map {|i| i.to_sym } : [] @exec_proc = args[:exec_proc] || lambda {|arg|} @completion_proc = args[:completion_proc] || lambda {|command, arg| [] } @help = args[:help] 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 |
#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
#commands ⇒ Object
72 73 74 |
# File 'lib/termtter/command.rb', line 72 def commands [name] + aliases end |
#complement(input) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/termtter/command.rb', line 22 def complement(input) command_info = match?(input) if command_info [completion_proc.call(command_info[0], command_info[1] || '')].flatten.compact else [name.to_s].grep(/^#{Regexp.quote(input)}/) end end |
#exec_if_match(input) ⇒ Object
MEMO: Termtter:Client からはこのメソッドを呼び出すことになると思う。
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/termtter/command.rb', line 32 def exec_if_match(input) command_info = match?(input) if command_info result = execute(command_info[1]) unless result.nil? return result else return true end else return nil end end |
#execute(arg) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/termtter/command.rb', line 55 def execute(arg) arg = case arg when nil '' when String arg else raise ArgumentError, 'arg should be String or nil' end exec_proc.call(arg) end |
#match?(input) ⇒ Boolean
return array like [command, arg]
47 48 49 50 51 52 53 |
# File 'lib/termtter/command.rb', line 47 def match?(input) if pattern =~ input [$2 || $3, $4] # $2 or $3 => command, $4 => argument else nil end end |
#pattern ⇒ Object
67 68 69 70 |
# File 'lib/termtter/command.rb', line 67 def pattern commands_regex = commands.map {|i| Regexp.quote(i.to_s) }.join('|') /^((#{commands_regex})|(#{commands_regex})\s+(.*?))\s*$/ end |