Skip to content

Instantly share code, notes, and snippets.

@pricees
Created November 25, 2012 21:53
Show Gist options
  • Select an option

  • Save pricees/4145555 to your computer and use it in GitHub Desktop.

Select an option

Save pricees/4145555 to your computer and use it in GitHub Desktop.
Curses Part 2-3: Subwindow
#!/usr/local/bin/ruby
require "curses"
begin
Curses.init_screen
Curses.noecho
win = Curses::Window.new(Curses.lines-10, Curses.cols - 10, 5, 5)
win.keypad(true)
STEP_Y = 1
STEP_X = 5
SW_SIDE = 6
subwin = win.subwin(5, 10, SW_SIDE, SW_SIDE)
x, y = subwin.begx, subwin.begy
loop do
win.clear
win.box("|", "-")
win.refresh
subwin.move(y, x)
subwin.setpos(1,2)
subwin.addstr("#{x} \n/ #{subwin.maxx} /\n #{win.maxx}")
subwin.box(".", ".")
subwin.refresh
case win.getch
when Curses::Key::UP then y -= STEP_Y
when Curses::Key::DOWN then y += STEP_Y
when Curses::Key::LEFT then x -= STEP_X
when Curses::Key::RIGHT then x += STEP_X
when ?q then break
end
# Check bounds
if y >= win.maxy
y = win.maxy - 1#subwin.maxy - 1
end
y = SW_SIDE if y < SW_SIDE
if x + SW_SIDE > win.maxx
x = win.maxx - SW_SIDE#(subwin.maxx / 2)
end
x = SW_SIDE if x < SW_SIDE
end
win.close
ensure
Curses.close_screen
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment