Class: CMDB::Shell::Prompter

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/shell/prompter.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Prompter

Returns a new instance of Prompter.



3
4
5
6
# File 'lib/cmdb/shell/prompter.rb', line 3

def initialize(text)
  require 'readline'
  @c = text
end

Instance Method Details

#read(shell) ⇒ Object

Prompt the user for input and read a line. Offer autocompletion services using Readline and CMDB/DSL searches.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cmdb/shell/prompter.rb', line 10

def read(shell)
  Readline.completion_proc = proc do |word|
    commands = shell.dsl.class.instance_methods.select do |m|
      m.to_s.index(word) == 0 && m !~ /[^a-z]/
    end.map(&:to_s)
    next commands if commands.any?

    hits  = shell.cmdb.search(shell.expand_path(word)).keys
    hits.sort! { |x, y| x.size <=> y.size }
    pwd = shell.pwd.join('.')
    hits[0...CMDB::Shell::MANY].map { |k| k.sub(pwd, '') }
  end

  Readline.readline(prompt(shell.cmdb, shell.pwd), true)
end