Class: NewPage::ReadlineInputHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/new_page/input_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ ReadlineInputHandler

Returns a new instance of ReadlineInputHandler.



53
54
55
56
57
58
59
60
# File 'lib/new_page/input_hash.rb', line 53

def initialize(**args)
  print "#{self} -> #{args}\n"
  if (args.include?('completion_handler'))
    @history = args['completion_handler'].new(**args)
  else
    @history = NoCompletion.new
  end
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



51
52
53
# File 'lib/new_page/input_hash.rb', line 51

def history
  @history
end

Instance Method Details

#completionObject



62
63
64
# File 'lib/new_page/input_hash.rb', line 62

def completion
  @history.completion
end

#input(key) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/new_page/input_hash.rb', line 75

def input(key)
  print "Reading #{key} at #{self}"
  old = Readline.completion_proc
  Readline.completion_proc = proc do |partial|
    if completion.include? key.to_s then
      completion[key.to_s].query(/^#{Regexp.escape(partial)}/)
    else
      []
    end
  end
  line = ""
  begin
    line = Readline.readline("#{key} : ", true).strip
  ensure
    Readline.completion_proc = old unless old.nil?
  end
  update_completion(key, line)
  @history.write_history
  line
end

#update_completion(key, value) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/new_page/input_hash.rb', line 66

def update_completion(key, value)
  values = value.split /[ \t]/
  if (completion.include?(key)) then
    completion[key] += values
  else
    completion[key] = values
  end
end