Class: Termtter::Command

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

Instance Attribute 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

Instance Method Details

#alias=(a) ⇒ Object

alias=

Symbol -> ()



87
88
89
# File 'lib/termtter/command.rb', line 87

def alias=(a)
  self.aliases = [a]
end

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

call

???



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/termtter/command.rb', line 54

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

#command_wordsObject



91
92
93
# File 'lib/termtter/command.rb', line 91

def command_words
  name.to_s.split(/\s+/)
end

#commandsObject

commands
Symbol


78
79
80
# File 'lib/termtter/command.rb', line 78

def commands
  [name] + aliases
end

#complement(input) ⇒ Object

complement

String -> [String]



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

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

match?

String -> Boolean

Returns:

  • (Boolean)


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

def match?(input)
  pattern === input
end

#patternObject

pattern

Regexp



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

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

#split_command_line(line) ⇒ Object

split_command_line

String -> (String, String)



96
97
98
99
100
# File 'lib/termtter/command.rb', line 96

def split_command_line(line)
  command_words_count = command_words.size
  parts = line.strip.split(/\s+/, command_words_count + 1)
  [parts[0...command_words_count].join(' '), parts[command_words_count] || '']
end