Skip to content

Instantly share code, notes, and snippets.

@phorsfall
Created August 19, 2010 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phorsfall/537620 to your computer and use it in GitHub Desktop.
Save phorsfall/537620 to your computer and use it in GitHub Desktop.
require 'curses'
# Innit.
Curses::init_screen
# Don't echo keypress on the console.
Curses::noecho
# Create a centered window, of size:
height, width = 32, 64
win = Curses::Window.new(height, width, (Curses::lines - height) / 2 , (Curses::cols - width) / 2)
# Arguments can specify horizontal and vertical characters used.
# Not sure how to specify which chars to use for corners.
win.box(0,0)
# Make arrow keys match Curses::KEY_UP and friends as expected.
win.keypad = true
# Don't block when calling Window#getch and friends.
win.nodelay = true
# Enable colour.
Curses.start_color
# Curses define the following constant, which are just integers 0-7:
# Curses::COLOR_BLUE Curses::COLOR_CYAN Curses::COLOR_GREEN Curses::COLOR_MAGENTA
# Curses::COLOR_RED Curses::COLOR_WHITE Curses::COLOR_YELLOW Curses::COLOR_BLACK
# You can use 16 colours if your terminal support it.
# e.g. export TERM=xterm-16color
# Pairs of foreground and background colours declared before use.
0.upto(15) do |fg|
Curses.init_pair(fg+1, fg, Curses::COLOR_BLACK)
end
# Can flip fg and bg.
win.attron(Curses::A_REVERSE)
#win.attroff(Curses::A_REVERSE)
begin
100.times do
# Random point.
x, y = rand(31), rand(30)
# Random colour pair.
win.attron(Curses.color_pair(rand(16)+1))
win.setpos(y+1, (x*2)+1)
# Add a single char.
#win.addch(32)
win.addstr(" ")
end
# Hide cursor.
Curses.curs_set(0)
win.refresh
sleep 0.1
end while win.getch != ?q
# Clean up.
win.close
Curses::close_screen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment