Class: Termtter::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/termtter/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)

Raises:

  • (ArgumentError)


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

#aliasesObject

Returns the value of attribute aliases.



5
6
7
# File 'lib/termtter/command.rb', line 5

def aliases
  @aliases
end

#completion_procObject

Returns the value of attribute completion_proc.



5
6
7
# File 'lib/termtter/command.rb', line 5

def completion_proc
  @completion_proc
end

#exec_procObject

Returns the value of attribute exec_proc.



5
6
7
# File 'lib/termtter/command.rb', line 5

def exec_proc
  @exec_proc
end

#helpObject

Returns the value of attribute help.



5
6
7
# File 'lib/termtter/command.rb', line 5

def help
  @help
end

#nameObject

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

#commandsObject

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/termtter/command.rb', line 66

def match?(input)
  !(pattern !~input)
end

#patternObject

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