Sha256: f4eb669f311ea2be4cf5476207591ac85e8c3f71f1c55023204c9773aaede384

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

require 'term/ansicolor'
class Utils::InteractiveSearch
  include Term::ANSIColor

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

  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

  private

  def getc
    system 'stty raw -echo'
    c = STDIN.getc
    system 'stty cooked echo'
    case c
    when "\x03", "\e"
      false
    when ?\r
      true
    when "\x7f"
      @answer.chop!
      nil
    when "\v"
      @answer.clear
    when /\A[\x00-\x1f]\z/
      nil
    else
      @answer << c
      nil
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
utils-0.17.0 lib/utils/interactive_search.rb
utils-0.16.0 lib/utils/interactive_search.rb
utils-0.15.1 lib/utils/interactive_search.rb
utils-0.15.0 lib/utils/interactive_search.rb
utils-0.14.0 lib/utils/interactive_search.rb
utils-0.13.0 lib/utils/interactive_search.rb
utils-0.12.0 lib/utils/interactive_search.rb