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.
Class Method Summary collapse
-
.split_command_line(line) ⇒ Object
- split_command_line
-
String -> (String, String).
Instance Method Summary collapse
-
#alias=(a) ⇒ Object
- alias=
-
Symbol -> ().
-
#call(cmd = nil, arg = nil, original_text = nil) ⇒ Object
- call
-
???.
-
#commands ⇒ Object
- commands
-
[Symbol].
-
#complement(input) ⇒ Object
- 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
- match?
-
String -> Boolean.
-
#pattern ⇒ Object
- pattern
-
Regexp.
-
#set(cfg) ⇒ Object
- set
-
Hash -> ().
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)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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| [] } }.merge(args) 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 |
#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 |
Class Method Details
.split_command_line(line) ⇒ Object
- split_command_line
-
String -> (String, String)
91 92 93 |
# File 'lib/termtter/command.rb', line 91 def self.split_command_line(line) line.strip.split(/\s+/, 2) end |
Instance Method Details
#alias=(a) ⇒ Object
- alias=
-
Symbol -> ()
86 87 88 |
# File 'lib/termtter/command.rb', line 86 def alias=(a) self.aliases = [a] end |
#call(cmd = nil, arg = nil, original_text = nil) ⇒ Object
- call
-
???
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/termtter/command.rb', line 53 def call(cmd = nil, arg = nil, original_text = nil) arg = case arg when nil '' when String arg else raise ArgumentError, 'arg should be String or nil' end exec_proc.call(arg) end |
#commands ⇒ Object
- commands
- Symbol
77 78 79 |
# File 'lib/termtter/command.rb', line 77 def commands [name] + aliases end |
#complement(input) ⇒ Object
- complement
-
String -> [String]
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/termtter/command.rb', line 39 def complement(input) if match?(input) && input =~ /^[^\s]+\s/ if completion_proc command_str, command_arg = Command.split_command_line(input) [completion_proc.call(command_str, command_arg || '')].flatten.compact else [] end else [name.to_s, aliases.to_s].grep(/^#{Regexp.quote(input)}/) end end |
#match?(input) ⇒ Boolean
- match?
-
String -> Boolean
66 67 68 |
# File 'lib/termtter/command.rb', line 66 def match?(input) !(pattern !~input) end |
#pattern ⇒ Object
- pattern
-
Regexp
71 72 73 74 |
# File 'lib/termtter/command.rb', line 71 def pattern commands_regex = commands.map {|name| Regexp.quote(name.to_s) }.join('|') /^((#{commands_regex})|(#{commands_regex})\s+(.*?))\s*$/ end |
#set(cfg) ⇒ Object
- set
-
Hash -> ()
30 31 32 33 34 35 36 |
# File 'lib/termtter/command.rb', line 30 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] end |