#!/usr/bin/ruby require 'io/console' # Map arrow keys to VI keys, and Ctrl+C to 'q' key_map = { 'D' => 'h', 'C' => 'l', 'A' => 'k', 'B' => 'j', "\3" => 'q' } # Map key values to themselves key_map.values().each{|x| key_map[x] = x } # Spawn thread to read keys, since getch will block last_key = 'j' input_thread = Thread.new{ while last_key != 'q' key = STDIN.getch() last_key = key_map[key] || last_key end } # Show banner text and hide cursor print "Type 'q' to quit\n\n\e[?25l" frame = 0 while last_key != 'q' text = "\e[1A\e[1G" if last_key == 'h' text += "Left " elsif last_key == 'l' text += "Right" elsif last_key == 'k' text += "Up " else text += "Down " end text += "\e[1B\e[5DFrame %2d" % frame print text frame = -~frame % 30 sleep 1. / 30 end # Show cursor again and reset colors print "\e[?25h\e[0m\n" input_thread.join