Class: SearchUI::Search

Inherits:
Object
  • Object
show all
Extended by:
Term::ANSIColor
Includes:
Term::ANSIColor
Defined in:
lib/search_ui/search.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Search.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/search_ui/search.rb', line 7

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

Instance Method Details

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/search_ui/search.rb', line 24

def start
  @output.print reset
  @matches = @match.(@answer)
  @selector = [ 0, [ @selector, @matches.size - 1 ].min ].max
  result = @query.(@answer, @matches, @selector)
  loop do
    @output.print clear_screen
    @output.print move_home { @prompt % @answer + ?\n + result }
    case getc
    when true
      @output.print clear_screen, move_home, reset
      if result = @found.(@answer, @matches, @selector)
        return result
      else
        return nil
      end
    when false
      return nil
    end
    @matches = @match.(@answer)
    @selector = [ 0, [ @selector, @matches.size - 1 ].min ].max
    result = @query.(@answer, @matches, @selector)
  end
end