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:            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)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/termtter/command.rb', line 15

def initialize(args)
  raise ArgumentError, ":name is not given." unless args.has_key?(:name)
  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.



7
8
9
# File 'lib/termtter/command.rb', line 7

def aliases
  @aliases
end

#completion_procObject

Returns the value of attribute completion_proc.



7
8
9
# File 'lib/termtter/command.rb', line 7

def completion_proc
  @completion_proc
end

#exec_procObject

Returns the value of attribute exec_proc.



7
8
9
# File 'lib/termtter/command.rb', line 7

def exec_proc
  @exec_proc
end

#helpObject

Returns the value of attribute help.



7
8
9
# File 'lib/termtter/command.rb', line 7

def help
  @help
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/termtter/command.rb', line 7

def name
  @name
end

Class Method Details

.split_command_line(line) ⇒ Object



76
77
78
# File 'lib/termtter/command.rb', line 76

def self.split_command_line(line)
  line.strip.split(/\s+/, 2)
end

Instance Method Details

#call(cmd = nil, arg = nil, original_text = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/termtter/command.rb', line 51

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



72
73
74
# File 'lib/termtter/command.rb', line 72

def commands
  [name] + aliases
end

#complement(input) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/termtter/command.rb', line 38

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

Returns:

  • (Boolean)


63
64
65
# File 'lib/termtter/command.rb', line 63

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

#patternObject



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

def pattern
  commands_regex = commands.map {|name| Regexp.quote(name.to_s) }.join('|')
  /^((#{commands_regex})|(#{commands_regex})\s+(.*?))\s*$/
end

#set(cfg) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/termtter/command.rb', line 30

def set(cfg)
  @name            = cfg[:name].to_sym
  @aliases         = cfg[:aliases].map {|e| e.to_sym }
  @exec_proc       = cfg[:exec_proc]
  @completion_proc = cfg[:completion_proc]
  @help            = cfg[:help]
end