Class: Utils::SearchUI

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SearchUI.



6
7
8
9
10
11
12
13
# File 'lib/utils/search_ui.rb', line 6

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

Instance Method Details

#startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/utils/search_ui.rb', line 15

def start
  @output.print clear_screen, move_home, reset
  loop do
    @output.print move_home { @prompt % @answer }
    case getc
    when true
      @output.print clear_screen, move_home, reset
      if result = @found.(@answer, @selector)
        return result
      else
        return nil
      end
    when false
      return nil
    end
    result = @query.(@answer, @selector)
    @output.print clear_screen
    unless @answer.empty?
      @output.print move_home { ?\n + result }
    end
  end
end