Skip to content

Instantly share code, notes, and snippets.

@carols10cents
Created February 1, 2015 21:29
Show Gist options
  • Select an option

  • Save carols10cents/ca138ab871266bcae385 to your computer and use it in GitHub Desktop.

Select an option

Save carols10cents/ca138ab871266bcae385 to your computer and use it in GitHub Desktop.
More procedural/less abstraction selecta
options = Configuration.parse_options(ARGV)
input_lines = $stdin.readlines
File.open("/dev/tty", "r") do |in_file|
File.open("/dev/tty", "w") do |out_file|
tty = TTY.new(in_file, out_file)
screen = Screen.new(tty)
screen.tty.stty("-echo -icanon")
config = Configuration.from_inputs(input_lines, options, screen.height)
search = Search.blank(config)
# We emit the number of lines we'll use later so we don't clobber whatever
# was already on the screen.
config.visible_choices.times { tty.puts }
while not search.done?
rendered = Renderer.new(search).render
start_line = screen.height - search.config.visible_choices - 1
ansi.hide_cursor!
screen.write_lines(start_line, rendered.choices)
screen.move_cursor(start_line, rendered.search_line.length)
ansi.show_cursor!
search = handle_key(search, tty.get_char)
end
# Always move the cursor to the bottom so the next program doesn't draw
# over whatever we left on the screen.
screen.move_cursor(screen.height - 1, 0)
screen.tty.stty("#{@original_stty_state}")
tty.puts
end
end
unless search.selection == Search::NoSelection
puts search.selection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment