Sha256: 774a73bda5eb1f8b075ccd600ca29ab10a2857b598ff5fbab996527e445d3e2c
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
#!/usr/bin/env ruby require 'io/console' require_relative 'ansi' require_relative 'query' require_relative 'renderer' module Omnibar class App attr_accessor :input attr_accessor :selection @@queries = [] def self.add_query(query) @@queries.push(query) end def initialize @input = '' @selection = 0 Omnibar.load_config end def run IO.console.raw do ANSI.clear_screen ANSI.move_cursor(0, 0) loop do render handle_input end end end def render Renderer.new(input, results, selection).render! end def handle_input(prefix = '') char = prefix << $stdin.getc case char when "\u0003" # ctrl-c quit when "\u007F" # backspace self.input = input[0..-2] when "\e", "\e[" handle_input(char) when "\e[A" self.selection = [selection - 1, 0].max when "\e[B" self.selection = [selection + 1, visible_queries.count - 1].min when "\r" perform_action! else input << char end end def results return [] if input.empty? queries.map(&:preview_text).compact end def perform_action! visible_queries[selection]&.perform! self.input = "" # TODO: after_perform callback end def queries @@queries.map { |q| q.new(input) } end def visible_queries queries.reject { |q| q.preview_text.nil? } end def quit ANSI.clear_screen exit 0 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
omnibar-0.0.2 | lib/omnibar/app.rb |