Class: Utils::InteractiveSearch

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/utils/interactive_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(query:, found:, output: STDOUT, prompt: 'Search? %s') ⇒ InteractiveSearch

Returns a new instance of InteractiveSearch.



5
6
7
8
9
10
11
# File 'lib/utils/interactive_search.rb', line 5

def initialize(query:, found:, output: STDOUT, prompt: 'Search? %s')
  @query  = query
  @found  = found
  @output = output
  @prompt = prompt
  @answer = ''
end

Instance Method Details

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/utils/interactive_search.rb', line 13

def start
  @output.print clear_screen
  loop do
    @output.print move_home(@prompt % @answer)
    case getc
    when true
      @output.print clear_screen move_home reset
      return !!@found.(@answer)
    when false
      return false
    end
    result = @query.(@answer)
    @output.print clear_screen
    unless @answer.empty?
      @output.print clear_screen move_to_line(2, result)
    end
  end
end