Created
August 18, 2014 16:24
-
-
Save kmdsbng/58b275c5d235ec043196 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| require 'curses' | |
| def main | |
| Curses.init_screen | |
| begin | |
| @y = Curses.lines / 2 | |
| @x = Curses.cols / 2 | |
| while true | |
| draw_cursor | |
| @input = wait_input | |
| break unless @input | |
| end | |
| ensure | |
| Curses.close_screen | |
| end | |
| end | |
| def draw_cursor | |
| s = "@" | |
| Curses.clear | |
| Curses.setpos(@y, @x) | |
| Curses.addstr(s) | |
| Curses.setpos(Curses.lines - 1, 0) | |
| Curses.addstr("Y:#{@y} X:#{@x} input:#{@input}") | |
| Curses.refresh | |
| end | |
| def wait_input | |
| input = Curses.getch | |
| return nil if input == 27 || input == 'q' # <ESC> key | |
| case input | |
| when 'h' then move_left(1) | |
| when 'j' then move_down(1) | |
| when 'k' then move_up(1) | |
| when 'l' then move_right(1) | |
| end | |
| input | |
| end | |
| def move_left(n) | |
| @x = [@x - n, 0].max | |
| end | |
| def move_right(n) | |
| @x = [@x + n, Curses.cols - 1].min | |
| end | |
| def move_up(n) | |
| @y = [@y - n, 0].max | |
| end | |
| def move_down(n) | |
| @y = [@y + n, Curses.lines - 2].min | |
| end | |
| case $PROGRAM_NAME | |
| when __FILE__ | |
| main | |
| when /spec[^\/]*$/ | |
| # {spec of the implementation} | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment