Sha256: b029be3ab9d8219d0fe616b121765cb3c31f82f46532ff2f371bc1b10fe9b50b
Contents?: true
Size: 1.42 KB
Versions: 5
Compression:
Stored size: 1.42 KB
Contents
require 'term/ansicolor' class Utils::SearchUI include Term::ANSIColor def initialize(query:, found:, output: STDOUT, prompt: 'Search? %s') @selector = 0 @query = query @found = found @output = output @prompt = prompt @answer = '' end 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 private def getc system 'stty raw -echo' c = STDIN.getc system 'stty cooked echo' case c when "\x03" false when "\e" STDIN.getc == ?[ or return nil STDIN.getc =~ /\A([AB])\z/ or return nil if $1 == ?A @selector -= 1 else @selector += 1 end @selector = [ @selector, 0 ].max nil when ?\r true when "\x7f" @selector = 0 @answer.chop! nil when "\v" @selector = 0 @answer.clear nil when /\A[\x00-\x1f]\z/ nil else @selector = 0 @answer << c nil end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
utils-0.21.2 | lib/utils/search_ui.rb |
utils-0.21.1 | lib/utils/search_ui.rb |
utils-0.21.0 | lib/utils/search_ui.rb |
utils-0.20.0 | lib/utils/search_ui.rb |
utils-0.19.0 | lib/utils/search_ui.rb |