-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path07-window_with_border.rb
87 lines (64 loc) · 2.06 KB
/
07-window_with_border.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'rubygems'
require 'ncurses'
def create_window(height, width, starty, startx)
window = Ncurses.newwin(height, width, starty, startx)
Ncurses.box(window, 0, 0) # 0, 0 gives default characters for the vertical
# and horizontal lines
Ncurses.wrefresh(window) # show that box
window
end
def destroy_window(window)
# Draws over the border with the space character (ASCII #32).
# The parameters are:
# wnd - window object
# ls - left edge character index
# rs - right edge character index
# ts - top edge character index
# bs - bottom edge character index
# tl - top left character index
# tr - top right character index
# bl - bottom left character index
# br - bottom right character index
Ncurses.wborder(window, 32, 32, 32, 32, 32, 32, 32, 32)
Ncurses.wrefresh(window)
Ncurses.delwin(window)
end
begin
Ncurses.initscr
Ncurses.cbreak
Ncurses.keypad(Ncurses.stdscr, true)
cols = Ncurses.getmaxx(Ncurses.stdscr)
rows = Ncurses.getmaxy(Ncurses.stdscr)
height = 3
width = 10
starty = (rows - height) / 2
startx = (cols - width) / 2
Ncurses.printw("Press F1 to exit")
Ncurses.refresh
window = create_window(height, width, starty, startx)
while (ch = Ncurses.getch) != Ncurses::KEY_F1
if ch == Ncurses::KEY_LEFT
startx -= 1
destroy_window(window)
window = create_window(height, width, starty, startx)
elsif ch == Ncurses::KEY_RIGHT
startx += 1
destroy_window(window)
window = create_window(height, width, starty, startx)
elsif ch == Ncurses::KEY_UP
starty -= 1
destroy_window(window)
window = create_window(height, width, starty, startx)
elsif ch == Ncurses::KEY_DOWN
starty += 1
destroy_window(window)
window = create_window(height, width, starty, startx)
end
end
rescue Exception => e
Ncurses.printw e.inspect + "\n"
Ncurses.printw e.backtrace.join("\n")
Ncurses.getch
ensure
Ncurses.endwin
end