Module: LokkaSh::Command

Defined in:
lib/lokka-sh/command.rb,
lib/lokka-sh/command/define.rb

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



27
28
29
# File 'lib/lokka-sh/command.rb', line 27

def [](name)
  commands[name.to_sym]
end

.clearObject



53
54
55
56
# File 'lib/lokka-sh/command.rb', line 53

def clear
  commands.clear
  completions.clear
end

.command_namesObject



23
24
25
# File 'lib/lokka-sh/command.rb', line 23

def command_names
  commands.keys
end

.commandsObject



4
5
6
# File 'lib/lokka-sh/command.rb', line 4

def commands
  @commands ||= {}
end

.completion_procObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lokka-sh/command.rb', line 31

def completion_proc
  lambda { |line|
    regex = /#{Regexp.quote(line)}/
    completions.map { |completion|
      case completion
      when String
        completion if completion.match(regex)
      when Proc
        completion.call(line)
      end
    }.compact
  }
end

.completionsObject



45
46
47
# File 'lib/lokka-sh/command.rb', line 45

def completions
  @completions ||= []
end

.completions=(completions) ⇒ Object



49
50
51
# File 'lib/lokka-sh/command.rb', line 49

def completions=(completions)
  @completions = completions
end

.define(*names, &block) ⇒ Object



8
9
10
11
12
13
# File 'lib/lokka-sh/command.rb', line 8

def define(*names, &block)
  names.each do |name|
    commands[name.to_sym] = block
    completions << name.to_s
  end
end

.find(line) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lokka-sh/command.rb', line 15

def find(line)
  if name = line.split(/\s+/, 2)[0]
    commands[name.to_sym]
  else
    nil
  end
end